Bringing A Window To Front Is Easily Possible, But The Common Solution Isn’t Satisfying
This is an issue I encountered, but couldn’t find a satisfying solution for online. You have a Window of another application, and you want to bring it to front, but without that giving it focus at all. The common solution I see often is to use the SetForegroundWindow() function, which does bring the applicaiton to front, but it gives it focus. While you could restore the focus to your previous application afterwards, this isn’t the most desirable solution in many cases.
There’s a chance that bringing a window to front that way isn’t supposed to be possible officially. Despite that, I was able to find a nice trick to do that. So I am sharing the solution I found here. It may not be an ideal solution, but it works fairly well. I hope this helps the people that are looking for a solution to the same problem.
Quickly go to:-
Let’s Get To The Coding
The solution is to make the window you want to bring forward a top most window. That will bring the window to the front, but it will make it sticky, so no other window could be brought in front of it. After that, you simply disable the top most flag, and so that’s it return to being a normal window, but after the window was already made in front of all the other windows.
To do both, we need the SetWindowPos function, with the right flags that make the window topmost, and to then unsticky it. Here’s how to import the function in your code:-
[DllImport("user32.dll", EntryPoint = "SetWindowPos")] public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
You need to declare some flags to use with the function as well:-
public const short SWP_NOMOVE = 0X2; public const short SWP_NOSIZE = 1; public const UInt32 TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE; public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); public static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
To use it, call SetWindowPos with the HWND_TOPMOST flag, then call it again with the HWND_NOTOPMOST. I added the two calls in a function you could use right away:-
public static void BringWindowToFront(IntPtr WindowHandle) { SetWindowPos(WindowHandle, (int)HWND_TOPMOST, 0, 0, 0, 0, (int)TOPMOST_FLAGS); SetWindowPos(WindowHandle, (int)HWND_NOTOPMOST, 0, 0, 0, 0, (int)TOPMOST_FLAGS); }
And Finally
This is one of the times where doing things in a twisted way works pretty well. I have been using this solution for a while now, and it’s working flawlessly for me. If you know a better way to bring a window to front, but without giving it focus. You’re more than welcome to share it with me. So that I coud include it here as well.
I hope you find the trick I shared here useful, and see you again with another one.
See Also
- Java VS Python VS C# detailed comparison, which language to learn first? – Tech Fairy
- Do you need an Expensive motherboard for gaming? – Tech Fairy
- What is the meaning of the different USB port colors? (blue, teal blue, yellow, red, black)
- Why motherboards & laptops still come with USB 2.0 ports When USB 3.0 Is Backward Compatible?
- USB 2.0 VS USB 3.0 Comparison: What are the differences between the two ports?
- History Of Graphics card motherboard slots: PCI VS AGP VS PCI-Express VS Integrated graphics.
- What are the advantages & disadvantages of using a Trackball? And why you should use one
- What’s the difference between remake & remaster? (With multiple examples).
This won’t work if there already are topmost windows in the way
Yeah, in that case, you will need to make your own Window TopMost. But the solution still do well in a lot of cases