开发者

How can I figure out the taskbar height without using Screen.WorkingArea?

I've got a form that's supposed to position itself at the far right edge of the screen, and stretch in height to fill the whole heigth of the working area.

Nothing too strange about that, and so I wrote a solution using Screen.WorkingArea.Height, which worked fine as long as I ran locally. The snag is that in production the form is run in a Citrix environment, an开发者_开发技巧d it seems to completely ignore the taskbar height. In Citrix Screen.WorkingArea.Height returns the exact same value as Screen.Bounds.Height - thus stretching itself under the taskbar.

My idea is to use Screen.Bounds.Height (as that seems to be returned correctly) and subtract the taskbar height on my own. Only problem is the only examples I can find on how to do this involve Screen.Bounds.Height - Screen.WorkingArea.Height.

So how can I access the height of the taskbar directly? (Of course, I'll gladly listen to any other advice on how to get around this problem!)


You'll have to use some native methods too access the properties of the taskbar.

Usage:

TaskbarInfo.Height

Class:

Public NotInheritable Class TaskbarInfo

    <DllImport("user32.dll", SetLastError:=True)> _
    Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function

    <DllImport("shell32.dll", SetLastError:=True)> _
    Public Shared Function SHAppBarMessage(ByVal dwMessage As ABM, <[In]()> ByRef pData As APPBARDATA) As IntPtr
    End Function

    Enum ABM As UInteger
        [New] = &H0
        Remove = &H1
        QueryPos = &H2
        SetPos = &H3
        GetState = &H4
        GetTaskbarPos = &H5
        Activate = &H6
        GetAutoHideBar = &H7
        SetAutoHideBar = &H8
        WindowPosChanged = &H9
        SetState = &HA
    End Enum

    Enum ABE As UInteger
        Left = 0
        Top = 1
        Right = 2
        Bottom = 3
    End Enum

    <StructLayout(LayoutKind.Sequential)> _
    Structure APPBARDATA
        Public cbSize As UInteger
        Public hWnd As IntPtr
        Public uCallbackMessage As UInteger
        Public uEdge As ABE
        Public rc As RECT
        Public lParam As Integer
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
    Structure RECT
        Public left As Integer
        Public top As Integer
        Public right As Integer
        Public bottom As Integer
    End Structure

    Public Shared Function Height() As Integer
        Dim taskbarHandle As IntPtr = FindWindow("Shell_TrayWnd", Nothing)

        Dim data As New APPBARDATA()
        data.cbSize = CUInt(Marshal.SizeOf(GetType(APPBARDATA)))
        data.hWnd = taskbarHandle
        Dim result As IntPtr = SHAppBarMessage(ABM.GetTaskbarPos, data)

        If result = IntPtr.Zero Then
            Throw New InvalidOperationException()
        End If

        Return Rectangle.FromLTRB(data.rc.left, data.rc.top, data.rc.right, data.rc.bottom).Height
    End Function

End Class

Source: http://winsharp93.wordpress.com/2009/06/29/find-out-size-and-position-of-the-taskbar/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜