• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
一只蜗牛
有一种爱挂着泪珠,但很凄美,它叫放弃!放弃虽然痛苦,但也是一种幸福的拥有,感情是不能勉强的,如果死死抓住, 抓住的也是伤痕和痛苦.把手握紧,里面什么也没有,把手松开就拥有了一切。
博客园    首页    新随笔    联系   管理    订阅  订阅
C# Winform 自适应

参考:http://yefenme.blog.163.com/blog/static/13069770420132283644288/

自适应首先考虑的是AutoScaleMode属性设置,其中=DPI对于图片控件来说很有效果,但是其他的就没用了,因此用参考文章中的帮助类AutoSizeFormClass。

  但是在实际应用中发现控件都乱了,一时没找到具体问题所在。后来细细看来,发现我的代码中有动态添加的控件,而参考文章中的初始化获取控件Size是在FormLoad中进行调用controllInitializeSize()的,导致控件数目不对。在动态添加控件时进行获取就可以达到想要的效果。

  但是对于一些Label等控件显示大小与Width,Height值没有关系,只是和Font.Size大小相关,因此还需要特殊处理一番,添加方法如下:

Dictionary<string, List <int>> dgvCols = new Dictionary<string, List<int>>();
Dictionary<string, float> LabelFonts = new Dictionary<string, float>();
Dictionary<string, float> RadioButtonFonts = new Dictionary<string, float>();

 

public void controllInitializeSize_Special(Control mForm)
        {
        AddControlInfo_Special(mForm);
        }        
private void AddControlInfo_Special(Control ctl)
        {
            foreach (Control c in ctl.Controls)
            {
              if (c.Controls.Count > 0)
                    AddControlInfo_Special(c);//窗体内其余控件还可能嵌套控件(比如panel),要单独抽出,因为要递归调用
                  if (c is SkinDataGridView )
            {
              SkinDataGridView dgv = c as SkinDataGridView;
                      List<int> columnsWidth = new List<int>();
              for (int col = 0; col < dgv.Columns.Count; col++)
                      {
                columnsWidth.Add(dgv.Columns[col].Width);
                      }
                      dgvCols.Add(c.Name, columnsWidth);
            } 
          if (c is Label)
          {
            Label label = c as Label;
                    float size = label.Font.Size;
                    LabelFonts.Add(label.Name, size);
          }
         if (c is RadioButton )
          {
                  RadioButton rdb = c as RadioButton;
                    float size = rdb.Font.Size;
                    RadioButtonFonts.Add(rdb.Name, size);
          }
      }
    }    

在Form_Load中调用即可。这样就完美解决了自适应问题。

 

一个不努力的人在努力做到努力
posted on 2018-09-05 15:53  XuPeppy  阅读(636)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3