Screen position issues for child windows when WPF is multi-screen

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.