星空网 > 软件开发 > ASP.net

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

    在开发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; }  }}

 

 

namespace Pharos.POS.Retailing.MultipScreen{  public enum ScreenType  {    /// <summary>    /// 主屏    /// </summary>    Primary = 0,    /// <summary>    /// 次屏    /// </summary>    Minor = 1,  }}

 

namespace Pharos.POS.Retailing.MultipScreen{  public enum WindowStartupLocationInScreen  {    Manual = 0,    CenterScreen = 1,  }}

注意: 请不要在window中设置 WindowStartupLocation,这样将导致界面显示异常,设置屏幕的位置可以通过MultipScreenAttribute 标记设置!

 




原标题:WPF 多屏开发,为窗体指定显示屏幕

关键词:wpf

wpf
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

无货源亚马逊店群全面介绍:https://www.ikjzd.com/w/1048
2019速卖通3.28大促:https://www.ikjzd.com/w/1049
wish:https://www.ikjzd.com/w/105
mymall:https://www.ikjzd.com/w/1050
爱淘:https://www.ikjzd.com/w/1051
商标抢注_商标抢注现状:https://www.ikjzd.com/w/1053
TikTok斥资210万美元游说美国参议院阻止法案通过 :https://www.goluckyvip.com/news/188220.html
北京飞机票查询(快速查询北京至各地机票价格和航班信息):https://www.vstour.cn/a/366178.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流