By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,852 Members | 2,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,852 IT Pros & Developers. It's quick & easy.

AppBar API Positioning

P: 2
Hi, I have been playing around with an appbar (just like the one for the start menu). In the code, I have managed to get it to display on the right side of the screen flawlessly, except its not border-less, and it should be. Setting the border style to none breaks its alignment on the right and no longer stays aligned.

And when I assign the bar to the top of the screen instead, It simply does not position any open windows to FIT underneath it. This bar should behave just like the one in the start menu. For example;

In the appbar when assigned to the right, it pushes other windows out to the left to make room for the bar to fit and dock to the right side.

What I am trying to do, is assign the appbar to the top position. But when I do this, it simply positions itself to the top and stays topmost and overlays the other windows which are open and it is not what its meant to do.

It should be acting as it does on the right (Or like the start menu does)... I've been reading the API's on pinvoke.net, but fail to see what I am doing wrong.

If anyone can give me some pointers, that would be very helpful. Thanks.

Code:
Expand|Select|Wrap|Line Numbers
  1. Imports System.Drawing
  2. Imports System.Collections
  3. Imports System.ComponentModel
  4. Imports System.Windows.Forms
  5. Imports System.Data
  6. Imports System.Runtime.InteropServices
  7. Imports System.Collections.Generic
  8. Imports System.Windows
  9. Imports System.Threading
  10. Imports System.Windows.Forms.VisualStyles.VisualStyleElement
  11.  
  12. Public Class Form1
  13.  
  14.     Private Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hwnd As IntPtr, 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
  15.     Public Declare Auto Function MoveWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal X As Int32, ByVal Y As Int32, ByVal nWidth As Int32, ByVal nHeight As Int32, ByVal bRepaint As Boolean) As Boolean
  16.     Declare Function SHAppBarMessage Lib "shell32.dll" Alias "SHAppBarMessage" (ByVal dwMessage As Integer, ByRef pData As APPBARDATA) As Integer
  17.     Declare Function SetWindowPos Lib "user32.dll" (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
  18.  
  19.     Structure APPBARDATA
  20.         Dim cbSize As Integer
  21.         Dim hwnd As Integer
  22.         Dim uCallbackMessage As [Delegate]
  23.         Dim uEdge As Integer
  24.         Dim rc As RECT
  25.         Dim lParam As Integer ' message specific
  26.     End Structure
  27.     Structure RECT
  28.         Dim Left As Integer
  29.         Dim Top As Integer
  30.         Dim Right As Integer
  31.         Dim Bottom As Integer
  32.     End Structure
  33.  
  34.     Const ABE_LEFT As Integer = 0
  35.     Const ABE_TOP As Integer = &H1
  36.     Const ABE_RIGHT As Integer = 2
  37.     Const ABE_BOTTOM As Integer = 3
  38.  
  39.     Const ABM_NEW As Integer = 0
  40.     Const ABM_REMOVE As Integer = 1
  41.     Const ABM_QUERYPOS As Integer = 2
  42.     Const ABM_SETPOS As Integer = &H3
  43.     Const ABM_GETSTATE As Integer = 4
  44.     Const ABM_GETTASKBARPOS As Integer = 5
  45.     Const ABM_ACTIVATE As Integer = 6
  46.     Const ABM_GETAUTOHIDEBAR As Integer = 7
  47.     Const ABM_SETAUTOHIDEBAR As Integer = 8
  48.     Const ABM_WINDOWPOSCHANGED As Integer = 9
  49.  
  50.     Const ABS_AUTOHIDE As Integer = 1
  51.     Const ABS_ALWAYSONTOP As Integer = 2
  52.  
  53.     Const HWND_NOTTOPMOST As Integer = -2
  54.     Const HWND_TOPMOST As Integer = -1
  55.     Const HWND_TOP As Integer = 0
  56.     Const SHOWNORMAL As Integer = 5
  57.  
  58.     Const SWP_NOSIZE As Integer = &H1
  59.     Const SWP_NOMOVE As Short = &H2
  60.     Const SWP_NOZORDER As Integer = 4
  61.     Const SWP_NOACTIVATE As Integer = &H10
  62.     Const SWP_DRAWFRAME As Integer = &H20
  63.     Const SWP_SHOWWINDOW As Integer = &H40
  64.     Dim abd As APPBARDATA
  65.  
  66.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'On load, this works, but only with fixed border.
  67.         'If fixed border is removes, it breaks the allignment. 
  68.         Me.TopMost = True
  69.         '  Me.Size = New Size(150, 350)
  70.         Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedToolWindow
  71.  
  72.         abd.hwnd = Me.Handle.ToInt32
  73.         abd.cbSize = Len(abd)
  74.         With abd
  75.             .uEdge = ABE_RIGHT
  76.             .rc.Top = Screen.PrimaryScreen.WorkingArea.Top  '0
  77.             .rc.Left = Screen.PrimaryScreen.WorkingArea.Width - 100 ' width of our appbar
  78.             .rc.Right = Screen.PrimaryScreen.WorkingArea.Width  ' right
  79.             .rc.Bottom = Screen.PrimaryScreen.WorkingArea.Height ' bottom
  80.             SHAppBarMessage(ABM_NEW, abd)
  81.             SetWindowPos(abd.hwnd, HWND_TOP, .rc.Left, .rc.Top, .rc.Right - .rc.Left, .rc.Bottom, SWP_SHOWWINDOW Or SWP_NOACTIVATE)
  82.             SHAppBarMessage(ABM_SETPOS, abd)
  83.         End With
  84.     End Sub
  85.     Private Sub Button1_Top(sender As Object, e As EventArgs) Handles SendTopBtn.Click
  86.         'This button should remove the current bar from the right and reinstate it on Top.
  87.         'Once it moves the bar to the top, the buttons can not be seen, I am guessing because they are outside the screen bounds. 
  88.         SHAppBarMessage(ABM_REMOVE, abd) 'Removes the bar
  89.         Me.TopMost = True
  90.         '   Me.Size = New Size(150, 350)
  91.         Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
  92.  
  93.         abd.hwnd = Me.Handle.ToInt32
  94.         abd.cbSize = Len(abd)
  95.         With abd
  96.             .uEdge = ABE_RIGHT
  97.             .rc.Top = Screen.PrimaryScreen.WorkingArea.Top - (Screen.PrimaryScreen.WorkingArea.Height / 1.04) 'Top
  98.             .rc.Left = Screen.PrimaryScreen.WorkingArea.Width - Screen.PrimaryScreen.WorkingArea.Width ' Width of our AppBar
  99.             .rc.Right = Screen.PrimaryScreen.WorkingArea.Width  ' right
  100.             .rc.Bottom = Screen.PrimaryScreen.WorkingArea.Height ' bottom
  101.             SHAppBarMessage(ABM_NEW, abd)
  102.             SetWindowPos(abd.hwnd, HWND_TOP, .rc.Left, .rc.Top, .rc.Right - .rc.Left, .rc.Bottom, SWP_SHOWWINDOW Or SWP_NOACTIVATE)
  103.             SHAppBarMessage(ABM_SETPOS, abd)
  104.         End With
  105.     End Sub
  106.  
  107.     Private Sub Button2_Right(sender As Object, e As EventArgs) Handles SendRightBtn.Click
  108.         'This button should remove the current bar from top and put it on the right side.
  109.         'Once it moves the bar to the right, the buttons can be seen again.
  110.         SHAppBarMessage(ABM_REMOVE, abd) 'Removes the bar
  111.         Me.TopMost = True
  112.         '    Me.Size = New Size(150, 350)
  113.         Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
  114.  
  115.         abd.hwnd = Me.Handle.ToInt32
  116.         abd.cbSize = Len(abd)
  117.         With abd
  118.             .uEdge = ABE_RIGHT
  119.             .rc.Top = Screen.PrimaryScreen.WorkingArea.Top  '0
  120.             .rc.Left = Screen.PrimaryScreen.WorkingArea.Width - 100 ' width of our appbar
  121.             .rc.Right = Screen.PrimaryScreen.WorkingArea.Width  ' right
  122.             .rc.Bottom = Screen.PrimaryScreen.WorkingArea.Height ' bottom
  123.             SHAppBarMessage(ABM_NEW, abd)
  124.             SetWindowPos(abd.hwnd, HWND_TOP, .rc.Left, .rc.Top, .rc.Right - .rc.Left, .rc.Bottom, SWP_SHOWWINDOW Or SWP_NOACTIVATE)
  125.             SHAppBarMessage(ABM_SETPOS, abd)
  126.         End With
  127.     End Sub
  128.     Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
  129.         SHAppBarMessage(ABM_REMOVE, abd) 'Closes the appbar and normalizes other windows positions. 
  130.     End Sub
Sep 18 '14 #1

✓ answered by IronRazer

Hi,
There where a few things that needed to be changed around so, i pretty much rewrote all of the code for this. You may want to put some of this in a separate class such as the API functions, structures, constants, and maybe the SetAppBarPosition sub that i created.

When you reset the AppBar position you don`t need to unregister it and reregister it. All you have to to is Register it when your application is loading and then from there you only need to call the SetAppBarPosition to reposition it to the Top, Left, Right, or Bottom.

However, you may want to look into using the WndProc overrides sub to receive appbar notification messages that will let you know it needs to be repositioned. For instance, say the appbar is on the right and the taskbar is on the bottom. If the taskbar is set to AutoHide it will move up and down when the mouse enters or leaves it. Those messages will be sent to the WndProc overrides sub to let you know your AppBar`s height should be reset. I put a link in the code you can read about this.

Anyways, you don`t set the FormBorderStyle to None. The trick is setting the correct window styles in the CreateParams. I believe this code will do what you want. 8)

Expand|Select|Wrap|Line Numbers
  1. Imports System.Runtime.InteropServices
  2.  
  3. Public Class Form1
  4.     Private Const ABM_NEW As Integer = &H0
  5.     Private Const ABM_REMOVE As Integer = &H1
  6.     Private Const ABM_QUERYPOS As Integer = &H2
  7.     Private Const ABM_SETPOS As Integer = &H3
  8.     Private Const ABM_GETSTATE As Integer = &H4
  9.     Private Const ABM_GETTASKBARPOS As Integer = &H5
  10.     Private Const ABM_ACTIVATE As Integer = &H6
  11.     Private Const ABM_GETAUTOHIDEBAR As Integer = &H7
  12.     Private Const ABM_SETAUTOHIDEBAR As Integer = &H8
  13.     Private Const ABM_WINDOWPOSCHANGED As Integer = &H9
  14.  
  15.     Private Const ABS_AUTOHIDE As Integer = &H1
  16.     Private Const ABS_ALWAYSONTOP As Integer = &H2
  17.  
  18.     'This is the AppBar message constant to identify the appbar messages when they are recieved in the
  19.     'Overrides WndProc sub. Used to recieve messages from the AppBar. Read the msdn document at link below.
  20.     'http://msdn.microsoft.com/en-us/library/windows/desktop/cc144177%28v=vs.85%29.aspx
  21.     Private Const APPBAR_CALLBACKMESSAGE As Integer = &H401
  22.  
  23.     Private abd As APPBARDATA
  24.  
  25.     <DllImport("shell32.dll", EntryPoint:="SHAppBarMessage", CallingConvention:=CallingConvention.StdCall)> _
  26.     Private Shared Function SHAppBarMessage(ByVal dwMessage As UInteger, ByRef pData As APPBARDATA) As UInteger
  27.     End Function
  28.  
  29.     <DllImport("user32.dll", EntryPoint:="MoveWindow")> _
  30.     Public Shared Function MoveWindow(ByVal hWnd As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, <MarshalAs(UnmanagedType.Bool)> ByVal bRepaint As Boolean) As <MarshalAs(UnmanagedType.Bool)> Boolean
  31.     End Function
  32.  
  33.     <StructLayout(LayoutKind.Sequential)> _
  34.     Private Structure APPBARDATA
  35.         Dim cbSize As Integer
  36.         Dim hwnd As IntPtr
  37.         Dim uCallbackMessage As Integer
  38.         Dim uEdge As Integer
  39.         Dim rc As RECT
  40.         Dim lParam As Integer
  41.     End Structure
  42.  
  43.     <StructLayout(LayoutKind.Sequential)> _
  44.     Private Structure RECT
  45.         Dim Left As Integer
  46.         Dim Top As Integer
  47.         Dim Right As Integer
  48.         Dim Bottom As Integer
  49.     End Structure
  50.  
  51.     Private Enum AppBarrPos As Integer
  52.         ABE_LEFT = &H0
  53.         ABE_TOP = &H1
  54.         ABE_RIGHT = &H2
  55.         ABE_BOTTOM = &H3
  56.     End Enum
  57.  
  58.     Protected Overrides ReadOnly Property CreateParams() As CreateParams
  59.         Get
  60.             Dim cp As CreateParams = MyBase.CreateParams
  61.             cp.Style = cp.Style And -12582913
  62.             cp.Style = cp.Style And -8388609
  63.             Return cp
  64.         End Get
  65.     End Property
  66.  
  67.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'On load, this works, but only with fixed border.
  68.         Me.Visible = False
  69.         Me.TopMost = True
  70.         Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedToolWindow
  71.         RegisterNewAppBar() 'Register the AppBar
  72.         SetAppBarPosition(AppBarrPos.ABE_RIGHT) 'Set the edge to start on
  73.     End Sub
  74.  
  75.     Private Sub RegisterNewAppBar()
  76.         abd = New APPBARDATA
  77.         abd.cbSize = Marshal.SizeOf(GetType(APPBARDATA))
  78.         abd.hwnd = Me.Handle
  79.         abd.uCallbackMessage = APPBAR_CALLBACKMESSAGE
  80.         SHAppBarMessage(ABM_NEW, abd)
  81.     End Sub
  82.  
  83.     Private Sub SetAppBarPosition(ByVal ab_Position As AppBarrPos)
  84.         With abd
  85.             .uEdge = ab_Position 'set the left, right, top, or bottom edge for the appbar
  86.  
  87.             'set the desired rectangle for the appbar
  88.             If ab_Position = AppBarrPos.ABE_RIGHT Then
  89.                 .rc.Top = Screen.PrimaryScreen.Bounds.Top
  90.                 .rc.Left = Screen.PrimaryScreen.Bounds.Right - 100
  91.                 .rc.Right = Screen.PrimaryScreen.Bounds.Right
  92.                 .rc.Bottom = Screen.PrimaryScreen.Bounds.Height
  93.             ElseIf ab_Position = AppBarrPos.ABE_TOP Then
  94.                 .rc.Top = Screen.PrimaryScreen.Bounds.Top
  95.                 .rc.Left = Screen.PrimaryScreen.Bounds.Left
  96.                 .rc.Right = Screen.PrimaryScreen.Bounds.Right
  97.                 .rc.Bottom = Screen.PrimaryScreen.Bounds.Top + 100
  98.             ElseIf ab_Position = AppBarrPos.ABE_LEFT Then
  99.                 .rc.Top = Screen.PrimaryScreen.Bounds.Top
  100.                 .rc.Left = Screen.PrimaryScreen.Bounds.Left
  101.                 .rc.Right = Screen.PrimaryScreen.Bounds.Right + 100
  102.                 .rc.Bottom = Screen.PrimaryScreen.Bounds.Height
  103.             ElseIf ab_Position = AppBarrPos.ABE_BOTTOM Then
  104.                 .rc.Top = Screen.PrimaryScreen.Bounds.Bottom - 100
  105.                 .rc.Left = Screen.PrimaryScreen.Bounds.Left
  106.                 .rc.Right = Screen.PrimaryScreen.Bounds.Right
  107.                 .rc.Bottom = Screen.PrimaryScreen.Bounds.Bottom
  108.             End If
  109.  
  110.             'Request the position and size. When returned the rect is adjusted to not interfere with taskbar and other appbars
  111.             SHAppBarMessage(ABM_QUERYPOS, abd)
  112.  
  113.             'resets the size of the window if the rect`s position was adjusted by the ABM_QUERYPOS call
  114.             If ab_Position = AppBarrPos.ABE_RIGHT Then
  115.                 .rc.Left = .rc.Right - 100
  116.             ElseIf ab_Position = AppBarrPos.ABE_TOP Then
  117.                 .rc.Bottom = .rc.Top + 100
  118.             ElseIf ab_Position = AppBarrPos.ABE_LEFT Then
  119.                 .rc.Right = .rc.Left + 100
  120.             ElseIf ab_Position = AppBarrPos.ABE_BOTTOM Then
  121.                 .rc.Top = .rc.Bottom - 100
  122.             End If
  123.  
  124.             SHAppBarMessage(ABM_SETPOS, abd)
  125.             MoveWindow(.hwnd, .rc.Left, .rc.Top, .rc.Right - .rc.Left, .rc.Bottom - .rc.Top, True)
  126.             If Not Me.Visible Then Me.Visible = True
  127.         End With
  128.     End Sub
  129.  
  130.     Private Sub Button_Top_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Top.Click
  131.         SetAppBarPosition(AppBarrPos.ABE_TOP)
  132.     End Sub
  133.  
  134.     Private Sub Button_Bottom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Bottom.Click
  135.         SetAppBarPosition(AppBarrPos.ABE_BOTTOM)
  136.     End Sub
  137.  
  138.     Private Sub Button_Left_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Left.Click
  139.         SetAppBarPosition(AppBarrPos.ABE_LEFT)
  140.     End Sub
  141.  
  142.     Private Sub Button2_Right_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button_Right.Click
  143.         SetAppBarPosition(AppBarrPos.ABE_RIGHT)
  144.     End Sub
  145.  
  146.     Private Sub Button_Close_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Close.Click
  147.         Me.Close()
  148.     End Sub
  149.  
  150.     Protected Overrides Sub OnFormClosing(ByVal e As System.Windows.Forms.FormClosingEventArgs)
  151.         SHAppBarMessage(ABM_REMOVE, abd)
  152.         MyBase.OnFormClosing(e)
  153.     End Sub
  154. End Class

Share this Question
Share on Google+
2 Replies


IronRazer
P: 82
Hi,
There where a few things that needed to be changed around so, i pretty much rewrote all of the code for this. You may want to put some of this in a separate class such as the API functions, structures, constants, and maybe the SetAppBarPosition sub that i created.

When you reset the AppBar position you don`t need to unregister it and reregister it. All you have to to is Register it when your application is loading and then from there you only need to call the SetAppBarPosition to reposition it to the Top, Left, Right, or Bottom.

However, you may want to look into using the WndProc overrides sub to receive appbar notification messages that will let you know it needs to be repositioned. For instance, say the appbar is on the right and the taskbar is on the bottom. If the taskbar is set to AutoHide it will move up and down when the mouse enters or leaves it. Those messages will be sent to the WndProc overrides sub to let you know your AppBar`s height should be reset. I put a link in the code you can read about this.

Anyways, you don`t set the FormBorderStyle to None. The trick is setting the correct window styles in the CreateParams. I believe this code will do what you want. 8)

Expand|Select|Wrap|Line Numbers
  1. Imports System.Runtime.InteropServices
  2.  
  3. Public Class Form1
  4.     Private Const ABM_NEW As Integer = &H0
  5.     Private Const ABM_REMOVE As Integer = &H1
  6.     Private Const ABM_QUERYPOS As Integer = &H2
  7.     Private Const ABM_SETPOS As Integer = &H3
  8.     Private Const ABM_GETSTATE As Integer = &H4
  9.     Private Const ABM_GETTASKBARPOS As Integer = &H5
  10.     Private Const ABM_ACTIVATE As Integer = &H6
  11.     Private Const ABM_GETAUTOHIDEBAR As Integer = &H7
  12.     Private Const ABM_SETAUTOHIDEBAR As Integer = &H8
  13.     Private Const ABM_WINDOWPOSCHANGED As Integer = &H9
  14.  
  15.     Private Const ABS_AUTOHIDE As Integer = &H1
  16.     Private Const ABS_ALWAYSONTOP As Integer = &H2
  17.  
  18.     'This is the AppBar message constant to identify the appbar messages when they are recieved in the
  19.     'Overrides WndProc sub. Used to recieve messages from the AppBar. Read the msdn document at link below.
  20.     'http://msdn.microsoft.com/en-us/library/windows/desktop/cc144177%28v=vs.85%29.aspx
  21.     Private Const APPBAR_CALLBACKMESSAGE As Integer = &H401
  22.  
  23.     Private abd As APPBARDATA
  24.  
  25.     <DllImport("shell32.dll", EntryPoint:="SHAppBarMessage", CallingConvention:=CallingConvention.StdCall)> _
  26.     Private Shared Function SHAppBarMessage(ByVal dwMessage As UInteger, ByRef pData As APPBARDATA) As UInteger
  27.     End Function
  28.  
  29.     <DllImport("user32.dll", EntryPoint:="MoveWindow")> _
  30.     Public Shared Function MoveWindow(ByVal hWnd As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, <MarshalAs(UnmanagedType.Bool)> ByVal bRepaint As Boolean) As <MarshalAs(UnmanagedType.Bool)> Boolean
  31.     End Function
  32.  
  33.     <StructLayout(LayoutKind.Sequential)> _
  34.     Private Structure APPBARDATA
  35.         Dim cbSize As Integer
  36.         Dim hwnd As IntPtr
  37.         Dim uCallbackMessage As Integer
  38.         Dim uEdge As Integer
  39.         Dim rc As RECT
  40.         Dim lParam As Integer
  41.     End Structure
  42.  
  43.     <StructLayout(LayoutKind.Sequential)> _
  44.     Private Structure RECT
  45.         Dim Left As Integer
  46.         Dim Top As Integer
  47.         Dim Right As Integer
  48.         Dim Bottom As Integer
  49.     End Structure
  50.  
  51.     Private Enum AppBarrPos As Integer
  52.         ABE_LEFT = &H0
  53.         ABE_TOP = &H1
  54.         ABE_RIGHT = &H2
  55.         ABE_BOTTOM = &H3
  56.     End Enum
  57.  
  58.     Protected Overrides ReadOnly Property CreateParams() As CreateParams
  59.         Get
  60.             Dim cp As CreateParams = MyBase.CreateParams
  61.             cp.Style = cp.Style And -12582913
  62.             cp.Style = cp.Style And -8388609
  63.             Return cp
  64.         End Get
  65.     End Property
  66.  
  67.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'On load, this works, but only with fixed border.
  68.         Me.Visible = False
  69.         Me.TopMost = True
  70.         Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedToolWindow
  71.         RegisterNewAppBar() 'Register the AppBar
  72.         SetAppBarPosition(AppBarrPos.ABE_RIGHT) 'Set the edge to start on
  73.     End Sub
  74.  
  75.     Private Sub RegisterNewAppBar()
  76.         abd = New APPBARDATA
  77.         abd.cbSize = Marshal.SizeOf(GetType(APPBARDATA))
  78.         abd.hwnd = Me.Handle
  79.         abd.uCallbackMessage = APPBAR_CALLBACKMESSAGE
  80.         SHAppBarMessage(ABM_NEW, abd)
  81.     End Sub
  82.  
  83.     Private Sub SetAppBarPosition(ByVal ab_Position As AppBarrPos)
  84.         With abd
  85.             .uEdge = ab_Position 'set the left, right, top, or bottom edge for the appbar
  86.  
  87.             'set the desired rectangle for the appbar
  88.             If ab_Position = AppBarrPos.ABE_RIGHT Then
  89.                 .rc.Top = Screen.PrimaryScreen.Bounds.Top
  90.                 .rc.Left = Screen.PrimaryScreen.Bounds.Right - 100
  91.                 .rc.Right = Screen.PrimaryScreen.Bounds.Right
  92.                 .rc.Bottom = Screen.PrimaryScreen.Bounds.Height
  93.             ElseIf ab_Position = AppBarrPos.ABE_TOP Then
  94.                 .rc.Top = Screen.PrimaryScreen.Bounds.Top
  95.                 .rc.Left = Screen.PrimaryScreen.Bounds.Left
  96.                 .rc.Right = Screen.PrimaryScreen.Bounds.Right
  97.                 .rc.Bottom = Screen.PrimaryScreen.Bounds.Top + 100
  98.             ElseIf ab_Position = AppBarrPos.ABE_LEFT Then
  99.                 .rc.Top = Screen.PrimaryScreen.Bounds.Top
  100.                 .rc.Left = Screen.PrimaryScreen.Bounds.Left
  101.                 .rc.Right = Screen.PrimaryScreen.Bounds.Right + 100
  102.                 .rc.Bottom = Screen.PrimaryScreen.Bounds.Height
  103.             ElseIf ab_Position = AppBarrPos.ABE_BOTTOM Then
  104.                 .rc.Top = Screen.PrimaryScreen.Bounds.Bottom - 100
  105.                 .rc.Left = Screen.PrimaryScreen.Bounds.Left
  106.                 .rc.Right = Screen.PrimaryScreen.Bounds.Right
  107.                 .rc.Bottom = Screen.PrimaryScreen.Bounds.Bottom
  108.             End If
  109.  
  110.             'Request the position and size. When returned the rect is adjusted to not interfere with taskbar and other appbars
  111.             SHAppBarMessage(ABM_QUERYPOS, abd)
  112.  
  113.             'resets the size of the window if the rect`s position was adjusted by the ABM_QUERYPOS call
  114.             If ab_Position = AppBarrPos.ABE_RIGHT Then
  115.                 .rc.Left = .rc.Right - 100
  116.             ElseIf ab_Position = AppBarrPos.ABE_TOP Then
  117.                 .rc.Bottom = .rc.Top + 100
  118.             ElseIf ab_Position = AppBarrPos.ABE_LEFT Then
  119.                 .rc.Right = .rc.Left + 100
  120.             ElseIf ab_Position = AppBarrPos.ABE_BOTTOM Then
  121.                 .rc.Top = .rc.Bottom - 100
  122.             End If
  123.  
  124.             SHAppBarMessage(ABM_SETPOS, abd)
  125.             MoveWindow(.hwnd, .rc.Left, .rc.Top, .rc.Right - .rc.Left, .rc.Bottom - .rc.Top, True)
  126.             If Not Me.Visible Then Me.Visible = True
  127.         End With
  128.     End Sub
  129.  
  130.     Private Sub Button_Top_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Top.Click
  131.         SetAppBarPosition(AppBarrPos.ABE_TOP)
  132.     End Sub
  133.  
  134.     Private Sub Button_Bottom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Bottom.Click
  135.         SetAppBarPosition(AppBarrPos.ABE_BOTTOM)
  136.     End Sub
  137.  
  138.     Private Sub Button_Left_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Left.Click
  139.         SetAppBarPosition(AppBarrPos.ABE_LEFT)
  140.     End Sub
  141.  
  142.     Private Sub Button2_Right_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button_Right.Click
  143.         SetAppBarPosition(AppBarrPos.ABE_RIGHT)
  144.     End Sub
  145.  
  146.     Private Sub Button_Close_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Close.Click
  147.         Me.Close()
  148.     End Sub
  149.  
  150.     Protected Overrides Sub OnFormClosing(ByVal e As System.Windows.Forms.FormClosingEventArgs)
  151.         SHAppBarMessage(ABM_REMOVE, abd)
  152.         MyBase.OnFormClosing(e)
  153.     End Sub
  154. End Class
Sep 26 '14 #2

P: 2
You've given me some excellent content to play with there, and thank you for fixing the code Iron. I am now at the stage of dragging the dock to x / y positions to change positions that way too. :)

As always, very helpful. Thanks again!
Sep 27 '14 #3

Post your reply

Sign in to post your reply or Sign up for a free account.