How I can keep my window staying always on top even if there is a window of another application with Topmost = true
option activated and trying to stay in front of my window?

- 18,404
- 16
- 78
- 126

- 1,061
- 2
- 15
- 25
-
16[Don't do that](http://blogs.msdn.com/b/oldnewthing/archive/2011/03/10/10138969.aspx). – Mat Dec 11 '11 at 08:03
-
I believe this is possible through the WinAPI: there is "some" winner, but consider an application like DeskPins that wins over well, pretty much anything. (And I am pretty sure it *doesn't involve events*.) – Dec 11 '11 at 08:05
-
Run osk.exe and try to put something on top of it. – Hans Passant Dec 11 '11 at 12:11
3 Answers
You can do a platform invoke on BringWindowToTop to achieve this:
[DllImport("user32.dll", SetLastError=true)]
static extern bool BringWindowToTop(IntPtr hWnd);
[DllImport("user32.dll", SetLastError=true)]
static extern bool BringWindowToTop(HandleRef hWnd);
And call to it when the FocusLost event fires.

- 3,753
- 2
- 28
- 39
Easiest way(assuming you allready have topmost promery set) would be calling
myform.BringToFront();
on FIXED but relativly small time intervals(see Timer class), through all the time form must stay on top.
If calling this was conected to event that inform you of losing privileg of beeing on top, that could possibly cause resource-fihgts between multiple applications. Price of beeing safe is that some other program might be cheating by lisstening to informations when he is overthroned by your program, but the only solution for you wolud than be to kill that other program if you want to stay on top all the time :D

- 111
- 5
-
I've implemented this but I'm still wondering if there's a better way. What do you think the On Screen Keyboard that comes with Windows is doing to always stay on top? – Bruce van der Kooij Jan 08 '15 at 21:57
-
@BrucevanderKooij, OSK.exe doesn't stay always on top, nobody does. It is always in front of windows that don't have TopMost propery, but when you put Task Manager in front of it, and change focus between those two and some other Topmost=false windows you see strange focus/topmost bugs/(resource fight) appear on win7. If you want to be sure to have desired position, you must use win32 api calls. I learnd to use winapi calls form c# just to acomplish this in a subtitle writing program which I was making. This may be useful: http://stackoverflow.com/questions/916259/win32-bring-a-window-to-top – ETFovac May 24 '15 at 19:23
-
I tried to check what you said but on my Windows 8.1 install I have not been able to get taskmgr.exe to steal focus from osk.exe. It seems the Windows 8.1 task manager doesn't even try to stay on top. – Bruce van der Kooij May 25 '15 at 01:12
It should be possible by setting the Focus on window, from OnFocusLost event handler.

- 16,931
- 22
- 71
- 103