Samuel
Samuel

Reputation: 17171

Form.TopMost works sometimes

It appears that the topmost property sometimes puts my application above all others, but throughout my testing it has been very weird in that sometimes it works and the window remains above all other (external application) windows, but sometimes it does nothing at all. I have even tried using the WS_EX_TOPMOST flag by setting it with the Win32 API call to setWindowLong(). None of them keep the window on top. Is there another way to keep a window on top of every open window besides using topmost? Or is there something else I should know about topmost?

Upvotes: 2

Views: 5269

Answers (4)

Martin.Martinsson
Martin.Martinsson

Reputation: 2154

Works 100%!

User32.AllowSetForegroundWindow((uint)Process.GetCurrentProcess().Id);
User32.SetForegroundWindow(Handle);
User32.ShowWindow(Handle, User32.SW_SHOWNORMAL);

Upvotes: 0

Pierre Arnaud
Pierre Arnaud

Reputation: 10537

I simply use this:

form.TopLevel = true;
form.TopMost  = true;

which makes the window top-level (i.e. it has no parent and behaves as the main form of the application), then ensures that it is topmost (i.e. displays above all other non-topmost windows). It has always worked like a charm.

Note that I do this before I show the window.

Upvotes: 5

Ken
Ken

Reputation: 1880

I've had luck with the following win32 api calls:

const int SW_SHOW = 5;
BringWindowToTop(form.Handle);
ShowWindow(form.Handle, SW_SHOW);

Upvotes: 2

Brian R. Bondy
Brian R. Bondy

Reputation: 347466

In addition to Form.TopMost you can try the Win32 API SetForegroundWindow.

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
public static extern bool SetForegroundWindow(IntPtr hwnd);

Then call SetForegroundWindow(this.Handle).

Upvotes: 1

Related Questions