thebunnyrules
thebunnyrules

Reputation: 1670

Programatically get Windows Taskbar Info: AutoHidden State, Taskbar Coordinates, which Screen Edge it's on and Taskbar Thickness

It took me a while to find this answer, so I thought I'd share it here as Q&A.

I wanted a way for my Visual Basic program program to retrieve the following info:

  1. The location of Taskbar's top, bottom, left and right edges. Bottom and Top represents the y values of any point on those edges and top and left represents x values of any points on those edges.
  2. The screen edge on which it resides: bottom, top, left or right screen edge
  3. Autohidden State of Windows Taskbar
  4. Thickness of Taskbar (remember that thickness will vary based on DPI and screen edge position where left and right edges may result in thicker taskbars than the top and bottom ones).

Upvotes: 1

Views: 434

Answers (1)

thebunnyrules
thebunnyrules

Reputation: 1670

This code is written by me but based on the information I gleamed from this post on CodeGuru. I would like to thank Visual Vincent whose suggested corrections in the comment section made it possible to run this code in x64.

Prep: Shared constants, variables and functions:

Const ABS_AUTOHIDE As Int32 = 1
Const ABS_ONTOP As Int32 = 2
Const ABM_NEW As Int32 = 0
Const ABM_REMOVE As Int32 = 1
Const ABM_QUERYPOS As Int32 = 2
Const ABM_SETPOS As Int32 = 3
Const ABM_GETSTATE As Int32 = 4
Const ABM_GETTASKBARPOS As Int32 = 5
Const ABM_ACTIVATE As Int32 = 6
Const ABM_GETAUTOHIDEBAR As Int32 = 7
Const ABM_SETAUTOHIDEBAR As Int32 = 8
Const ABM_WINDOWPOSCHANGED As Int32 = 9

Const TB_POS_BOTTOM As Integer = 1
Const TB_POS_TOP As Integer = 2
Const TB_POS_LEFT As Integer = 3
Const TB_POS_RIGHT As Integer = 4

Private Declare Function apiSHAppBarMessage Lib "shell32" Alias "SHAppBarMessage" (ByVal dwMessage As UInt32, ByRef pData As APPBARDATA) As UIntPtr

Private Structure RECT
    Public rLeft, rTop, rRight, rBottom As Int32
End Structure

Private Structure APPBARDATA
    Public cbSize As UInt32, hwnd As IntPtr, uCallbackMessage, uEdge As UInt32, rc As RECT, lParam As IntPtr
End Structure

Dim ABD As New APPBARDATA
Dim Autohide_State As Int32
Dim taskbar_left, taskbar_right, taskbar_top, taskbar_bottom, taskbar_edge, taskbar_thickness As Integer    

Part 1 & 2: The Sub below can be used to find the location of the taskbar's top, bottom, left and right edges AND the screen edge on which it resides.

    Private Sub GetTaskBarEdge_and_Coordinates()
    apiSHAppBarMessage(ABM_GETTASKBARPOS, ABD)
    taskbar_left = ABD.rc.rLeft
    taskbar_right = ABD.rc.rRight
    taskbar_top = ABD.rc.rTop
    taskbar_bottom = ABD.rc.rBottom

    'Figure out if it's located on the buttom, top, left or right edge of screen 
    If (taskbar_left < 5) And (taskbar_top > 5) Then
        taskbar_edge = TB_POS_BOTTOM
    ElseIf (taskbar_left < 5) And (taskbar_top < 5) And (taskbar_right > taskbar_bottom) Then
        taskbar_edge = TB_POS_TOP
    ElseIf (taskbar_left < 5) And (taskbar_top < 5) And (taskbar_right < taskbar_bottom) Then
        taskbar_edge = TB_POS_LEFT
    ElseIf (taskbar_left > 5) And (taskbar_top < 5) Then
        taskbar_edge = TB_POS_RIGHT
    Else
        MsgBox("Something went wrong while I was trying to find the taskbar edge. Please contact the develloper. Defaulting Edge to bottom.")
        taskbar_edge = TB_POS_BOTTOM
    End If
End Sub

Part 3: The Function Below will get you a integer value for the Autohidden state.

0 Means AH is off, Always on top is off.  
1 means AH is on and Always on Top is off. 
2 means AH is off and AoT is on. 
3 means AH and AoT are on. 

Note the constants I setup in part 1: ABS_AUTOHIDE = 1 , ABS_ONTOP = 2.

Private Function GetTaskBarAHState() As Integer
    Return CInt(apiSHAppBarMessage(ABM_GETSTATE, Nothing))
End Function

Part 4: Taskbar thickness can be calculated from taskbar_bottom - taskbar_top OR taskbar_right - taskbar_left (depending on which edge the taskbar is).

Private Sub GetTaskBar_Thickness()

    GetTaskBarEdge_and_Coordinates()

    Select Case taskbar_edge
        Case TB_POS_BOTTOM
            taskbar_thickness = Math.Abs(taskbar_bottom - taskbar_top)
        Case TB_POS_TOP
            taskbar_thickness = Math.Abs(taskbar_bottom - taskbar_top)
        Case Case TB_POS_LEFT
            taskbar_thickness = Math.Abs(taskbar_right - taskbar_left)
        Case TB_POS_RIGHT
            taskbar_thickness = Math.Abs(taskbar_right - taskbar_left)
        Case Else
            MsgBox("Something went wrong while I tried to find Taskbar thickness. Please contact the develloper. Default to taskbar thickness of 39 [4]")
            taskbar_thickness = 39
    End Select

End Sub     

Upvotes: 1

Related Questions