当前位置:首页 > Windows程序 > 正文

WPF 多屏开发,为窗体指定显示屏幕

2021-05-26 Windows程序

    在开发POS应用过程中,有一个这样的需求:

POS机大都拥有一个主屏一个副屏,主屏用于业务操作,副屏用于向客户展示实时交易数据。这样就有了对窗体显示在哪个窗体有了特定的要求。但是作为一个比较懒惰的程序员,,我重来不愿意为这样的事情去做第二次,所以实现一个Window的扩展来实现这样的事情。

    实现构想,通过System.Windows.Forms下Screen类获取屏幕信息,实现Attribute用于标记窗体指定的显示屏幕。

以下是实现代码:

   

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> /// 在窗体初始化显示的位置 /// </summary> public WindowStartupLocationInScreen InScreen { get; private set; } /// <summary> /// 屏幕索引, 0为主屏,1+为次屏 /// </summary> public int Index { get; private set; } /// <summary> /// 当任何指定次屏没有找到时,如果该值为TRUE,则忽略这个页面的显示,否则将显示在主屏 /// </summary> public bool IngoreMinorScreenError { get; private set; } } }

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/71559.html