I have a clock application. I have set the Window's TopMost property. But, randomly, some other window or visual studio comes above clock.
Is there any other way to make my window (clock app) to display always on top of all other applications.
I have a clock application. I have set the Window's TopMost property. But, randomly, some other window or visual studio comes above clock.
Is there any other way to make my window (clock app) to display always on top of all other applications.
Are you sure it's a random window? If another window is also a topmost window, it is possible for it to come above your window.
I also had this problem when setting Window.Topmost = true on already existing window sometimes worked, sometimes not. Below is my workaround, you can probably combine it with Window_Deactivated approach mentioned by other people, if WS_EX_TOPMOST style is lost at runtime.
Extra code:
I know that this question is old, but I don't quite understand why the accepted answer has received up votes... or why it was accepted... it doesn't really answer the question, or provide a solution and answers posted these days that are that short are almost always down voted and/or deleted by the community. Ah well, I guess it was posted in different times.
Either way, as old as it is, I have a possible solution for anyone who may come across this post in the future. You can simply handle the
Window.Deactivated
Event and/or theApplication.Deactivated
Event. TheWindow.Deactivated
Event occurs when a window becomes a background window and theApplication.Deactivated
Event occurs when an application stops being the foreground application.The idea is to set the relevant
TopMost
property totrue
each time your application orWindow
loses focus:It's worth noting that other developers can also use this technique, so this doesn't guarantee that your
Window
will always remain on top, but it works for me and the situation is still certainly improved by using it.