Discussion:
How to get parent window topmost when child window activated?
(too old to reply)
JiiPee
2019-05-17 10:27:17 UTC
Permalink
I have MFC frame window. Then on its client view I have child window and
the view it the parent of the child window (child window is a frame
based window). When I click the child windows title it gets activated
but the main frame window will not become topmost. I mean if the program
is under a notepad and then I click the childwindow (child of the view)
the program will not go in front of the notepad .. although the
childiwindow does get activated.

This works but it gives flicker (first main frame activated and then
focus back to child window):

(in child windows OnActivate):

OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)

{

        GetParentFrame()->BringWindowToTop(); // bring the main frame top
        SetFocus(); // set focus back to child window frame


what is the right way to do this? This seems to work but
focus/activation goes first to mainframe which I want to prevent.
R.Wieser
2019-05-17 13:43:16 UTC
Permalink
JiiPee,
This seems to work but focus/activation goes first to mainframe which I
want to prevent.
Maybe freeze the desktop before making the changes (LockWindowUpdate) and
release afterwards ?

Regards,
Rudy Wieser
JiiPee
2019-05-17 16:20:01 UTC
Permalink
Post by R.Wieser
JiiPee,
This seems to work but focus/activation goes first to mainframe which I
want to prevent.
Maybe freeze the desktop before making the changes (LockWindowUpdate) and
release afterwards ?
Regards,
Rudy Wieser
oh, I already tried to freeze with Lock the frame, but did not work. But
was just thinking that if I freeze the desktop its affecting other
programs? It would freeze other programs so not so good?


But is there any way automatically cause a child window to make the
parent window topmost (without putting focus on the parent window)? MDI
type of program surely does that.
R.Wieser
2019-05-17 16:46:52 UTC
Permalink
JiiPee,
But was just thinking that if I freeze the desktop its affecting other
programs? It would freeze other programs so not so good?
It would freeze the /updating/, but gathering those changes untill the
"unfreeze". In your case the in-between time (freezing/unfreezing) seems to
be quite short, and would /most likely/ (not tested) not have any adverse
effect.
oh, I already tried to freeze with Lock the frame, but did not work
The problem is that LockWindowUpdate only works for a single window.

But now I think of it: There is a function which changes the Z-placement of
a window /without/ activating it. Lemme look ....

Found it: try the SetWindowPos (on your child window) function with HWND_TOP
and the SWP_NOMOVE and SWP_NOSIZE flags. The child should get activated and
it and its parent be pulled to the top.

Regards,
Rudy Wieser

P.s.
I'm suggesting API calls 'cause I've got little knowledge of MFC. Sorry. :-)
JiiPee
2019-05-17 16:53:57 UTC
Permalink
Post by R.Wieser
Found it: try the SetWindowPos (on your child window) function with HWND_TOP
and the SWP_NOMOVE and SWP_NOSIZE flags. The child should get activated and
it and its parent be pulled to the top.
ok thanks. I think I tried topMost. ok, I double check that with TOP.
Will try this later...
JiiPee
2019-05-17 18:08:33 UTC
Permalink
Post by R.Wieser
Found it: try the SetWindowPos (on your child window) function with HWND_TOP
and the SWP_NOMOVE and SWP_NOSIZE flags. The child should get activated and
it and its parent be pulled to the top.
This did not help. It does the same as BringWindowToTop()

GetParentFrame()->SetWindowPos(&wndTop,
            0, 0, 0, 0,
            SWP_NOMOVE | SWP_NOSIZE);
JiiPee
2019-05-17 19:07:35 UTC
Permalink
Post by R.Wieser
Found it: try the SetWindowPos (on your child window) function with HWND_TOP
and the SWP_NOMOVE and SWP_NOSIZE flags. The child should get activated and
it and its parent be pulled to the top.
Your suggestion took me to correct path. But one thing is just missing
from your code SWP_NOACTIVATE:


SetWindowPos(&wndTop,
            0, 0, 0, 0,
            SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);


this one does it!

Thanks, good to learn new things
R.Wieser
2019-05-17 19:41:36 UTC
Permalink
JiiPee,
Post by JiiPee
Your suggestion took me to correct path. But one thing is just missing
:-) I had it in my message, but removed it because you wanted your child
window activated. Oh well ..

But you got it to work anyway. Well done.

Regards,
Rudy Wieser
JiiPee
2019-05-17 19:56:55 UTC
Permalink
Post by R.Wieser
JiiPee,
Post by JiiPee
Your suggestion took me to correct path. But one thing is just missing
:-) I had it in my message, but removed it because you wanted your child
window activated. Oh well ..
Yes but the calll if for the top level frame which needs to be called with

SWP_NOACTIVATE.

thanks again :)

I am using GetTopLevelParent() to make sure the whole program goes on top Z order
Post by R.Wieser
But you got it to work anyway. Well done.
Regards,
Rudy Wieser
JiiPee
2019-05-17 16:28:14 UTC
Permalink
Post by R.Wieser
JiiPee,
This seems to work but focus/activation goes first to mainframe which I
want to prevent.
Maybe freeze the desktop before making the changes (LockWindowUpdate) and
release afterwards ?
Regards,
Rudy Wieser
this one does not work

        auto dt = GetDesktopWindow();

dt->LockWindowUpdate();

              GetParentFrame()->BringWindowToTop(); // bring the main
frame top
        SetFocus(); // set focus back to child window frame

        dt->UnlockWindowUpdate();
JiiPee
2019-05-17 16:31:01 UTC
Permalink
Post by R.Wieser
JiiPee,
This seems to work but focus/activation goes first to mainframe which I
want to prevent.
Maybe freeze the desktop before making the changes (LockWindowUpdate) and
release afterwards ?
Regards,
Rudy Wieser
its close to work. Actually this one does the same thing:


OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)

{

        GetParentFrame()->BringWindowToTop(); // bring the main frame top

..

so the setfocus not needed. The end result of this is correct, but just
that it does not activate immediately the childwindow when lbutton down
and the flicker happens because it activates first the parent frame.
Loading...