Winfrom 实现窗体拖拽自适应

第一步:创建一个AutoSizeFormClass类

第二步:初始化窗体大小类 并命名asc

第三步:调用controlAutoSize方法把本身传进去

  public partial class 自适应 : Form
  {
      AutoSizeFormClass asc = new AutoSizeFormClass();
      public 自适应()
      {
          InitializeComponent();
      }
      #region 创建自适应类
      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 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 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);
              }
          }
          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;
                  }
              }
          }
      }
      #endregion

      private void 自适应_SizeChanged(object sender, EventArgs e)
      {
          //把窗体放进自适应方法里
          asc.controlAutoSize(this);
      }
  }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lucky.帅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值