Maybe you mean CreateWindowEx(WS_EX_NOACTIVATE, ...)
?
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.
Windows API - Win32
C++
-
David Lowndes 4,711 Reputation points
2021-10-01T10:33:07.51+00:00 Why do you want it to lose focus?
Do you want it to move to another window in your application, or some other application? -
C C 1 Reputation point
2021-10-01T10:56:05.163+00:00 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.
-
David Lowndes 4,711 Reputation points
2021-10-01T11:15:41.433+00:00 Can you explain your scenario; how is this window gaining focus when you presumably don't want it to?
-
C C 1 Reputation point
2021-10-01T11:29:52.483+00:00 I click on the window to give it focus. I then send it to the back of the Z order. It retains the focus while under other windows.
-
RLWA32 40,286 Reputation points
2021-10-01T11:31:30.603+00:00 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?
-
C C 1 Reputation point
2021-10-01T11:36:27.407+00:00 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.
-
David Lowndes 4,711 Reputation points
2021-10-01T11:37:26+00:00 It sounds like you're looking for this sort of thing, where you handle the WM_MOUSEACTIVATE message and return MA_NOACTIVATE.
-
RLWA32 40,286 Reputation points
2021-10-01T11:46:27.217+00:00 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
-
C C 1 Reputation point
2021-10-01T11:50:16.717+00:00 It's not like love at all. I don't want to steal anything. Instead, I want Windows itself to to give focus to any window it loves and favors.
Will this work? ShowWindow( [hwnd], SW_HIDE )
-
RLWA32 40,286 Reputation points
2021-10-01T11:52:11.38+00:00 Try it and let us know how it works out. Of course, if I was working on a document and suddenly my application disappeared from the screen I might not be very happy.
And will you ever unhide the window?
-
David Lowndes 4,711 Reputation points
2021-10-01T12:42:15.907+00:00 If you want the same behaviour as minimize, why not use ShowWindow with SW_MINIMIZE ?
-
C C 1 Reputation point
2021-10-01T14:34:32.283+00:00 Only the focus behaviour of minimise is desired. Minimise is not desired.
-
RLWA32 40,286 Reputation points
2021-10-01T16:19:20.873+00:00 Can you explain why you want to programmatically switch away from the foreground window in which the user is working to some other window selected by the system?
-
C C 1 Reputation point
2021-10-01T21:27:37.28+00:00 The switch would be initiated by the user through an intermediary program. In this case, the user would not be very happy not being able to accomplish a full switch. In the worst case, they would be a bit confused.
Focus change via win32 directly looks to be impossible. Indirectly, it is possible, but not worth the effort or added code for a marginal improvement in usability. So, I'll let the user deal with the slight confusion.
-
RLWA32 40,286 Reputation points
2021-10-01T22:09:48.157+00:00 If bringing a different window to the foreground is initiated by the user then I don't understand the purpose of these machinations. The user could simply switch to any other window displayed on the desktop without them.
-
C C 1 Reputation point
2021-10-01T22:47:14.713+00:00 The question is about switching away from a program and not about switch to a program.
-
RLWA32 40,286 Reputation points
2021-10-01T22:58:20.76+00:00 Thats a distinction without a difference. You don't get one without the other.
-
Xiaopo Yang - MSFT 11,496 Reputation points • Microsoft Vendor
2021-10-04T05:17:02.417+00:00 How about sending a alt+tab message?
Sign in to comment
2 answers
Sort by: Newest
-
Drake_Wu 1 Reputation point
2021-10-19T07:22:49.423+00:00 -
SM 416 Reputation points
2021-10-04T14:00:17.95+00:00 How about getting the next window in z-order by using GetWindow() & GW_HWNDNEXT and call SetFocus() on that?
-SM