在开发POS应用过程中,有一个这样的需求: POS机大都拥有一个主屏一个副屏,主屏用于业务操作,副屏用于向客户展示实时交易数据。这样就有了对窗体显示在哪个窗体有了特定的要求。但是作为一个比 ...
在开发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 }}
原标题:WPF 多屏开发,为窗体指定显示屏幕
关键词:wpf
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。