3
votes

My .NET app is running in the background, and occasionally creates and shows a form. The form should be visible above all other windows, but without focus.

My first approach was to set the TopMost property of the form, but that fails in many cases for reasons I can't explain.

Then I tried calling BringToFront in the Shown event handler, but that didn't do anything.

I also tried BringWindowToTop API, which returned true (= success), but without result.

Now I'm stuck with SetForegroundWindow, which works, but also gives focus to the window (as it should).

Any suggestions would be appreciated.

(Also, is it normal that I can just steal focus without any problem?)

1
My initial response would be to set TopMost but you already said that fails. So, you might want to elaborate on these reasons that you can't explain.rein

1 Answers

2
votes

This may help you:

[DllImport("user32.dll")]
public static extern bool SetWindowPos (IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
void Exmpl() {
    IntPtr HWND_TOPMOST = new IntPtr(-1);
    uint SWP_NOACTIVATE = 0x0010;
    uint SWP_NOMOVE = 0x0002;
    uint SWP_NOSIZE = 0x0001;
    SetWindowPos(form.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
}