码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2015-05-29 21:45:28      阅读:910      评论:0      收藏:0      [点我收藏+]

标签:

    在开发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 多屏开发,为窗体指定显示屏幕

标签:

原文地址:http://www.cnblogs.com/akong/p/4539367.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!