Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Window.Left is wrong when moving a maximized Window with Windows + Shift + Arrow Keys

Tags:

c#

wpf

I encountered a problem in WPF (C#), I need to position a popup on a certain point within the parent window. I get the parent position with Application.Current.MainWindow.Left and Application.Current.MainWindow.Top, it works as long as I don't move the window from one monitor to the other with the Windows shortcut Windows + Shift + /. If I use the shortcut the properties stay the same as they were before moving the window.

The window has to be WindowState.Maximized, it works if it is not maximized.

I also tried using Window.GetWindow(this) instead of Application.Current.MainWindow, result is the same.

It seems as if the positionchanged event doesn't occur for the Application.Current.MainWindow and it doesn't update the Left and Top properties.

I didn't find anything on this on SO or Google. A workaround, hint or solution are greatly appreciated.

like image 371
kirbby Avatar asked Oct 17 '25 03:10

kirbby


1 Answers

Try to use this:

    WindowInteropHelper windowInteropHelper = new WindowInteropHelper(Application.Current.MainWindow);
    Screen screen = System.Windows.Forms.Screen.FromHandle(windowInteropHelper.Handle);

The Screen Bounds property provides the coordinates of the whole window and the WorkingArea the boundaries of the area without titlebar and docked windows.

like image 189
Mihaeru Avatar answered Oct 18 '25 20:10

Mihaeru