Windows 窗体的自适应分辨率、分屏显示、开机自启动

前言

这里所说的针对Winform、WPF 都适用。开机自启动对于控制台的也可以。

还是从项目实践中得来的,在这里记录下来。

对于自适应、分屏显示,在以前感觉应该比较高大上的问题,会比较难。在经过这次的实践感觉挺简单的。不过其中还是有一点要根据实际的业务有一些复杂。

一、自适应分辨率

在屏幕的分辨率改变的时候,改变窗口的大小为当前的分辨率,进行适应。这里是要在 “Window_SizeChanged” 事件里面处理。

具体代码:

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.WindowState = WindowState.Normal;
            this.WindowStyle = WindowStyle.None;
            this.ResizeMode = ResizeMode.NoResize;
            if (screenIndex == 0)
            {
                this.Left = 0;
                this.Top = 0;
                Width = Screen.AllScreens[0].Bounds.Width;
                Height = Screen.AllScreens[0].Bounds.Height;
            }
            else
            {
                int leftPix = 0;
                for (int i = screenIndex - 1; i >= 0; i--)
                {
                    leftPix += Screen.AllScreens[i].Bounds.Width;
                }
                this.Left = leftPix;
                this.Top = 0;
                Width = Screen.AllScreens[screenIndex].Bounds.Width;
                Height = Screen.AllScreens[screenIndex].Bounds.Height;
            }
        }

这个代码里面有分屏显示的一部分。

二、分屏显示

项目中需要一台主机输出到多个屏幕,在对应屏幕显示窗口。

在这里要注意,屏幕的排列顺序。这个顺序是“显示”设置界面设置的顺序,不是物理的顺序

例如:对于两个屏幕,物理上可以一个在上一个在下,但是在“显示”设置界面,可以设置为左右的。

对于左右顺序:每一个屏幕的宽、高还是屏幕的宽高,只是其起始位置坐标不一样。第一个左上角:(0,0),第二个左上角(width1,0)【这里的 width1 是平屏幕1的宽】,后面以此类推。

对于上下顺序:每一个屏幕的宽、高还是屏幕的宽高,只是其起始位置坐标不一样。第一个左上角:(0,0),第二个左上角(0,height1)【这里的 height1是平屏幕1的高】,后面以此类推。

下面的是一个示例图:

知道了屏幕对应坐标的变化就可以上代码:

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Normal;
            this.WindowStyle = WindowStyle.None;
            this.ResizeMode = ResizeMode.NoResize;
            string appName = "Client";
            int appCount = Process.GetProcessesByName(appName).ToList().Count;
            int screenCount = Screen.AllScreens.Count();
            if (appCount > screenCount)
            {
                this.Close();
            }
            else if (appCount == 1)
            {
                this.Left = 0;
                this.Top = 0;
                Width = Screen.AllScreens[0].Bounds.Width;
                Height = Screen.AllScreens[0].Bounds.Height;
            }
            else
            {
                int leftPix = 0;
                for (int i = appCount - 2; i >= 0; i--)
                {
                    leftPix += Screen.AllScreens[i].Bounds.Width;
                }
                this.Left = leftPix;
                this.Top = 0;
                Width = Screen.AllScreens[appCount - 1].Bounds.Width;
                Height = Screen.AllScreens[appCount - 1].Bounds.Height;
            }
            this.screenIndex = appCount - 1;

            InitPlay();
        }

上面的代码就是根据当前程序的个数,来分配屏幕。开了多个程序,感觉这样方式还不够好,应该一个程序进行分配。

三、开机自启动

开机自启动有几个方式:

1、在开机启动文件夹里面加入要开机启动的快捷方式即可;

2、修改注册表,添加要启动的程序到注册表;

这里用的是第二种方法。

        public static void AutoStart(bool isAuto)
        {
            try
            {
                if (isAuto == true)
                {
                    RegistryKey R_local = Registry.LocalMachine;    //RegistryKey R_local = Registry.CurrentUser;
                    RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                    R_run.SetValue("应用名称", "程序地址/Client.exe");
                    R_run.Close();
                    R_local.Close();    
                }
                else
                {
                    RegistryKey R_local = Registry.LocalMachine;//RegistryKey R_local = Registry.CurrentUser;
                    RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                    R_run.DeleteValue("应用名称", false);
                    R_run.Close();
                    R_local.Close();
                }

                //GlobalVariant.Instance.UserConfig.AutoStart = isAuto;
            }
            catch (Exception)
            {
                System.Windows.MessageBox.Show("您需要管理员权限修改", "提示");
            }
        }

猜你喜欢

转载自www.cnblogs.com/zhurong/p/10900039.html
今日推荐