C# WinForm窗体及其控件自适应各种屏幕分辨率

时间:2023-03-09 22:50:57
C# WinForm窗体及其控件自适应各种屏幕分辨率

C# WinForm窗体及其控件自适应各种屏幕分辨率

一。说明 
  我们自己编写程序的界面,会遇到各种屏幕分辨率,只有自适应才能显的美观。实际上,做到这点也很简单,就是首先记录窗体和它上面控件的初始位置和大小,当窗体改变比例时,其控件的位置和大小也按此比例变化即可。因为窗体上控件的位置和大小是相对于自己所在的窗体的,也就是所谓的窗口坐标。 
  在这里我们只考虑相对于自己窗体的窗口坐标更简单,也就是成比例变化。为了多个窗体共用,我在这里创建一个类AutoSizeFormClass ,1.使用它去记录窗体和其控件的初始位置和大小,2.根据窗体变化了的大小,成比例地实现其控件的水平和垂直方向的变化,也就是自适应。

二。使用方法 
  使用方法很简单, 
  1.把自适应的类整体复制到你的工程命名空间里, 
   然后在需要自适应的窗体中做3步即可: 
  2.声明自适应类实例。 
  3.为窗体添加Load事件,并在其方法Form1_Load中,调用类的初始化方法,记录窗体和其控件初始位置和大小 
  4.为窗体添加SizeChanged事件,并在其方法Form1_SizeChanged中,调用类的自适应方法,完成自适应

三。完整代码如下:

(一)。自适应窗体的代码:

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
{
//1.声明自适应类实例
AutoSizeFormClass asc = new AutoSizeFormClass();
public Form1()
{
InitializeComponent();
}
//2. 为窗体添加Load事件,并在其方法Form1_Load中,调用类的初始化方法,记录窗体和其控件的初始位置和大小
private void Form1_Load(object sender, EventArgs e)
{
asc.controllInitializeSize(this);
}
//3.为窗体添加SizeChanged事件,并在其方法Form1_SizeChanged中,调用类的自适应方法,完成自适应
private void Form1_SizeChanged(object sender, EventArgs e)
{
asc.controlAutoSize(this);
}
}
}

(二)自适应类的代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
class AutoSizeFormClass
{
//(1).声明结构,只记录窗体和其控件的初始位置和大小。
public struct controlRect
{
public int Left;
public int Top;
public int Width;
public int Height;
}
//(2).声明 1个对象
//注意这里不能使用控件列表记录 List<Control> nCtrl;,因为控件的关联性,记录的始终是当前的大小。
public List<controlRect> oldCtrl;
//int ctrl_first = 0;
//(3). 创建两个函数
//(3.1)记录窗体和其控件的初始位置和大小,
public void controllInitializeSize(Form mForm)
{
// if (ctrl_first == 0)
{
// ctrl_first = 1;
oldCtrl = new List<controlRect>();
controlRect cR;
cR.Left = mForm.Left; cR.Top = mForm.Top; cR.Width = mForm.Width; cR.Height = mForm.Height;
oldCtrl.Add(cR);
foreach (Control c in mForm.Controls)
{
controlRect objCtrl;
objCtrl.Left = c.Left; objCtrl.Top = c.Top; objCtrl.Width = c.Width; objCtrl.Height = c.Height;
oldCtrl.Add(objCtrl);
}
}
// this.WindowState = (System.Windows.Forms.FormWindowState)(2);//记录完控件的初始位置和大小后,再最大化
//0 - Normalize , 1 - Minimize,2- Maximize
}
//(3.2)控件自适应大小,
public void controlAutoSize(Form mForm)
{
//int wLeft0 = oldCtrl[0].Left; ;//窗体最初的位置
//int wTop0 = oldCtrl[0].Top;
////int wLeft1 = this.Left;//窗体当前的位置
//int wTop1 = this.Top;
float wScale = (float)mForm.Width / (float)oldCtrl[].Width;//新旧窗体之间的比例,与最早的旧窗体
float hScale = (float)mForm.Height / (float)oldCtrl[].Height;//.Height;
int ctrLeft0, ctrTop0, ctrWidth0, ctrHeight0;
int ctrlNo = ;//第1个是窗体自身的 Left,Top,Width,Height,所以窗体控件从ctrlNo=1开始
foreach (Control c in mForm.Controls)
{
ctrLeft0 = oldCtrl[ctrlNo].Left;
ctrTop0 = oldCtrl[ctrlNo].Top;
ctrWidth0 = oldCtrl[ctrlNo].Width;
ctrHeight0 = oldCtrl[ctrlNo].Height;
//c.Left = (int)((ctrLeft0 - wLeft0) * wScale) + wLeft1;//新旧控件之间的线性比例
//c.Top = (int)((ctrTop0 - wTop0) * h) + wTop1;
c.Left = (int)((ctrLeft0) * wScale);//新旧控件之间的线性比例。控件位置只相对于窗体,所以不能加 + wLeft1
c.Top = (int)((ctrTop0) * hScale);//
c.Width = (int)(ctrWidth0 * wScale);//只与最初的大小相关,所以不能与现在的宽度相乘 (int)(c.Width * w);
c.Height = (int)(ctrHeight0 * hScale);//
ctrlNo += ;
}
} }
}

当然,窗口坐标和屏幕坐标也是可以相互转换的,  

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
int x = e.X; //相对form窗口的坐标,客户区坐标
int y = e.Y;
int x1 = Control.MousePosition.X;//相对显示器,屏幕的坐标
int y1 = Control.MousePosition.Y;
}
它们之间转换如下: 
this.Location; // 窗体所在坐标
this.PointToScreen(new Point(, )); // 客户区坐标转换为屏幕坐标
this.PointToClient(new Point(, )); // 屏幕坐标转换为客户区坐标