[VB.Net] GetWindowPos

I wrote this function a while back, but its often quite useful, so I thought that I would post it back up here.
Its basically the equivalent of the SetWindowPos API, which is used for setting various properties of a window.
This function is slightly edited from the one previously posted, which had an error in the size calculation and this also uses the .net Point and Rectangle structures

    Private Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As IntPtr, ByVal wFlag As Integer) As IntPtr
    Private Declare Function GetWindowPlacement Lib "user32" (ByVal hwnd As IntPtr, ByRef lpwndpl As WINDOWPLACEMENT) As Integer

    Private Structure WINDOWPLACEMENT
        Public Length As Integer
        Public flags As Integer
        Public showCmd As Integer
        Public ptMinPosition As Point
        Public ptMaxPosition As Point
        Public rcNormalPosition As Rectangle
    End Structure

    Public Enum WNDSTATE
        SW_HIDE = 0
        SW_SHOWNORMAL = 1
        SW_NORMAL = 1
        SW_MAXIMIZE = 3
        SW_SHOW = 5
        SW_MINIMIZE = 6
        SW_SHOWNA = 8
        SW_RESTORE = 9
        SW_SHOWDEFAULT = 10
        SW_MAX = 10
    End Enum

    Private Const GW_HWNDNEXT = 2
    Private Const GW_HWNDPREV = 3

    ''' <summary >
    ''' Return values from equivalent to those in the SetWindowPos API
    ''' </summary >
    ''' <param name="hwnd" > In - The handle of the window you wish to get the information from</param >
    ''' <param name="ptrPhwnd" > Out - Returns the handle of the previous window in the Z order</param >
    ''' <param name="ptrNhwnd" > Out - Returns the handle of the next window in the Z order</param >
    ''' <param name="ptPoint" > Out - Returns the location of the specified window on the screen</param >
    ''' <param name="szSize" > Out - Returns the size of the specified window</param >
    ''' <param name="intShowCmd" > Out - Returns the state of the window, returns a WNDSTATE value</param >
    ''' <remarks > If you use the function on a control, it will return the handles for the buttons next in the forms Z order.
    ''' If the handle passed for hwnd is invalid, the function will return 0 for all values</remarks >
    Public Sub GetWindowPos(ByVal hwnd As Integer, ByRef ptrPhwnd As Integer, ByRef ptrNhwnd As Integer, ByRef ptPoint As Point, ByRef szSize As Size, ByRef intShowCmd As WNDSTATE)
        wInf.Length = System.Runtime.InteropServices.Marshal.SizeOf(wInf)
        GetWindowPlacement(hwnd, wInf)
        szSize = New Size(wInf.rcNormalPosition.Right - (wInf.rcNormalPosition.Left * 2), wInf.rcNormalPosition.Bottom - (wInf.rcNormalPosition.Top * 2))
        ptPoint = New Point(wInf.rcNormalPosition.Left, wInf.rcNormalPosition.Top)
        ptrPhwnd = GetNextWindow(hwnd, GW_HWNDPREV)
        ptrNhwnd = GetNextWindow(hwnd, GW_HWNDNEXT)
        intShowCmd = wInf.showCmd
    End Sub


        Dim hwnd As Integer = Process.GetProcessesByName("calc")(0).MainWindowHandle
        Dim ptrPhwnd, ptrNhwnd As Integer
        Dim ptPoint As Point
        Dim szSize As Size
        Dim intShowCmd As WNDSTATE
        GetWindowPos(hwnd, ptrPhwnd, ptrNhwnd, ptPoint, szSize, intShowCmd)
        Debug.WriteLine("Window Handle: " & hwnd)
        Debug.WriteLine("Previous Window Handle: " & ptrPhwnd)
        Debug.WriteLine("Next Window Handle: " & ptrNhwnd)
        Debug.WriteLine("Window Location: (" & ptPoint.X & ", " & ptPoint.Y & ")")
        Debug.WriteLine("Window Size - Width: " & szSize.Width & " Height: " & szSize.Height)
        Debug.WriteLine("Window State: " & intShowCmd.ToString)

Would output:

Window Handle: 4065694
Previous Window Handle: 8194392
Next Window Handle: 263910
Window Location: (2934, 89)
Window Size – Width: 480 Height: 310
Window State: SW_NORMAL

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: