Click here to Skip to main content
13,300,331 members (52,066 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hello MFC developers,

I tried to create a child window using:
CreateEx( NULL, NULL, "MyChild", WS_CHILD | WS_VISIBLE | WS_BORDER, 300, 300, 400, 200, hParentWnd, NULL, NULL );

Where the parent HWND hParentWnd has many other child windows already. However, this created a child window hiding behind all the siblings.
Windows Spy++ shows that it is on top (first) of the z-order among the child windows of hParentWnd. I have tried all different WIN32 commands including SetWindowPos(), BringWindowToTop(), SetForegroundWindow(), SetFocus(), SetActiveWindow(), SendMessage(WM_ACTIVATE, 0, 0), etc., but none brings it from behind the siblings.
When I replaced WS_CHILD by WS_OVERLAPPEDWINDOW in the CreateEX() function, the created non-child window has no problem showing as the topmost foreground active window.

So what must I do to get the child window to the top from behind the siblings?

Thanks a million.


========= more info to the question as follows ==========

Here is the code I used:

HWND hButtWnd = CreateWindowEx( WS_EX_STATICEDGE, "BUTTON", "MyButton", WS_CHILD | WS_VISIBLE,	buttRect.left,, 50, 60, m_hWnd, NULL, GetModuleHandle(NULL), NULL );

::EnableWindow(hButtWnd, FALSE);

CWnd* pChildWnd = new CWnd;

pChildWnd->CreateEx( NULL, NULL, "MyChild", WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL, childRect, CWnd::FromHandle(m_hWnd), NULL, NULL );

pChildWnd->SetWindowPos(&wndTop, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);

The first two lines create a new button MyButton among other pre-existing sibling windows. MyButton could be created after MyChild, but it didn't matter, MyChild still appeared hidden behind MyButton and all other siblings.

I could drag on the vertical scroll (I have WS_VSCROLL in the style) but MyChild could not be brought to the foreground. Always remained behind the siblings.

Disabling MyButton only greyed out MyButton, and MyChild still remained behind MyButton.

Windows Spy++ confirmed that the SetWindowPos() command did bring it to the top of the z-order; however, it was still behind all its siblings.


Posted 28-Feb-12 10:25am
Updated 16-Aug-16 17:57pm
André Kraak 28-Feb-12 15:43pm
Edited question:
Added pre tags

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Use the widnows style :

HWND hButtWnd = CreateWindowEx( WS_EX_STATICEDGE, "BUTTON", "MyButton", WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS ,	buttRect.left,, 50, 60, m_hWnd, NULL, GetModuleHandle(NULL), NULL );

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.171207.1 | Last Updated 16 Aug 2016
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100