James Cadd James Cadd - 1 year ago 86
C# Question

Make a window topmost using a window handle

After launching an application using the Process class I'd like to make that window topmost. Currently, my app is the topmost window so when i launch the other app it doesn't display. One thing that came to mind is that I could set topmost = false for my application before launching the process, the problem with this is I want to give the process ample time to load up before displaying it to the user, so I'd like more control over when I switch the other application to the topmost.

Answer Source

You need to use P/Invoke with SetWindowPos to accopmlish this:

static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
const UInt32 SWP_NOSIZE = 0x0001;
const UInt32 SWP_NOMOVE = 0x0002;
const UInt32 SWP_SHOWWINDOW = 0x0040;

// Call this way:
SetWindowPos(theWindowHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download