Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello MFC developers,

I tried to create a child window using:
C++
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.

Ernest.

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

Here is the code I used:

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

C++
::EnableWindow(hButtWnd, FALSE);

C++
CWnd* pChildWnd = new CWnd;

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

C++
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.

Thanks.

Ernest.
Posted
Updated 5-Feb-21 1:49am
v5
Comments
André Kraak 28-Feb-12 15:43pm    
Edited question:
Added pre tags

Use the widnows style :
C++
WS_CLIPSIBLINGS



C++
HWND hButtWnd = CreateWindowEx( WS_EX_STATICEDGE, "BUTTON", "MyButton", WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS ,	buttRect.left, buttRect.top, 50, 60, m_hWnd, NULL, GetModuleHandle(NULL), NULL );
 
Share this answer
 
v3
Use WS_CLIPSIBLINGS style with the window you want to keep behind and use BringWindowToTop for the top window
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900