Question Repositioning the taskbar

johnmcd

New member
Joined
Dec 10, 2010
Messages
2
Programming Experience
3-5
Hi all,

I've been trying for over a week now to figure out how to programatically move the windows taskbar using vb.net. I've scoured forums, googled myself to death and I still haven't had any luck. I found this entry here in the forums - http://www.vbdotnetforums.com/vb-net-general-discussion/811-change-dock-location-taskbar.html - but the question was never answered. I also found this on a C# forum - Moving windows takbar? - C# / C Sharp answers - but I can't get it working in vb.net. In case anyone's wondering, I need to do it for an app that can automatically reposition the taskbar to the preferred position on a Windows tablet when the screen is rotated. I've created a small test app which basically has a single form with 4 radio buttons on it. You select one (I only have 'Bottom' and 'Left' coded right now) and hit the 'Try' button and the taskbar should be re-positioned. Below is my code - I've checked the return status from all the calls and everything returns correctly.

Any help would be greatly appreciated!

Thanx.

John

Imports Microsoft.Win32 'Registry Functions
Imports System.Runtime.InteropServices 'API functions

Public Class Form1
' COnstants
Friend Const TB_TOP As Integer = 1
Friend Const TB_BOTTOM As Integer = 2
Friend Const TB_LEFT As Integer = 3
Friend Const TB_RIGHT As Integer = 4
Friend Const ABM_GETPOS As Long = &H5
Friend Const ABM_SETPOS As Long = &H3
Friend Const ABM_QUERYPOS As Long = &H2
Friend Const ABE_BOTTOM As Integer = 3
Friend Const ABE_LEFT As Integer = 0
Friend Const ABE_RIGHT As Integer = 2
Friend Const ABE_TOP As Integer = 1

' Structures we need to mess with the taskbar
<StructLayout(LayoutKind.Sequential)> Structure RECT
Public left As Integer
Public top As Integer
Public right As Integer
Public bottom As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> Structure APPBARDATA
Public cbSize As Integer
Public hWnd As IntPtr
Public uCallbackMessage As Integer
Public uEdge As Integer
Public rc As RECT
Public lParam As IntPtr
End Structure

Public Enum AppBarNotifications
StateChange = 0
PosChanged = 1
FullScreenApp = 2
WindowArrange = 3
End Enum

Public Enum AppBarStates
AutoHide = 1
AlwaysOnTop = 2
End Enum

'Win32 functions we need
Private Declare Function FindWindow Lib "user32.dll" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As IntPtr

<DllImport("SHELL32", CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function SHAppBarMessage(ByVal dwMessage As Integer, ByRef BarrData As APPBARDATA) As Integer
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto, EntryPoint:="MoveWindow")> _
Private Shared Function MoveWindow( _
ByVal hwnd As IntPtr, _
ByVal x As Integer, _
ByVal y As Integer, _
ByVal nWidth As Integer, _
ByVal nHeight As Integer, _
ByVal bRepaint As Boolean _
) As Integer
End Function

Private Sub btnTry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTry.Click
Dim theScreenBounds As Rectangle
Dim hwndTB As New IntPtr
Dim abd As New APPBARDATA
Dim abdNew As New APPBARDATA
Dim strOut As String
Dim nTbHeight As Integer
Dim bRet As Boolean
Dim nWidth As Integer
Dim nHeight As Integer

' Get a handle to the taskbar
hwndTB = FindWindow("Shell_TrayWnd", vbNullString)
If hwndTB.Equals(IntPtr.Zero) Then
MsgBox("Taskbar windows handle is invalid!")
End If

' Get the current taskbar position
abd.hWnd = hwndTB
abd.cbSize = Marshal.SizeOf(abd)
SHAppBarMessage(ABM_GETPOS, abd)
Select Case abd.uEdge
Case ABE_BOTTOM, ABE_TOP
nTbHeight = System.Math.Abs(abd.rc.top - abd.rc.bottom)
Case ABE_LEFT, ABE_RIGHT
nTbHeight = System.Math.Abs(abd.rc.left - abd.rc.right)
End Select

' Figure out if we're landscape or portrait and change the taskbar accordingly
theScreenBounds = Screen.GetBounds(Screen.PrimaryScreen.Bounds)

If rbBottom.Checked Then
' Query the acceptable size
abd.uEdge = ABE_BOTTOM
abd.rc.bottom = theScreenBounds.Height
abd.rc.left = 0
abd.rc.right = theScreenBounds.Width
abd.rc.top = theScreenBounds.Height - nTbHeight
abd.hWnd = hwndTB
abd.cbSize = Marshal.SizeOf(abd)
SHAppBarMessage(ABM_QUERYPOS, abd)
SHAppBarMessage(ABM_SETPOS, abd)
MoveWindow(hwndTB, abd.rc.left, abd.rc.top, abd.rc.right - abd.rc.left, abd.rc.bottom - abd.rc.top, 1)
ElseIf rbLeft.Checked Then
' Query the acceptable size
abd.uEdge = ABE_LEFT
abd.rc.bottom = theScreenBounds.Height
abd.rc.left = 0
abd.rc.right = theScreenBounds.Width - nTbHeight
abd.rc.top = 0
abd.hWnd = hwndTB
abd.cbSize = Marshal.SizeOf(abd)
SHAppBarMessage(ABM_QUERYPOS, abd)
SHAppBarMessage(ABM_SETPOS, abd)
nWidth = abd.rc.right - abd.rc.left
nHeight = theScreenBounds.Height
bRet = MoveWindow(hwndTB, abd.rc.left, abd.rc.top, nWidth, nHeight, True)
If bRet = False Then
MsgBox("MoveWindow failed!")
Else
MsgBox("MoveWindow suceeded!")
End If
End If



End Sub
End Class
 
Never mind

I just found out from MS support that the taskbar can't be re-positioned programatically - it's considered a 'user' setting and as such they don't want you messing with it.

John
 

Latest posts

Back
Top