Questions about screen in WPF (primary secondary)

Source: Internet
Author: User

PrimaryScreen different uses of WPF and Winform

7187291

Wpf:
 This  This . Height;  This this. Width;
Winform:
 This  This . Height;  This this. Width;

Show and maximize WPF window on a specific screenquestion:

I ' m using the following method to display a WPF Window on a specific screen:

Private void Showonmonitor (int  Monitor,window Window) {    = screen.allscreens;     = Windowstyle.none;     = windowstartuplocation.manual;     = Screens[monitor]. Bounds.left;     = Screens[monitor]. bounds.top;     // window. WindowState = windowstate.maximized;     window. Show ();}

The problem is I need the Window to being maximized to fill the whole screens but as soon as I set:

Window. WindowState = windowstate.maximized;

It keeps showing the Window maximized but the first screen no matter what number I pass through to the Sh Owonmonitor () method.

Replies

1. Found the answer for this problem from the below link

Http://mostlytech.blogspot.in/2008/01/maximizing-wpf-window-to-second-monitor.html

We cannot maximize the window until the window is loaded when using multiple screens. So hooks up a window loaded event and sets the window state to maximized.

Private voidShowonmonitor (intMonitor, Window window) {    varScreen =Screenhandler.getscreen (monitor); varCurrentscreen = Screenhandler.getcurrentscreen ( This); Window. WindowState=Windowstate.normal; Window. Left=Screen .    Workingarea.left; Window. Top=Screen .    Workingarea.top; Window. Width=Screen .    Workingarea.width; Window. Height=Screen .    Workingarea.height; Window. Loaded+=window_loaded; }/*You can use this event for all the Windows*/Private voidWindow_Loaded (Objectsender, RoutedEventArgs e) {    varSenderwindow = Sender asWindow; Senderwindow.windowstate=windowstate.maximized;}

ScreenHandler.cs

 Public Static classscreenhandler{ Public StaticScreen getcurrentscreen (window window) {varParentarea =NewSystem.Drawing.Rectangle ((int) window. Left, (int) window. Top, (int) window. Width, (int) window.        Height); returnScreen.fromrectangle (Parentarea); }     Public StaticScreen Getscreen (intRequestedscreen) {        varScreens =Screen.allscreens; varMainscreen =0; if(screens. Length >1&& Mainscreen <screens. Length) {returnScreens[requestedscreen]; }        returnscreens[0]; }}

2. I found another post on MSDN which also works:

Private void Showonmonitor (int  Monitor,window Window) {    = screen.allscreens;     = Windowstyle.none;     = windowstartuplocation.manual;     = Screens[monitor]. Bounds.left;     = Screens[monitor]. bounds.top;     + = (snd, arg)         = = windowstate.maximized;    Window. Show ();}

3. I am running into what I ' m sure are a problem with an obvious solution but where do I run Showonmonitor (monitor,window)?     After InitializeComponent () of the MainWindow () class function But that doesn ' t seem to is the case. I thought I would just need to run showonmonitor (0,mainwindow); But it's saying ' MainWindow is a type not a variable ' which are true, so I'm not sure how to access to the current Mainwind ow.

EDIT:
Found the solution.   ' This ' was the window to pass. Showonmonitor (0,this);
Also I tweaked ScreenHandler.cs:

if 1 && Mainscreen < screens. Length) {    if (screens. Length > requestedscreen)    {        return  screens[requestedscreen];    }     Else     {        return1];}    }

This conditional ensures, the requested monitor is within bounds and avoids a ' dumb ' crash in case a config XML or STA Rtup flag is malformed.

How do I get the size of the current screen in WPF?

Https://stackoverflow.com/questions/1927540/how-to-get-the-size-of-the-current-screen-in-wpf

Question:

I know I can get the size of the primary screen by using

System.Windows.SystemParameters.PrimaryScreenWidth; System.Windows.SystemParameters.PrimaryScreenHeight;

But how does I get the size of the current screen? (multi-screen users do don't always use the primary screen and not all screens is using the same resolution, right?)

It would is nice to being able to acces the size from XAML, but doing so from code (C #) would suffice.

Answer:

1. I created a little wrapper around the screens from System.Windows.Forms, currently everything works ... Not sure about the "device independent pixels", though.

 Public classwpfscreen{ Public StaticIenumerable<wpfscreen>Allscreens () {foreach(Screen screeninchSystem.Windows.Forms.Screen.AllScreens) {yield return Newwpfscreen (screen); }    }     Public Staticwpfscreen getscreenfrom (Window window) {windowinterophelper windowinterophelper=Newwindowinterophelper (window); Screen Screens=System.Windows.Forms.Screen.FromHandle (Windowinterophelper.handle); Wpfscreen Wpfscreen=Newwpfscreen (screen); returnWpfscreen; }     Public StaticWpfscreen Getscreenfrom (Point point) {intx = (int) Math.Round (point.        X); inty = (int) Math.Round (point.        Y); //is x, y device-independent-pixels??System.Drawing.Point Drawingpoint =NewSystem.Drawing.Point (x, y); Screen Screens=System.Windows.Forms.Screen.FromPoint (Drawingpoint); Wpfscreen Wpfscreen=Newwpfscreen (screen); returnWpfscreen; }     Public Staticwpfscreen Primary {Get{return NewWpfscreen (System.Windows.Forms.Screen.PrimaryScreen);} }    Private ReadOnlyScreen screen ; Internalwpfscreen (System.Windows.Forms.Screen screen) { This. Screen =Screen ; }     PublicRect devicebounds {Get{return  This. GetRect ( This. Screen. Bounds); }    }     PublicRect Workingarea {Get{return  This. GetRect ( This. Screen. Workingarea); }    }    PrivateRect getrect (Rectangle value) {//should x, y, width, height be device-independent-pixels??        return NewRect {X=value. X, Y=value. Y, Width=value. Width, Height=value.    Height}; }     Public BOOLIsprimary {Get{return  This. Screen. Primary; }    }     Public stringDeviceName {Get{return  This. Screen. devicename; }    }}

2. As far as I know there are no native WPF function to get dimensions of the current monitor. Instead could PInvoke native multiple display monitors functions, wrap them in managed class and expose all properties You need to consume the them from XAML.

Wpf:multiple Screens

Https://stackoverflow.com/questions/17859414/wpf-multiple-screens

Question:

I ' m writing a screensaver in WPF. I have the screensaver working, however, it is only displays on my main monitor. Is there a by "black out" or draw graphics to additional monitors when the user has multiple displays? I ' ve done some searching around, but haven ' t found anything relevant.

UPDATE:

From Ananthonline ' s answer below, I is able to accomplish the "black out" effect on non-primary displays using the follow ing window:

<Windowx:class= "Screensaver.blackout"xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"Cursor= "None"WindowStyle= "None"ResizeMode= "Noresize"Background= "Black"></Window>

and initializing one for each screens in App.xaml.cs using the following process:

foreach inch Screen.allscreens) {    if (s! = Screen.primaryscreen    )        {new  BlackOut ()        ; = s.workingarea.top;         = s.workingarea.left;         = s.workingarea.width;         = s.workingarea.height;        Blackout.show ();    }}

Note an import to was System.Windows.Forms required to access the Screen class.

Answer:

You should is able to use the system.drawing.screen.* classes to set up multiple windows on each screen. Mind so you don't set each window to being maximized, but a properly sized, border less window.

Also-you might want to remember, the total bounds of the Multi monitor setup, the is not always is a rectangle (if you p LAN to "union" all the bounds to create a window spanning all monitors).

Questions about screen in WPF (primary secondary)

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.