Jalaj

January 28, 2007

Getting Window Handles

Filed under: Visual Basic, WinAPI — Jalaj @ 10:56 am

Previously we saw how to get handle to a window using a few commands. Windows have more API functions that facilates getting handle to windows in more complex or different situations. Listed here are a few of them.

GetForegroundWindow function return the handle to the window which is currently active. The handle returned may be NULL in case the active window as lost activation. No parameters are required to be passed to the function.

Declare Function GetForegroundWindow Lib "user32" Alias "GetForegroundWindow" () As Long

WindowFromPoint function returns the handle to window which is visible at a given point on screen. If no window is visible at the point the return is NULL and if the point is contained by a static text the handle of the window that contains it is returned.

Declare Function WindowFromPoint Lib "user32" Alias "WindowFromPoint" ( _
     ByVal xPoint As Long, ByVal yPoint As Long) As Long

ChildWindowFromPoint function returns the handle to the child window that exist at a particular point relative to the parent window given the handle to its parent. If the point lies outside the parent window a NULL is returned. If the point is within the parent window but no child exists at the point then the handle to the parent window is returned. The handle to a child window is returned if it occupies the point, even if it is transparent, invisible or disabled.

Declare Function ChildWindowFromPoint Lib "user32" Alias "ChildWindowFromPoint" ( _
     ByVal hWnd As Long, ByVal xPoint As Long, ByVal yPoint As Long) As Long

ChildWindowFromPointEx is similar to ChildWindowFromPoint except that it takes an additional parameter enabling it to skip either or all of transparent, invisible and disabled child windows. This parameter can take one or more of constants as defined below.

Declare Function ChildWindowFromPointEx Lib "user32" Alias "ChildWindowFromPointEx" ( _
     ByVal hWnd As Long, ByVal pt As POINTAPI, ByVal un As Long) As Long

const CWP_ALL = 0
const CWP_SKIPINVISIBLE = 1
const CWP_SKIPDISABLED = 2
const CWP_SKIPTRANSPARENT = 4

RealChildWindowFromPoint is similar to ChildWindowFromPoint except in handling of transparent windows. If the child window at a point is transparent the handle to window that is visible at the same point, though behind the transparent window, is returned.

Declare Function RealChildWindowFromPoint Lib "user32.dll" (ByVal hwndParent As Long, _
     ByVal ptParentClientCoords As Struct_MembersOf_POINT) As Long

GetParent function returns the handle to the given window’s parent or owner. If the given window is a top level window then the handle to it’s owner is returned. If it’s a top level un-owned window then NULL is returned.

Declare Function GetParent Lib "user32" Alias "GetParent" (ByVal hwnd As Long) As Long

GetAncestor function is similar to GetParent except that it takes an additional parameter by which we can specify whether the handle to immediate parent is required or the root window or the owner window. The additional parameter may be one of the constants as defined below.

Declare Function GetAncestor Lib "user32.dll" (ByVal hwnd As Long, ByVal gaFlags As Long) As Long

Const GA_PARENT = 1
Const GA_ROOT = 2
Const GA_ROOTOWNER = 3

GetNextWindow function returns the handle to the window that exist as next or previous window to the given window in the Z-order. The flag may be one of the constants defined as below.

Declare Function GetNextWindow Lib "user32" Alias "GetWindow" ( _
     ByVal hwnd As Long, ByVal wFlag As Long) As Long

Const GW_HWNDNEXT = 2
Const GW_HWNDPREV = 3

GetShellWindow returns handle to the Shell’s desktop window. If no shell process exists then the NULL is returned. This function is applicable for windows 2000 onwards.

Declare Function GetShellWindow() As Long

GetTopWindow returns the handle to the child window, of a given window, which is on the top in Z-order. If the given window doesn’t have a child window a NULL is returned.

Declare Function GetTopWindow Lib "user32" Alias "GetTopWindow" (ByVal hwnd As Long) As Long

GetWindow returns the handle to a window which bears a relationship, with the given window, as defined by the second parameter, which should be one of the constants as below.

Declare Function GetWindow Lib "user32" Alias "GetWindow" ( _
    ByVal hwnd As Long, ByVal wCmd As Long) As Long

Const GW_HWNDFIRST = 0
Const GW_HWNDLAST = 1
Const GW_HWNDNEXT = 2
Const GW_HWNDPREV = 3
Const GW_OWNER = 4
Const GW_CHILD = 5
Const GW_ENABLEDPOPUP = 6

1 Comment »

  1. [...] among the most read ones. The highest viewed one were those related to Menus, Using Unicode fonts, getting Window handles and working with them, Shutdown API functions, Accessing Clipboard, storing Unicode data in [...]

    Pingback by Year One Digest « Jalaj — December 12, 2007 @ 2:47 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.