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

 1 近期做项目时,遇到开发的winform在自己电脑上可以正常显示,共享到其他电脑就事儿不能显示了:
 2 
 3 1.当两个电脑分辨率相同时,无法显示完全,请检查form的autoscalemode属性是否为none,并设为none
 4 
 5 2.分辨率不同时,可直接在form的构造函数中调用初始化函数之后, 加上一句AutoReSizeForm. SetFormSize(this);(对于自定义控件usercontrol也适用)
 6 
 7 public class AutoReSizeForm
 8 
 9     {
10          static float SH
11          {
12              get
13              {
14                  return (float)Screen.PrimaryScreen.Bounds.Height / Properties.Settings.Default.Y;
15              }
16          }
17          static float SW
18          {
19              get
20              {
21                  return (float)Screen.PrimaryScreen.Bounds.Width / Properties.Settings.Default.X;
22              }
23          }
24 
25  
26 
27 
28          public static void SetFormSize(Control fm)
29          {
30              fm.Location = new Point((int)(fm.Location.X * SW), (int)(fm.Location.Y * SH));
31              fm.Size = new Size((int)(fm.Size.Width * SW), (int)(fm.Size.Height * SH));
32              fm.Font = new Font(fm.Font.Name, fm.Font.Size * SH,fm.Font.Style,fm.Font.Unit,fm.Font.GdiCharSet,fm.Font.GdiVerticalFont);
33              if (fm.Controls.Count!=0)
34              {
35                  SetControlSize(fm);
36              }
37          }
38 
39 
40          private static void SetControlSize(Control InitC)
41          {
42              foreach (Control c in InitC.Controls)
43              {
44                  c.Location = new Point((int)(c.Location.X * SW), (int)(c.Location.Y * SH));
45                  c.Size = new Size((int)(c.Size.Width * SW), (int)(c.Size.Height * SH));
46                  c.Font = new Font(c.Font.Name, c.Font.Size * SH, c.Font.Style, c.Font.Unit, c.Font.GdiCharSet, c.Font.GdiVerticalFont);
47                  if (c.Controls.Count != 0)
48                  {
49                      SetControlSize(c);
50                  }
51              }
52          }
53      }

 

posted @ 2013-05-31 10:06  如.若  阅读(1760)  评论(0编辑  收藏  举报