During the development of the POS application, there is a need to:
POS machine has a main screen, a secondary screen, the main screen for business operations, sub-screen to show customers real-time transaction data. This gives you a specific requirement for which form to display on the form. But as a lazy programmer, I am not willing to do this for a second time, so implement a window extension to achieve such a thing.
Implementation of the concept, through the System.Windows.Forms screen class to obtain the information on the display, the implementation of attribute to mark the form specified by the screen curtain.
Here is the implementation code:
usingPharos.POS.Retailing.MultipScreen;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Windows.Forms;namespacepharos.pos.retailing{ Public Static classMultipscreenmanager {#regionPropertyInternal Staticscreen[] Allscreens {Get { returnScreen.allscreens; } } Internal StaticScreen PrimaryScreen {Get { returnScreen.primaryscreen; } } Internal StaticIenumerable<screen>Minorscreens {Get { returnScreen.AllScreens.Where (o = o.primary = =false); } } Internal StaticScreen Firstminorscreen {Get { returnMinorscreens.firstordefault (); } } #endregionProperty#regionMethod Public Static voidShowinscreen ( ThisSystem.Windows.Window Win) {Setscreen (Win); Win. Show (); } Public Static voidShowdialoginscreen ( ThisSystem.Windows.Window Win) {Setscreen (Win); Win. ShowDialog (); } Private Static voidSetscreen (System.Windows.Window win) {varattr = win. GetType (). GetCustomAttributes (typeof(Multipscreenattribute),false). FirstOrDefault (o = O isMultipscreenattribute); intindex =0; BOOLIngoreoperation =false; Windowstartuplocationinscreen Inscreen=Windowstartuplocationinscreen.centerscreen; if(attr! =NULL) { vartemp = (attr asMultipscreenattribute); Index=temp. Index; Inscreen=temp. Inscreen; Ingoreoperation=temp. Ingoreminorscreenerror; } Screen Screens=PrimaryScreen; if(Index = =1&& Firstminorscreen! =NULL) { screen=Firstminorscreen; } Else if(Index >1&& Index <Minorscreens.count ()) { screen=Minorscreens.elementat (index); } Else if(Index >0&& index >= minorscreens.count () &&ingoreoperation) { return; } Switch(inscreen) { CaseWindowStartupLocationInScreen.CenterScreen:SetWindowInScreenCenter (win, screen); Break; CaseWindowStartupLocationInScreen.Manual:SetWindowInScreenManual (win, screen); Break; } } Private Static voidSetwindowinscreencenter (System.Windows.Window win, screen screen) {win. Top= screen. WORKINGAREA.Y + (screen. Workingarea.height-win. Height)/2; Win. Left= screen. workingarea.x + (screen. Workingarea.width-win. Width)/2; } Private Static voidSetwindowinscreenmanual (System.Windows.Window win, screen screen) {win. Top=Screen . WORKINGAREA.Y; Win. Left=Screen . workingarea.x; } #endregionMethod }}
usingSystem;namespacepharos.pos.retailing.multipscreen{[AttributeUsage (AttributeTargets.Class)] Public classMultipscreenattribute:attribute { PublicMultipscreenattribute (Screentype type = screentype.primary, Windowstartuplocationinscreen inScreen =windowstartuplocationinscreen.centerscreen): This((int) type, inscreen) {} PublicMultipscreenattribute (intindex =0, Windowstartuplocationinscreen Inscreen =windowstartuplocationinscreen.centerscreen) {Index=index; Inscreen=Inscreen; } /// <summary> ///Where the form is initialized to appear/// </summary> PublicWindowstartuplocationinscreen Inscreen {Get;Private Set; } /// <summary> ///screen Index, 0 main screen, 1+ to Subdisplay/// </summary> Public intIndex {Get;Private Set; } /// <summary> ///When no specified secondary screen is found, if the value is true, the display of the page is ignored, otherwise it will be displayed in the main screen/// </summary> Public BOOLIngoreminorscreenerror {Get;Private Set; } }}
namespacepharos.pos.retailing.multipscreen{ Public enumScreentype {/// <summary> ///Main Screen/// </summary>Primary =0, /// <summary> ///Secondary Screen/// </summary>Minor =1, }}
namespace pharos.pos.retailing.multipscreen{ publicenum windowstartuplocationinscreen { 0, 1, }}
Note: do not set windowstartuplocation in window, this will cause the interface to display an exception, the location of the setting screen can be Multipscreenattribute tag Settings!
WPF Multi-screen development, specifying a display screen for a form