Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Always On Top

Tags:

c#

wpf

topmost

Is it possible to make a window stay always on top even when other application is running on Fullscreen? I'm using right now TopMost = true but when other application is running on fullscreen mine becomes invisible. It's WindowStyle = None window by the way.

Edit: And do not let other window minimalize ofcourse

like image 468
Kuba Wasilczyk Avatar asked Nov 18 '13 14:11

Kuba Wasilczyk


3 Answers

This won't work 100% of the time, but it will improve the situation somewhat. You can set Topmost = true in the handler for the Window.Deactivated event:

private void Window_Deactivated(object sender, EventArgs e)
{
    Window window = (Window)sender;
    window.Topmost = true;
}

The Deactivated event will be called whenever your application loses focus (often when another application requests to be Topmost) and so this will reset your application on top after this.

like image 54
Sheridan Avatar answered Nov 13 '22 03:11

Sheridan


Try this solution from MSDN, it should work for you. In the Window Activated Event add the following code:

this.Width   = System.Windows.SystemParameters.PrimaryScreenWidth;
this.Height  = System.Windows.SystemParameters.PrimaryScreenHeight;
this.Topmost = true;
this.Top  = 0;
this.Left = 0;

in DeActivated Event add the following code

this.Topmost = true;
this.Activate();

Original post from MSDN

like image 22
Mohammed A. Fadil Avatar answered Nov 13 '22 02:11

Mohammed A. Fadil


None of the solutions above for me worked, so here is what I ended up doing. It worked perfectly for me.

Basically, to keep it on top you just set the lose focus event to make it go back to top.

XAML:

PreviewLostKeyboardFocus="Window_PreviewLostKeyboardFocus"
    

Code Behind:

private void Window_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
      var window = (Window)sender;
      window.Topmost = true;
}
like image 14
Travis Tubbs Avatar answered Nov 13 '22 03:11

Travis Tubbs