zoukankan      html  css  js  c++  java
  • 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中调用即可。这样就完美解决了自适应问题。

  • 相关阅读:
    设置多台机器linux服务器ssh相互无密码访问
    linux环境下 卸载 Oracle11G
    树型结构递归 实体递归 JSON格式
    Fiddler工具非常强大好用
    SQL 分页 SQL SERVER 2008
    Html table 细边框
    Oracle用户密码过期的处理方法
    将目录下面所有的 .cs 文件合并到一个 code.cs 文件中,写著作权复制代码时的必备良药
    微软帮你做了枚举的位运算
    根据身份证算出生日期和性别
  • 原文地址:https://www.cnblogs.com/Betty-IT/p/9592573.html
Copyright © 2011-2022 走看看