当前位置:编程学习 > C#/ASP.NET >>

c# winform窗口自适应各种分辨率类

近期做项目时,遇到开发的winform在自己电脑上可以正常显示,共享到其他电脑就事儿不能显示了:

1.当两个电脑分辨率相同时,无法显示完全,请检查form的autoscalemode属性是否为none,并设为none

2.分辨率不同时,可直接在form的构造函数中调用初始化函数之后, 加上一句AutoReSizeForm. SetFormSize(this);(对于自定义控件usercontrol也适用)

public class AutoReSizeForm

    {
        static float SH
        {
            get
            {
                return (float)Screen.PrimaryScreen.Bounds.Height / Properties.Settings.Default.Y;
            }
        }
        static float SW
        {
            get
            {
                return (float)Screen.PrimaryScreen.Bounds.Width / Properties.Settings.Default.X;
            }
        }

 


        public static void SetFormSize(Control fm)
        {
            fm.Location = new Point((int)(fm.Location.X * SW), (int)(fm.Location.Y * SH));
            fm.Size = new Size((int)(fm.Size.Width * SW), (int)(fm.Size.Height * SH));
            fm.Font = new Font(fm.Font.Name, fm.Font.Size * SH,fm.Font.Style,fm.Font.Unit,fm.Font.GdiCharSet,fm.Font.GdiVerticalFont);
            if (fm.Controls.Count!=0)
            {
                SetControlSize(fm);
            }
        }


        private static void SetControlSize(Control InitC)
        {
            foreach (Control c in InitC.Controls)
            {
                c.Location = new Point((int)(c.Location.X * SW), (int)(c.Location.Y * SH));
                c.Size = new Size((int)(c.Size.Width * SW), (int)(c.Size.Height * SH));
                c.Font = new Font(c.Font.Name, c.Font.Size * SH, c.Font.Style, c.Font.Unit, c.Font.GdiCharSet, c.Font.GdiVerticalFont);
                if (c.Controls.Count != 0)
                {
                    SetControlSize(c);
                }
            }
        }
    }

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,