C# WinForm窗体及其控件自适应各种屏幕分辨率
来源:互联网 发布:翼龙贷 招聘网络 编辑:程序博客网 时间:2024/06/08 11:56
1.声明AutoSizeFormClass类 copy
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
-
- namespace WindowsApplication1
- {
- public class AutoSizeFormClass
- {
-
- public struct controlRect
- {
- public int Left;
- public int Top;
- public int Width;
- public int Height;
- }
-
-
-
- public List<controlRect> oldCtrl = new List<controlRect>();
- int ctrlNo = 0;
-
-
- public void controllInitializeSize(Control mForm)
- {
- controlRect cR;
- cR.Left = mForm.Left; cR.Top = mForm.Top; cR.Width = mForm.Width; cR.Height = mForm.Height;
- oldCtrl.Add(cR);
- AddControl(mForm);
-
-
- }
- private void AddControl(Control ctl)
- {
- foreach (Control c in ctl.Controls)
- {
-
-
- controlRect objCtrl;
- objCtrl.Left = c.Left; objCtrl.Top = c.Top; objCtrl.Width = c.Width; objCtrl.Height = c.Height;
- oldCtrl.Add(objCtrl);
-
- if (c.Controls.Count > 0)
- AddControl(c);
- }
- }
-
- public void controlAutoSize(Control mForm)
- {
- if (ctrlNo == 0)
- {
-
- controlRect cR;
-
- cR.Left = 0; cR.Top = 0; cR.Width = mForm.PreferredSize.Width; cR.Height = mForm.PreferredSize.Height;
-
- oldCtrl.Add(cR);
- AddControl(mForm);
- }
- float wScale = (float)mForm.Width / (float)oldCtrl[0].Width;
- float hScale = (float)mForm.Height / (float)oldCtrl[0].Height;
- ctrlNo = 1;
- AutoScaleControl(mForm, wScale, hScale);
- }
- private void AutoScaleControl(Control ctl, float wScale, float hScale)
- {
- int ctrLeft0, ctrTop0, ctrWidth0, ctrHeight0;
-
- foreach (Control c in ctl.Controls)
- {
-
-
- ctrLeft0 = oldCtrl[ctrlNo].Left;
- ctrTop0 = oldCtrl[ctrlNo].Top;
- ctrWidth0 = oldCtrl[ctrlNo].Width;
- ctrHeight0 = oldCtrl[ctrlNo].Height;
-
-
- c.Left = (int)((ctrLeft0) * wScale);
- c.Top = (int)((ctrTop0) * hScale);
- c.Width = (int)(ctrWidth0 * wScale);
- c.Height = (int)(ctrHeight0 * hScale);
- ctrlNo++;
-
- if (c.Controls.Count > 0)
- AutoScaleControl(c, wScale, hScale);
-
- if (ctl is DataGridView)
- {
- DataGridView dgv = ctl as DataGridView;
- Cursor.Current = Cursors.WaitCursor;
-
- int widths = 0;
- for (int i = 0; i < dgv.Columns.Count; i++)
- {
- dgv.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.AllCells);
- widths += dgv.Columns[i].Width;
- }
- if (widths >= ctl.Size.Width)
- dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
- else
- dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
-
- Cursor.Current = Cursors.Default;
- }
- }
-
-
- }
- }
- }
2.为窗体添加Load事件,并在其方法Form1_Load中,调用类的初始化方法,记录窗体和其控件初始位置和大小3.为窗体添加SizeChanged事件,并在其方法Form1_SizeChanged中,调用类的自适应方法,完成自适应 copy
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
-
- namespace WindowsApplication1
- {
- public partial class Form1 : Form
- {
-
- AutoSizeFormClass asc = new AutoSizeFormClass();
-
- public Form1()
- {
- InitializeComponent();
- }
-
-
- private void Form1_Load(object sender, EventArgs e)
- {
- asc.controllInitializeSize(this);
- }
-
-
- private void Form1_SizeChanged(object sender, EventArgs e)
- {
- asc.controlAutoSize(this);
- }
- }
- }