I am writing an application that needs to bring window of an external app to the foreground, and not necessarily steal focus (there is a setting the user can toggle to steal/not steal focus).
What is the best way to go about this using the win32 API? I have tried SetForeground() but it always steals focus and does not consistenly work.
What is the best way to go about this? Any thoughts?
SetForegroundWindow is supposed to steal focus and there are certain cases where it will fail.
The SetForegroundWindow function puts the thread that created the specified window into the foreground and activates the window. Keyboard input is directed to the window
Try capturing the focus with SetCapture prior to making the call. Also look into different ways of bringing the window to the front: SetForeGroundWindow, SetActiveWindow, even simulating a mouse click can do this.
What is the difference between SetForeGroundWindow, SetActiveWindow, and BringWindowToTop? It appears as if they all do the same thing.
According to MSDN, SetForeGroundWindow will activate the window and direct keyboard focus to it. This attempts to work even when your process is in the background. SetActiveWindow does the same thing as SetForeGroundWindow, but it doesn't do anything if your application isn't the frontmost application. Finally, BringWindowToTop only brings the window to the top, and doesn't change the keyboard focus.
You can try the BringWindowToTop function to not steal focus. I haven't used it, but it seems to be what you're looking for.
Have you tried using SetWindowPos. This is the canonical function for moving, resizing and setting z-order in Windows. There is a SWP_NOACTIVATE flag you can use. Look at http://msdn.microsoft.com/en-us/library/ms633545(VS.85).aspx. I have not tried this on a window belonging to another process, but it is probably worth a try.
SetWindowPos + SWP_NOACTIVATE does the job.
You could use FindWindow to get the HWND of the window, then use the BringWindowToTop function found in the Win32 API.
Related
My app screencaptures another window that runs on a second monitor. Now I'd also like to forward mouse clicks made in my app to that window. I tried using SendMessage in user32.dll for this, but this also makes window focus switch, which causes some issues, like the two windows rapidly fighting for focus. Is there are way to place those mouse events without making the hidden window active and losing focus on the main app?
Is there are way to place those mouse events without making the hidden window active and losing focus on the main app?
No, there is not even a way to forward mouse input to another receiver. Messages are only part of the input processing. The system also does internal bookkeeping and you cannot replicate that.
The only reliable way to inject input is by calling SendInput. Doing so doesn't allow you to specify a receiver. Input goes to whichever thread is determined to be the receiver by the system.
Although, more often than not, this question is asked when the problem that needs to be solved is a different one altogether: How do you automate a UI? The answer to that question is UI Automation.
I need to be able to issue keyboard input to a particular window in the background on a periodic basis without interrupting the user. How could this be done?
Use case:
I have two windows open: one window is waiting for the input (window A), and I'm actively working in another window (window B). While working in window B, periodic commands are issued to window A through the keyboard without interrupting the interactivity going on in window B.
I'm hoping to accomplish this using Python or C#...whichever gets the job done cleanest and fastest (or a nice hybrid thereof).
Check winGuiAuto.py. If you can find the hwnd for the control whose input you want to change, you can send it to it even if it's not the active window.
Even it doesn't solve your problem directly, the source code is a good lesson on using the win32api with Python and should help you anyway.
I'm working on a docking project, in which I need my form to dock itself into a window that doesn't belong to my application. I have no problem detecting the location of the window, however I haven't got a clue on how to intercept the event of movement of the window. Does anyone know how can I intercept the event of movement of a window?
Maybe this post will give you some clues.
An alternative way (but not very nice) would be to have a timer that fires every 500 milisecs and checks each time for window position and compare with the previous one. Then if different adjust your window accordingly.
Listening for window messages requires injecting code in the process whose window you want to dock to. You cannot inject C# code, you can't reliably get the CLR initialized in that process. You'll only have a fighting chance if you use native code. Google EasyHook. Black belt Win32 API skills are required to bring this to a good end.
I am using SetWindowPos() to try and send Windows to back/front of the z-order.
It seems to work for the most part however I am noticing for certain windows it just won't work although the function will return success.
For example Window Task Manager seems to work perfectly fine but the other windows aren't responding properly to the function call.
Any insights into what may be happening? I know the information is sparse however I am not sure what to include.
Pretty much seems to work using SetForegroundWindow.
From what I could discern it seems that using SetWindowPos and setting a window to HWND_TOP will make it top of the z-order following the foreground window.
Meaning that once the foreground window is removed (minimized/closed) it will then be the next one at the TOP.
I too have hod problems with this across processes. This from the MS documentation:
To use SetWindowPos to bring a window to the top, the process that owns the window must have SetForegroundWindow permission.
might be the cause?
I have a fullscreen window, and I want to prevent pop up windows that appear at the right bottom corner of my screen. I set the Topmost property to true, but apparently it does not help. I also tried activating the form and giving it the focus once it got deactivated, but that did not help either. What is a way to ignore such windows while the user is engaged with the fullscreen app? I am .NET programming in C#.
You can't do it, this fails the "what if two programs tried to do this" test:
those popups are just normal windows like yours, they also use the same top-most style you are using.
if there was a way to always be above other topmost windows they would have used it too rendering it useless (because the authors of the other apps are just as concerned about the user missing their "super important" notifications as you are about them interfering with your full screen app).
You can try and play dirty tricks to force your window to the top of the top-most z-order, but those popups are likely to use the exact same tricks, again making this all useless (and as an extra bonus all those dirty tricks can turn your app into a compatibility nightmare).
You can disable these balloon notifications using these steps:
Click Start, Run and type regedit
Navigate to the following subkey:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced
In the right pane, create a DWORD value named EnableBalloonTips
Double-click the new entry, and give it a value of 0.
Quit Registry Editor.
Log off Windows (this is not very cool...), and then log back on for the changes to take effect.
if you need help in how doing this by program, don't hesitate to ask ;)
I don't think that you can block all the popups, windows might not let you do that. But you can try with SetWindowPos function and pass it HWND_TOP parameter. It might work a little better than Topmost = true.
I used a sys tray popup control on my personal project SvnRadar written in WPF.
The control is at the http://www.hardcodet.net/projects/wpf-notifyicon written by Philipp Sumi.
Very nice.Only thing you will be need to "detach" it from the SysTray screen coordinates and
make it appear where you wish.
Hope it helps.
Good luck.