Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get parent window topmost when child window activated?

15 views
Skip to first unread message

JiiPee

unread,
May 17, 2019, 6:27:21 AM5/17/19
to
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

unread,
May 17, 2019, 9:43:27 AM5/17/19
to
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

unread,
May 17, 2019, 12:20:05 PM5/17/19
to
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.

JiiPee

unread,
May 17, 2019, 12:28:18 PM5/17/19
to
On 17/05/2019 14:43, R.Wieser wrote:
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

unread,
May 17, 2019, 12:31:05 PM5/17/19
to
On 17/05/2019 14:43, R.Wieser wrote:
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.

R.Wieser

unread,
May 17, 2019, 12:47:06 PM5/17/19
to
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

unread,
May 17, 2019, 12:54:00 PM5/17/19
to
On 17/05/2019 17:46, R.Wieser wrote:
> 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

unread,
May 17, 2019, 2:08:37 PM5/17/19
to
On 17/05/2019 17:46, R.Wieser wrote:
> 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

unread,
May 17, 2019, 3:07:39 PM5/17/19
to
On 17/05/2019 17:46, R.Wieser wrote:
> 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

unread,
May 17, 2019, 3:41:46 PM5/17/19
to
JiiPee,

> Your suggestion took me to correct path. But one thing is just missing
> from your code SWP_NOACTIVATE:

:-) 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

unread,
May 17, 2019, 3:56:58 PM5/17/19
to
On 17/05/2019 20:41, R.Wieser wrote:
> JiiPee,
>
>> Your suggestion took me to correct path. But one thing is just missing
>> from your code SWP_NOACTIVATE:
> :-) 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
0 new messages