c# winform 根據窗體自動調整控件


一、概述

     本文要實現的功能是:當窗體最大化時,控件的大小可以隨窗體一起變化。開發環境,vs2010 c# winform,窗體名稱采用默認的Form1.

2、把調整控件大小的方法放到一個類中:FormSetSelfAuto.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Windows.Forms;
 6 using System.Drawing;
 7 
 8 
 9 namespace Kaifafanli
10 {
11    public class FormSetSelfAuto
12     {
13         private float X;
14         private float Y;
15         public float _x
16         {
17             set { X = value; }
18         }
19         public float _y
20         {
21             set { Y = value; }
22         }
23        //獲取控件的width,height,left,top,字體的大小值,存放在控件的tag屬性中
24         public void setTag(Control cons)
25         { 
26          //遍歷窗體中的控件
27             foreach(Control con in cons.Controls)
28             {
29                 con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;
30                 if(con.Controls.Count>0)
31                 {
32                     setTag(con);
33                 }
34             }
35         }
36        //根據窗體大小調整控件大小 
37        public void setControls(float newx,float newy,Control cons)
38         { 
39           //遍歷窗體中的控件,重新設置控件的值
40             foreach(Control con in cons.Controls)
41             {
42             //獲取控件tag屬性值,並分割后存儲字符串數組
43                 string[] mytag=con.Tag.ToString().Split(new char[]{':'});
44                 float a = Convert.ToSingle(mytag[0]) * newx;//根據窗體縮放比例確定控件的寬度值
45                 con.Width = (int)a;
46                 a = Convert.ToSingle(mytag[1]) * newy;
47                 con.Height = (int)a;//高度
48 
49                 a = Convert.ToSingle(mytag[2]) * newx;
50                 con.Left = (int)a;//左邊緣距離
51                 a = Convert.ToSingle(mytag[3]) * newy;
52                 con.Top = (int)a;//上邊緣距離
53                 Single currentSize = Convert.ToSingle(mytag[4]) * newy;
54                 con.Font = new Font(con.Font.Name,currentSize,con.Font.Style,con.Font.Unit);
55                 if(con.Controls.Count>0)
56                 {
57                     setControls(newx,newy,con);
58                 }
59 
60             }
61         }
62        public void form_Resize(Form fr)
63        {
64            float newx = (fr.Width) / X;
65            float newy = (fr.Height) / Y;
66            setControls(newx, newy, fr);
67            fr.Text = fr.Width.ToString() + " " + fr.Height.ToString();
68        }
69     }
70 }
FormSetSelfAuto.cs

3、在窗體Form1中的調用方法

Form1的代碼如下所示:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 
10 namespace Kaifafanli
11 {
12     public partial class Form1 : Form
13     {
14         public Form1()
15         {
16             InitializeComponent();
17         }
18 
19        
20         FormSetSelfAuto fa = new FormSetSelfAuto();
21         private void Form1_Load(object sender, EventArgs e)
22         {
23             this.Resize += new EventHandler(Form1_Resize);
24             fa._x = this.Width;
25             fa._y = this.Height;
26             fa.setTag(this);
27         }
28 
29         private void Form1_Resize(object sender, EventArgs e)
30         {
31             fa.form_Resize(this);
32         }
33     }
34 }
Form1

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM