Problem:
If multiple displays are running, move the main window from the current display to another display.
Set the Subwindow singleton mode, when the current display pops up, after the main window to move to another display, and then pop up the child window, you will find the child window ran to the original display .
----This is the pan of WPF
Since the windowstartuplocation= "Centerowner" has been set and the owner is added, the window pops up each time, theoretically, and the main window remains on the same screen.
Solve:
Add a delegate through the window's activated, and reset the window's location every time the window wakes up
2 ; 2;
Gets the screen where the current main window is located.
var screen = Screen.fromhandle (new windowinterophelper (MainWindow). Handle);
With:
By adding an additional property to the window
Public classwindowscreenhelper{ Public Static ReadOnlyDependencyProperty Iswindowshowincurrentscreenproperty =dependencyproperty.registerattached ("Iswindowshowincurrentscreen",typeof(BOOL),typeof(Windowscreenhelper),NewPropertyMetadata (default(BOOL) , Showwindowincurrentscreen)); Private Static voidShowwindowincurrentscreen (DependencyObject D, DependencyPropertyChangedEventArgs e) {varwindow = d asWindow; if(Window! =NULL) {window. Activated+=showincurrentscreenwindow_activated; } } Private Static voidShowincurrentscreenwindow_activated (Objectsender, EventArgs e) { varSubwindow = Sender asWindow; if(Subwindow = =NULL)return; if(Getiswindowshowincurrentscreen (Subwindow)) {varMainWindow =App.Current.MainWindow; if(MainWindow = =NULL)return; varScreen = Screen.fromhandle (NewWindowinterophelper (MainWindow). Handle); if(Subwindow.tag?). ToString () = =WindowState.Maximized.ToString ()) {Subwindow.left=Screen . Bounds.left; Subwindow.top=Screen . Bounds.top; Subwindow.width=Screen . Bounds.width; Subwindow.height=Screen . Bounds.height; } Else{subwindow.left= screen. Bounds.left + (screen. Bounds.width-subwindow.width)/2; Subwindow.top= screen. Bounds.top + (screen. Bounds.height-subwindow.height)/2; } } } Public Static voidSetiswindowshowincurrentscreen (DependencyObject element,BOOLvalue) {element. SetValue (iswindowshowincurrentscreenproperty, value); } Public Static BOOLGetiswindowshowincurrentscreen (DependencyObject element) {return(BOOL) element. GetValue (Iswindowshowincurrentscreenproperty); }}
Ps:
When the window state is maximized, that is windowstate= "maximized". The location is not set. Then you need to remove the xmal in the WindowState, through the background to set up full screen
Screen position issues for child windows when WPF is multi-screen