Code Snippets

  

VB.NET Source Code


Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 148,655 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,475 people online right now. Registration is fast and FREE... Join Now!





Create an application desktop toolbar

Makes a form that you can dock in desktop.

Submitted By: m2s87
Actions:
Rating:
Views: 9,248

Language: VB.NET

Last Modified: January 25, 2007
Instructions: Save it as a new class

You can use following snippet as a form code:
http://www.dreamincode.net/code/snippet896.htm

Snippet


  1. Imports System.Windows.Forms
  2.  
  3. Friend Class m2s87
  4.     Inherits System.Windows.Forms.Form
  5.  
  6.     Public Declare Function GetWindowPlacement Lib "User32" (ByVal hWnd As Integer, ByRef lpwndpl As WINDOWPLACEMENT) As Integer
  7.     Public Declare Function DrawFrameControl Lib "User32" (ByVal hdc As Integer, ByRef lpRect As RECT, ByVal un1 As Integer, ByVal un2 As Integer) As Integer
  8.     Public Declare Function SetRect Lib "User32" (ByRef lpRect As RECT, ByVal X1 As Integer, ByVal Y1 As Integer, ByVal X2 As Integer, ByVal Y2 As Integer) As Integer
  9.     Public Declare Function GetCursorPos Lib "User32" (ByRef lpPoint As POINTAPI) As Integer
  10.     Private Declare Function SHAppBarMessage Lib "Shell32" (ByVal dwMessage As ABM_, ByRef pData As APPBARDATA) As Integer
  11.     Private Declare Function SetWindowPos Lib "User32" (ByVal hWnd As Integer, ByVal hwndInsertAfter As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
  12.  
  13.     Event m2s87Load(ByVal x As Object)
  14.  
  15.     Public Structure RECT
  16.         Dim Left As Integer
  17.         Dim Top As Integer
  18.         Dim Right As Integer
  19.         Dim Bottom As Integer
  20.     End Structure
  21.  
  22.     Structure POINTAPI
  23.         Dim X As Integer
  24.         Dim Y As Integer
  25.     End Structure
  26.  
  27.     Structure WINDOWPLACEMENT
  28.         Dim Length As Integer
  29.         Dim flags As Integer
  30.         Dim ShowCmd As Integer
  31.         Dim ptMinPosition As POINTAPI
  32.         Dim ptMaxPosition As POINTAPI
  33.         Dim rcNormalPosition As RECT
  34.     End Structure
  35.  
  36.     Structure APPBARDATA
  37.         Dim cbSize As Integer
  38.         Dim hWnd As Integer
  39.         Dim uCallbackMessage As Integer
  40.         Dim uEdge As Integer
  41.         Dim rc As RECT
  42.         Dim lParam As Integer
  43.     End Structure
  44.  
  45.     Public Enum DockWhere As Integer
  46.         LEFT = &H0S
  47.         TOP = &H1S
  48.         RIGHT = &H2S
  49.         BOTTOM = &H3S
  50.         NONE = -1
  51.     End Enum
  52.     Public Enum ABM_ As Integer
  53.         _NEW = &H0S
  54.         _REMOVE = &H1S
  55.         _SETPOS = &H3S
  56.         _GETTASKBARPOS = &H5S
  57.     End Enum
  58.  
  59.  
  60.     Const HWND_TOPMOST As Short = -1
  61.     Const SWP_NOSIZE As Short = &H1S
  62.     Const SWP_NOMOVE As Short = &H2S
  63.  
  64.     Public Const DFC_BUTTON As Short = 4
  65.     Public Const DFCS_BUTTON3STATE As Short = &H10S
  66.  
  67.     Public Ymin, Xmin, Xmax, Ymax As Integer
  68.     Public Docked As Boolean
  69.     Public WPL As WINDOWPLACEMENT
  70.     Public PxSense As Integer = 20
  71.     Public DockMode As DockWhere
  72.  
  73.     Dim APD As APPBARDATA
  74.     Dim vaba As Rectangle = System.Windows.Forms.Screen.PrimaryScreen.Bounds
  75.     Dim WithEvents taimer As New System.Windows.Forms.Timer
  76.  
  77.     Private components As System.ComponentModel.IContainer
  78.  
  79.     Friend myY As Integer = 212
  80.     Friend myX As Integer = 212
  81.  
  82.     Public Sub New()
  83.         taimer.Interval = 50
  84.         taimer.Enabled = True
  85.  
  86.         Xmin = PxSense
  87.         Ymin = PxSense
  88.  
  89.         Xmax = CInt(vaba.Width - PxSense)
  90.         Ymax = CInt(vaba.Height - PxSense)
  91.     End Sub
  92.     Private Sub InitializeComponent()
  93.         Me.SuspendLayout()
  94.         '
  95.         'm2s87
  96.         '
  97.         Me.ClientSize = New System.Drawing.Size(292, 268)
  98.         Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
  99.         Me.MaximizeBox = False
  100.         Me.MinimizeBox = False
  101.         Me.Name = "m2s87"
  102.         Me.ShowInTaskbar = False
  103.         Me.ResumeLayout(False)
  104.  
  105.     End Sub
  106.  
  107.     Private Sub Hetk(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles taimer.Tick
  108.  
  109.         If Docked Then Exit Sub
  110.         taimer.Enabled = False
  111.         WPL.Length = Len(WPL)
  112.         Call GetWindowPlacement(Me.Handle.ToInt32, WPL)
  113.  
  114.         If DockMode <> DockWhere.NONE Then
  115.             Call InitAppBar()
  116.         End If
  117.         taimer.Enabled = True
  118.     End Sub
  119.     Public Sub InitAppBar()
  120.         Dim X, Y As Integer
  121.         Dim Fw, Fx, Fy, Fh As Integer
  122.         Dim TPos As APPBARDATA
  123.  
  124.         Docked = True
  125.  
  126.         Call SHAppBarMessage(ABM_._GETTASKBARPOS, TPos)
  127.  
  128.         Y = myY
  129.         X = myX
  130.  
  131.         Call SHAppBarMessage(ABM_._NEW, APD)
  132.  
  133.         Select Case DockMode
  134.             Case DockWhere.LEFT : With APD
  135.                     .uEdge = DockWhere.LEFT
  136.                     .rc.Top = 0
  137.                     .rc.Left = 0
  138.                     .rc.Right = X
  139.                     .rc.Bottom = TPos.rc.Top
  140.                 End With
  141.                 Fx = 0
  142.                 Fy = 0
  143.                 Fw = X
  144.                 Fh = TPos.rc.Top
  145.  
  146.             Case DockWhere.TOP : With APD
  147.                     .uEdge = DockWhere.TOP
  148.                     .rc.Top = 0
  149.                     .rc.Left = 0
  150.                     .rc.Right = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width
  151.                     .rc.Bottom = Y
  152.                 End With
  153.                 Fx = 0
  154.                 Fy = 0
  155.                 Fw = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width
  156.                 Fh = Y
  157.  
  158.             Case DockWhere.RIGHT : With APD
  159.                     .uEdge = DockWhere.RIGHT
  160.                     .rc.Top = 0
  161.                     .rc.Left = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width - X '- FrameX
  162.                     .rc.Right = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width
  163.                     .rc.Bottom = Y
  164.                 End With
  165.                 Fx = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width - X
  166.                 Fy = 0
  167.                 Fw = X
  168.                 Fh = TPos.rc.Top
  169.  
  170.             Case DockWhere.BOTTOM : With APD
  171.                     .uEdge = DockWhere.BOTTOM
  172.                     .rc.Top = TPos.rc.Top - Y
  173.                     .rc.Left = 0
  174.                     .rc.Right = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width
  175.                     .rc.Bottom = TPos.rc.Top
  176.                 End With
  177.                 Fx = 0
  178.                 Fy = TPos.rc.Top - Y
  179.                 Fw = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width
  180.                 Fh = Y
  181.         End Select
  182.  
  183.         Call SHAppBarMessage(ABM_._SETPOS, APD)
  184.         System.Windows.Forms.Application.DoEvents()
  185.  
  186.         With Me
  187.             '.Show()
  188.             Call SetWindowPos(.Handle.ToInt32, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE Or SWP_NOMOVE)
  189.             .Top = Fy
  190.             .Left = Fx
  191.             .Width = Fw
  192.             .Height = Fh
  193.         End With
  194.     End Sub
  195.  
  196.     Public Sub ExitAppBar()
  197.         taimer.Enabled = False
  198.         If Docked Then Call SHAppBarMessage(ABM_._REMOVE, APD)
  199.         Docked = False
  200.         DockMode = DockWhere.NONE
  201.     End Sub
  202.  
  203.     Private Sub m2s87_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DoubleClick
  204.  
  205.     End Sub
  206.  
  207.     'Form overrides dispose to clean up the component list.
  208.     <System.Diagnostics.DebuggerNonUserCode()> _
  209.     Protected Overrides Sub Dispose(ByVal disposing As Boolean)
  210.         ExitAppBar()
  211.         If disposing AndAlso components IsNot Nothing Then
  212.             components.Dispose()
  213.         End If
  214.         MyBase.Dispose(disposing)
  215.     End Sub
  216. End Class

Copy & Paste


Comments


jmcc2k 2008-11-13 02:03:51

See ShellAppBar component from http://www.ssware.com/shlobj/shlobj.htm for quickly converting all your forms to appbars with full auto-hide and drag-docking functionality.


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month