1 out of 4 rated this helpful - Rate this topic

SetParent function

Applies to: desktop apps only

Changes the parent window of the specified child window.

Syntax

HWND WINAPI SetParent(
  __in      HWND hWndChild,
  __in_opt  HWND hWndNewParent
);

Parameters

hWndChild [in]

Type: HWND

A handle to the child window.

hWndNewParent [in, optional]

Type: HWND

A handle to the new parent window. If this parameter is NULL, the desktop window becomes the new parent window. If this parameter is HWND_MESSAGE, the child window becomes a message-only window.

Return value

Type:

Type: HWND

If the function succeeds, the return value is a handle to the previous parent window.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

Remarks

An application can use the SetParent function to set the parent window of a pop-up, overlapped, or child window.

If the window identified by the hWndChild parameter is visible, the system performs the appropriate redrawing and repainting.

For compatibility reasons, SetParent does not modify the WS_CHILD or WS_POPUP window styles of the window whose parent is being changed. Therefore, if hWndNewParent is NULL, you should also clear the WS_CHILD bit and set the WS_POPUP style after calling SetParent. Conversely, if hWndNewParent is not NULL and the window was previously a child of the desktop, you should clear the WS_POPUP style and set the WS_CHILD style before calling SetParent.

When you change the parent of a window, you should synchronize the UISTATE of both windows. For more information, see WM_CHANGEUISTATE and WM_UPDATEUISTATE.

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
GetParent
Conceptual
Windows

 

 

Send comments about this topic to Microsoft

Build date: 2/3/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Also need to call SetWindowPos(SWP_FRAMECHANGED) when changing between null and non-null parent
See the reference for SetWindowPos().
C# syntax
[DllImport("User32", CharSet=CharSet.Auto, ExactSpelling=true)]
internal static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndParent);
vb.net syntax
<DllImport("User32", CharSet:=CharSet.Auto, ExactSpelling:=True)> _
Public Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndParent As IntPtr) As IntPtr
End Function
Possible VB9 declaration
Friend Declare Function SetParent Lib "user32.dll" (ByVal hWndChild As Int32, ByVal hWndNewParent As Int32) As Boolean