参考:http://yefenme.blog.163.com/blog/static/13069770420132283644288/spa
自适应首先考虑的是AutoScaleMode属性设置,其中=DPI对于图片控件来讲颇有效果,可是其余的就没用了,所以用参考文章中的帮助类AutoSizeFormClass。code
可是在实际应用中发现控件都乱了,一时没找到具体问题所在。后来细细看来,发现个人代码中有动态添加的控件,而参考文章中的初始化获取控件Size是在FormLoad中进行调用controllInitializeSize()的,致使控件数目不对。在动态添加控件时进行获取就能够达到想要的效果。orm
可是对于一些Label等控件显示大小与Width,Height值没有关系,只是和Font.Size大小相关,所以还须要特殊处理一番,添加方法以下:blog
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中调用便可。这样就完美解决了自适应问题。递归