最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C# Winform 實(shí)現(xiàn)控件自適應(yīng)父容器大小的示例代碼

 更新時(shí)間:2021年03月24日 10:39:44   作者:lcsyhh  
這篇文章主要介紹了C# Winform 實(shí)現(xiàn)控件自適應(yīng)父容器大小的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

在日常開發(fā)中經(jīng)常遇到控件不能隨著父容器大小的改變而且自動(dòng)改變控件的所在位置和大小。以下是實(shí)現(xiàn)的代碼

 /// <summary>
  /// 根據(jù)父容器實(shí)現(xiàn)控件自適應(yīng)大小位置
  /// </summary>
  /// <param name="control">所需自適應(yīng)大小位置的控件</param>
  private void ChangeLocationSizeByParent (Control control)
  {
    //記錄父容器大小,來(lái)判斷改變控件大小位置是因?yàn)楦溉萜鞯母淖冞€是通過(guò)設(shè)置控件大小位置去改變
    Size parentOldSize = control.Parent.Size;

    PointF locationPF = new PointF();
    locationPF.X = (float)control.Location.X / (float)control.Parent.Width;
    locationPF.Y = (float)control.Location.Y / (float)control.Parent.Height;
    
    PointF sizePF = new PointF();
    sizePF.X = (float)control.Width / (float)control.Parent.Width;
    sizePF.Y = (float)control.Height / (float)control.Parent.Height;

    control.LocationChanged += delegate (Object o, EventArgs e) {

      if (control.Parent != null&&parentOldSize.Equals(control.Parent.Size))
      {
        locationPF.X = (float)control.Location.X / (float)control.Parent.Width;
        locationPF.Y = (float)control.Location.Y / (float)control.Parent.Height;
        
      }
    };
    control.SizeChanged += delegate (Object o, EventArgs e) {

      if (control.Parent != null && parentOldSize.Equals(control.Parent.Size))
      {
        sizePF.X = (float)control.Width / (float)control.Parent.Width;
        sizePF.Y = (float)control.Height / (float)control.Parent.Height;
        
      }
    };
    control.ParentChanged += delegate (Object o, EventArgs e) {
      if (control.Parent == null)
      {
        return;
      }
      locationPF.X = (float)control.Location.X / (float)control.Parent.Width;
      locationPF.Y = (float)control.Location.Y / (float)control.Parent.Height;
      sizePF.X = (float)control.Width / (float)control.Parent.Width;
      sizePF.Y = (float)control.Height / (float)control.Parent.Height;
    };
    control.Parent.SizeChanged += delegate (Object po, EventArgs pe) {

      Control pControl = (Control)po;
      int x = (int)(pControl.Width * locationPF.X);
      int y = (int)(pControl.Height * locationPF.Y);
      control.Location = new Point(x, y);
      int width = (int)(pControl.Width * sizePF.X);
      int hetght = (int)(pControl.Height * sizePF.Y);
      control.Size = new Size(width, hetght);
      control.Refresh();
      parentOldSize = pControl.Size;
    };
  }

到此這篇關(guān)于C# Winform 實(shí)現(xiàn)控件自適應(yīng)父容器大小的示例代碼的文章就介紹到這了,更多相關(guān)C# Winform 控件自適應(yīng)父容器大小內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

景东| 尼勒克县| 铁力市| 杭锦旗| 津南区| 华阴市| 周口市| 桐梓县| 孝感市| 灌南县| 康定县| 枣强县| 镇平县| 绵竹市| 华坪县| 平罗县| 通河县| 肇源县| 桓台县| 桃源县| 仁化县| 永德县| 思南县| 团风县| 施秉县| 饶平县| 大新县| 慈利县| 桦南县| 宝鸡市| 拉孜县| 德钦县| 瑞安市| 家居| 石渠县| 祥云县| 固始县| 五常市| 油尖旺区| 韩城市| 日照市|