WPF multi-screen development, specifying a display screen for the form, wpf Display Screen
During the development of the POS application, there is a need as follows:
Most POS machines have a primary screen and a secondary screen for business operations. The secondary screen is used to display real-time transaction data to customers. In this way, you have specific requirements on which form the form is displayed. But as a relatively lazy programmer, I don't want to do this for the second time, so I want to implement a Window extension to achieve this.
The implementation concept is to obtain Screen information through the Screen class in System. Windows. Forms, and realize Attribute to mark the display Screen specified by the form.
The following is the implementation code:
using Pharos.POS.Retailing.MultipScreen;using System.Collections.Generic;using System.Linq;using System.Windows.Forms;namespace Pharos.POS.Retailing{ public static class MultipScreenManager { #region Property internal static Screen[] AllScreens { get { return Screen.AllScreens; } } internal static Screen PrimaryScreen { get { return Screen.PrimaryScreen; } } internal static IEnumerable<Screen> MinorScreens { get { return Screen.AllScreens.Where(o => o.Primary == false); } } internal static Screen FirstMinorScreen { get { return MinorScreens.FirstOrDefault(); } } #endregion Property #region Method public static void ShowInScreen(this System.Windows.Window win) { SetScreen(win); win.Show(); } public static void ShowDialogInScreen(this System.Windows.Window win) { SetScreen(win); win.ShowDialog(); } private static void SetScreen(System.Windows.Window win) { var attr = win.GetType().GetCustomAttributes(typeof(MultipScreenAttribute), false).FirstOrDefault(o => o is MultipScreenAttribute); int index = 0; bool ingoreOperation = false; WindowStartupLocationInScreen inScreen = WindowStartupLocationInScreen.CenterScreen; if (attr != null) { var temp = (attr as MultipScreenAttribute); index = temp.Index; inScreen = temp.InScreen; ingoreOperation = temp.IngoreMinorScreenError; } Screen screen = 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) { case WindowStartupLocationInScreen.CenterScreen: SetWindowInScreenCenter(win, screen); break; case WindowStartupLocationInScreen.Manual: SetWindowInScreenManual(win, screen); break; } } private static void SetWindowInScreenCenter(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 void SetWindowInScreenManual(System.Windows.Window win, Screen screen) { win.Top = screen.WorkingArea.Y; win.Left = screen.WorkingArea.X; } #endregion Method }}
Using System; namespace Pharos. POS. retailing. multipScreen {[AttributeUsage (AttributeTargets. class)] public class MultipScreenAttribute: Attribute {public MultipScreenAttribute (ScreenType type = ScreenType. primary, WindowStartupLocationInScreen inScreen = WindowStartupLocationInScreen. centerScreen): this (int) type, inScreen) {} public MultipScreenAttribute (int index = 0, WindowStartupLocationInScreen inScreen = WindowStartupLocationInScreen. centerScreen) {Index = index; InScreen = inScreen;} // <summary> // The position displayed during the initialization of the form /// </summary> public WindowStartupLocationInScreen InScreen {get; private set ;}/// <summary> /// screen Index. The value 0 indicates the primary screen, and the value 1 + indicates the secondary screen. /// </summary> public int Index {get; private set ;}/// <summary> /// if this value is TRUE, the display of this page is ignored when no specified screen is found, otherwise it will be displayed on the main screen // </summary> public bool IngoreMinorScreenError {get; private set ;}}}
Namespace Pharos. POS. retailing. multipScreen {public enum ScreenType {// <summary> /// home screen /// </summary> Primary = 0, /// <summary> /// secondary screen /// </summary> Minor = 1 ,}}
namespace Pharos.POS.Retailing.MultipScreen{ public enum WindowStartupLocationInScreen { Manual = 0, CenterScreen = 1, }}
Note: Do not set WindowStartupLocation In the window. This will cause an exception in the interface display. You can set the position of the screen throughMultipScreenAttribute flag settings!