c# winform窗口自适应各种分辨率类
生活随笔
收集整理的這篇文章主要介紹了
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 }
?
總結
以上是生活随笔為你收集整理的c# winform窗口自适应各种分辨率类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 拼接字符SQL语句拼接 最后一个字符多出
- 下一篇: Android Svn 中 Bin ,