question

CC-6791 avatar image
0 Votes"
CC-6791 asked drake-wu answered

Yielding focus on a window

I have the HWND to a window. If it has the focus, I want it to lose the focus. I don't mind where the focus goes. Is there anyway I can do that using win32? Thanks.

c++windows-api-general
· 18
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Why do you want it to lose focus?
Do you want it to move to another window in your application, or some other application?

0 Votes 0 ·
CC-6791 avatar image CC-6791 DavidLowndes-6766 ·

Ideally, I want the window to lose focus as if I have pressed the minimise button. Normally Windows decides which window gets the focus next. I have already sent the window to the back of the Z order. For it to retain focus is not useful.

0 Votes 0 ·

Can you explain your scenario; how is this window gaining focus when you presumably don't want it to?

0 Votes 0 ·
Show more comments

When you say "lose the focus" do you mean that the target window is the foreground window and you want a different window to become the foreground window?

0 Votes 0 ·

The window starts with focus. How it obtains that focus isn't important. The focus behaviour I am looking for is as if I pressed the minimise button. Windows then assigns the focus to whichever other window it wants.

0 Votes 0 ·

So the answer is "Yes, I want to make a different window the foreground window". There are various ways to steal the foreground from a window but generally its not a good idea. Take a look at Foreground activation permission is like love: You can’t steal it, it has to be given to you


0 Votes 0 ·
Show more comments

If you want the same behaviour as minimize, why not use ShowWindow with SW_MINIMIZE ?

0 Votes 0 ·
Show more comments
SM-4238 avatar image
0 Votes"
SM-4238 answered

How about getting the next window in z-order by using GetWindow() & GW_HWNDNEXT and call SetFocus() on that?

-SM

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

drake-wu avatar image
0 Votes"
drake-wu answered

Maybe you mean CreateWindowEx(WS_EX_NOACTIVATE, ...) ?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.