form分辨率

近期作項目時,遇到開發的winform在本身電腦上能夠正常顯示,共享到其餘電腦就事兒不能顯示了:函數

【轉載自:http://blog.csdn.net/lcawen88/article/details/8989332】this

1.當兩個電腦分辨率相同時,沒法顯示徹底,請檢查form的autoscalemode屬性是否爲none,並設爲none.net

2.分辨率不一樣時,可直接在form的構造函數中調用初始化函數以後, 加上一句AutoReSizeForm. SetFormSize(this);(對於自定義控件usercontrol也適用)orm

public class AutoReSizeFormblog

    {
        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);
                }
            }
        }
    } 開發

相關文章
相關標籤/搜索