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中调用即可。这样就完美解决了自适应问题。

  • 相关阅读:
    东芝开发板驱动OLED模块显示LOGO图片
    东芝MCU实现位带操作
    使用系统定时器SysTick实现精确延时微秒和毫秒函数
    VC++调试错误的解决方案
    #pragma once与 #ifndef的区别
    strcmp()字符串比较函数用法
    C、C++中的static和extern关键字
    error LNK1169 找到一个或多个多重定义的符号的解决方法
    vs2013编译obs源码
    Qt线程—QThread的使用--run和movetoThread的用法
  • 原文地址:https://www.cnblogs.com/Betty-IT/p/9592573.html
Copyright © 2011-2022 走看看