How to make an MDI child window stay on top of its

2020-04-21 02:53发布

问题:

This question is related to my previous one.

I have an MFC (VC6) MDI Application which has several MDI child windows acting as different views for one document.

Is it possible to set one of those frames to stay on top of the others?
I have tried to call

SetWindowPos(
   &GetParentFrame()->wndTopMost,
   0, 0, 0, 0,
   SWP_NOMOVE | SWP_NOSIZE);

and

ModifyStyleEx(0, WS_EX_TOPMOST);

from the CMDIChildWnd but neither appears to work.

回答1:

In your CMDIChildWnd class (usually CChildFrame), add a static HWND m_hTopWnd. Set it equal to the HWND of the child you want to be always on top.

Handle WM_WINDOWPOSCHANGED in CChildFrame. In the handler, check if the current m_hWnd == m_hTopWnd. If not, call

::SetWindowPos(m_hTopWnd, HWND_TOP, 0, 0, 0, 0, 
    SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);

This way whenever the position of any MDI child window is set, the "always on top" window will be pushed back to the top.

Also handle WM_CLOSE and when the top window is closing, set m_hTopWnd = NULL.

See also: CodeProject article and MSDN knowledgebase



回答2:

Are you sure it's good UI design to keep a child window on top of the others? Shouldn't this become a separate topmost frame? Or a control bar?