Check out my iOS apps here
As an Amazon Associate I earn from qualifying purchases.

How to bring an application window to front without giving it focus in C#, and without using SetGoregroundWindow()

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.

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

2 thoughts on “How to bring an application window to front without giving it focus in C#, and without using SetGoregroundWindow()”

Leave a Comment

Your email address will not be published. Required fields are marked *