winform控件随窗体大小变化

Winform 窗体控件随窗体大小自适应

步骤实现如下:

1.在窗体中放一个容器(Panel),将容器的Dock属性设置为Fill。窗体中所有控件都放入这个容器中。

2.创建一个窗体类,该窗体类继承于原始窗体类,原来的窗体继承创建的窗体类:如下图所示
自适应基类
正常窗体

新建一个 BaseForm :Form 窗体类,继承默认窗体类 Form ,而原来的 MainForm :Form 窗体类继承的默认窗体类修改为 MainForm :BaseForm 自定义新建的窗体类。

新建窗体类代码如下:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace app.YKKJ.MEMS
{
  /// <summary>
  /// 控件自适应窗体大小
  /// </summary>
  public partial class BaseForm : Form
  {
      #region 控件缩放
      double formWidth;//窗体原始宽度
      double formHeight;//窗体原始高度
      double scaleX;//水平缩放比例
      double scaleY;//垂直缩放比例
      Dictionary<string, string> controlInfo = new Dictionary<string, string>();
      //控件中心Left,Top,控件Width,控件Height,控件字体Size
      /// <summary>
      /// 获取所有原始数据
      /// </summary>
      protected void GetAllInitInfo(Control CrlContainer)
      {
          if (CrlContainer.Parent == this)
          {
              formWidth = Convert.ToDouble(CrlContainer.Width);
              formHeight = Convert.ToDouble(CrlContainer.Height);
          }
          foreach (Control item in CrlContainer.Controls)
          {
              if (item.Name.Trim() != "")
                  controlInfo.Add(item.Name, (item.Left + item.Width / 2) + "," + (item.Top + item.Height / 2) + "," + item.Width + "," + item.Height + "," + item.Font.Size);
              if ((item as UserControl) == null && item.Controls.Count > 0)
                  GetAllInitInfo(item);
          }
      }

      private void ControlsChangeInit(Control CrlContainer)
      {
          scaleX = (Convert.ToDouble(CrlContainer.Width) / formWidth);
          scaleY = (Convert.ToDouble(CrlContainer.Height) / formHeight);
      }

      /// <summary>
      /// 设置控件大小以及样式
      /// </summary>
      /// <param name="CrlContainer"></param>
      private void ControlsChange(Control CrlContainer)
      {
          double[] pos = new double[5];//pos数组保存当前控件中心Left,Top,控件Width,控件Height,控件字体Size
          foreach (Control item in CrlContainer.Controls)
          {
              //判断有此控件
              if (item.Name.Trim() != "")
              {
                  if ((item as UserControl) == null && item.Controls.Count > 0)
                      ControlsChange(item);
                  //获取窗体改变前的控件信息
                  string[] strs = controlInfo[item.Name].Split(',');
                  for (int j = 0; j < 5; j++)
                  {
                      pos[j] = Convert.ToDouble(strs[j]);
                  }
                  //设置窗体改变之后的控件信息
                  double itemWidth = pos[2] * scaleX;
                  double itemHeight = pos[3] * scaleY;
                  //左边距
                  item.Left = Convert.ToInt32(pos[0] * scaleX - itemWidth / 2);
                  //上边距
                  item.Top = Convert.ToInt32(pos[1] * scaleY - itemHeight / 2);
                  //控件宽度
                  item.Width = Convert.ToInt32(itemWidth);
                  //控件高度
                  item.Height = Convert.ToInt32(itemHeight);
                  try
                  {
                      //字体大小  可更具自己需要选择是否调整
                      item.Font = new Font(item.Font.Name, float.Parse((pos[4] * Math.Min(scaleX, scaleY)).ToString()));
                  }
                  catch
                  {

                  }
              }
          }
      }
      #endregion
          
      /// <summary>
      /// 窗体改变大小
      /// </summary>    
      protected override void OnSizeChanged(EventArgs e)
      {
          base.OnSizeChanged(e);
          if (controlInfo.Count > 0)
          {
              ControlsChangeInit(this.Controls[0]);
              //获取Controls实例
              ControlsChange(this.Controls[0]);
          }
      }
  }
}

新建的窗体类中主要包括自定义几个方法,用以实现控件自适应

(1)获取控件初始信息:GetAllInitInfo()

(2)获取窗体缩放比例:ControlsChaneInit()

(3)窗体改变时修改控件大小:ControlsChange()

最后。别忘了在窗体类的构造函数中调用获取初始信息GetAllInitInfo()的方法,并将当前this.Control[0]参数传进去:

public Form1()       
{
 	InitializeComponent();  
    ​GetAllInitInfo(this.Controls[0]);      
}

这样,一个自适应窗体就实现了,再也不用担心最大化和拖拽后窗体控件位置错位的尴尬了。

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值