Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
1,979 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.
Can you explain your scenario; how is this window gaining focus when you presumably don't want it to?
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.
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?
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.
It sounds like you're looking for this sort of thing, where you handle the WM_MOUSEACTIVATE message and return MA_NOACTIVATE.
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
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 )
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?
If you want the same behaviour as minimize, why not use ShowWindow with SW_MINIMIZE ?
Only the focus behaviour of minimise is desired. Minimise is not desired.
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?
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.
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.
The question is about switching away from a program and not about switch to a program.
Thats a distinction without a difference. You don't get one without the other.
How about sending a alt+tab message?
Sign in to comment
2 answers
Sort by: Newest
Maybe you mean
CreateWindowEx(WS_EX_NOACTIVATE, ...)
?Sign in to comment
How about getting the next window in z-order by using GetWindow() & GW_HWNDNEXT and call SetFocus() on that?
-SM
Sign in to comment
Activity