6 out of 35 rated this helpful Rate this topic

GetWindow function

Retrieves a handle to a window that has the specified relationship (Z-Order or owner) to the specified window.

Syntax

HWND WINAPI GetWindow(
  __in  HWND hWnd,
  __in  UINT uCmd
);

Parameters

hWnd [in]

Type: HWND

A handle to a window. The window handle retrieved is relative to this window, based on the value of the uCmd parameter.

uCmd [in]

Type: UINT

The relationship between the specified window and the window whose handle is to be retrieved. This parameter can be one of the following values.

ValueMeaning
GW_CHILD
5

The retrieved handle identifies the child window at the top of the Z order, if the specified window is a parent window; otherwise, the retrieved handle is NULL. The function examines only child windows of the specified window. It does not examine descendant windows.

GW_ENABLEDPOPUP
6

The retrieved handle identifies the enabled popup window owned by the specified window (the search uses the first such window found using GW_HWNDNEXT); otherwise, if there are no enabled popup windows, the retrieved handle is that of the specified window.

GW_HWNDFIRST
0

The retrieved handle identifies the window of the same type that is highest in the Z order.

If the specified window is a topmost window, the handle identifies a topmost window. If the specified window is a top-level window, the handle identifies a top-level window. If the specified window is a child window, the handle identifies a sibling window.

GW_HWNDLAST
1

The retrieved handle identifies the window of the same type that is lowest in the Z order.

If the specified window is a topmost window, the handle identifies a topmost window. If the specified window is a top-level window, the handle identifies a top-level window. If the specified window is a child window, the handle identifies a sibling window.

GW_HWNDNEXT
2

The retrieved handle identifies the window below the specified window in the Z order.

If the specified window is a topmost window, the handle identifies a topmost window. If the specified window is a top-level window, the handle identifies a top-level window. If the specified window is a child window, the handle identifies a sibling window.

GW_HWNDPREV
3

The retrieved handle identifies the window above the specified window in the Z order.

If the specified window is a topmost window, the handle identifies a topmost window. If the specified window is a top-level window, the handle identifies a top-level window. If the specified window is a child window, the handle identifies a sibling window.

GW_OWNER
4

The retrieved handle identifies the specified window's owner window, if any. For more information, see Owned Windows.

 

Return value

Type: HWND

If the function succeeds, the return value is a window handle. If no window exists with the specified relationship to the specified window, the return value is NULL. To get extended error information, call GetLastError.

Remarks

The EnumChildWindows function is more reliable than calling GetWindow in a loop. An application that calls GetWindow to perform this task risks being caught in an infinite loop or referencing a handle to a window that has been destroyed.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winuser.h (include Windows.h)

Library

User32.lib

DLL

User32.dll

See also

Reference
EnumChildWindows
Conceptual
Windows

 

 

Send comments about this topic to Microsoft

Build date: 9/11/2011

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
C# syntax
[DllImport("user32", CharSet=CharSet.Auto, SetLastError=true, ExactSpelling=true)]
public static extern IntPtr GetWindow(IntPtr hwnd, int wFlag);
vb.net syntax
<DllImport("user32", CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:=True)> _
Public Shared Function GetWindow(ByVal hwnd As IntPtr, ByVal uCmd As Integer) As IntPtr
End Function
Owner window or Parent Window
Beware, “Owner window” and “Parent window” are two different things, if you try to use GW_OWNER in order to retrieve the parent window, you’ll get NULL and you probably think your program has an error, but GetLastError() will return zero so that means you program is correct. In the “See also” section should be a reference to GetParent.