2017-06-25 46 views
1

我花了一段時間來找到這個答案,所以我想我會在這裏分享它爲Q & A.編程方式獲得Windows任務欄上的信息:自動隱藏狀態,任務欄座標,屏幕邊緣是上和任務欄厚度

我想要一種方式讓我的Visual Basic程序程序檢索以下信息:

  1. 任務欄的頂部,底部,左側和右側邊緣的位置。底部 和頂部表示這些邊緣上的任意點的y值,頂部表示這些邊緣上的任意點的x值。
  2. 在其上駐留的屏幕邊緣:底部,頂部,左或右屏幕 邊緣
  3. 視窗
  4. 自動隱藏狀態的任務欄
  5. 任務欄(記住,厚度的
  6. 厚度會根據DPI和屏幕邊緣位置左和右邊緣可能會導致比頂部和底部更厚的任務欄)。

回答

1

此代碼是由我寫的,但基於我從this post on CodeGuru閃爍的信息。我要感謝Visual Vincent,它在評論部分提出了更正,使得可以在x64中運行此代碼。

製備:共享的常量,變量和函數:

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  

第1部分& 2:下面的子可用於在任務欄的頂部,底部,左,右邊緣的位置與屏幕邊緣它駐留在哪裏。

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 

第3部分:下面的函數將爲您獲取Autohidden狀態的整數值。

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. 

注常數我安裝在第1部分:ABS_AUTOHIDE = 1,ABS_ONTOP = 2

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

4部分:任務欄厚度可以從taskbar_bottom計算 - taskbar_top OR taskbar_right - taskbar_left(取決於任務欄的邊緣)。

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  
+2

不錯,但'APPBARDATA'結構中的字段不是正確的類型。 cbSize應該是UInt32,hWnd應該是IntPtr,uCallbackMessage,uEdge應該是UInt32,lParam應該是IntPtr。 –

+2

此外,'apiSHAppBarMessage'函數中的'dwMessage'參數應該是'UInt32',並且函數的返回值應該是'UIntPtr'。 –

+0

感謝文森特,我將再次閱讀並應用您所建議的更改。 – thebunnyrules