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.
Asked
Active
Viewed 3.3k times
1 Answers
65
You need to use P/Invoke with SetWindowPos to accopmlish this:
[DllImport("user32.dll")]
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);

Reed Copsey
- 554,122
- 78
- 1,158
- 1,373
-
Thanks for that, should do the trick! Is user32 valid on 64 bit systems too? – James Cadd Oct 07 '09 at 00:12
-
@JamesCadd Bydefault, windows 64 bit have both 32 and 64 bit system files. – Gray Programmerz Aug 04 '21 at 13:48