在做YH的时候,为了扩展界面的功能区域,使得更为方便的使用系统,所以对界面的大小进行了扩充。随之而来的分辨率问题也就来了。功能写完了,但是现在又在原来的界面上显示不全。改回去太麻烦了,只好写了一个自适应分辨率的类,来解决这一问题。
多的不说了,直接上代码吧。
1using System; 2using System.Collections.Generic; 3using System.Drawing; 4using System.Windows.Forms; 5 6class AutoSizeForm 7{ 8 //(1).声明结构,只记录窗体和其控件的初始位置和大小。 9 public struct controlRect 10 { 11 public int Left; 12 public int Top; 13 public int Width; 14 public int Height; 15 public float FontSize; 16 } 17 18 //(2).声明 1个对象 19 //注意这里不能使用控件列表记录 List<Control> nCtrl;,因为控件的关联性,记录的始终是的大小。 20 public List<controlRect> oldCtrl; 21 22 //int ctrl_first = 0; 23 //(3). 创建两个函数 24 //(3.1)记录窗体和其控件的初始位置和大小, 25 public void controllInitializeSize(Form mForm) 26 { 27 // if (ctrl_first == 0) 28 { 29 // ctrl_first = 1; 30 oldCtrl = new List<controlRect>(); 31 controlRect cR; 32 //记录窗体位置和大小及字体大小 33 cR.Left = mForm.Left; 34 cR.Top = mForm.Top; 35 cR.Width = mForm.Width; 36 cR.Height = mForm.Height; 37 cR.Width = int.Parse(mForm.Tag.ToString().Split(',')[0]); 38 cR.Height = int.Parse(mForm.Tag.ToString().Split(',')[1]); 39 cR.FontSize = mForm.Font.Size; 40 41 oldCtrl.Add(cR); 42 //记录控件的位置大小及字体大小 43 GetControlSize(mForm); 44 } 45 } 46 47 //记录控件容器中各个控件的位置与大小 48 private void GetControlSize(Control con) 49 { 50 int s = con.Controls.Count; 51 string name = con.Name; 52 //记录控件的位置大小及字体大小 53 foreach (Control c in con.Controls) 54 { 55 controlRect objCtrl; 56 objCtrl.Left = c.Left; 57 objCtrl.Top = c.Top; 58 objCtrl.Width = c.Width; 59 objCtrl.Height = c.Height; 60 objCtrl.FontSize = c.Font.Size; 61 oldCtrl.Add(objCtrl); 62 //记录容器控件中的控件位置,大小,及字体大小 63 if (c.GetType().ToString() == "System.Windows.Forms.Panel") 64 { 65 GetControlSize(c); 66 } 67 } 68 } 69 70 71 //(3.2)控件自适应大小, 72 public void controlAutoSize(Form mForm) 73 { 74 float wScale; 75 float hScale; 76 try 77 { 78 wScale = (float)mForm.Width / (float)oldCtrl[0].Width;//新旧窗体之间的比例,与最早的旧窗体 79 hScale = (float)mForm.Height / (float)oldCtrl[0].Height;//.Height; 80 } 81 catch (Exception) 82 { 83 return; 84 } 85 86 int ctrLeft0, ctrTop0, ctrWidth0, ctrHeight0; 87 float ctrFontSize0; 88 int ctrlNo = 1;//第1个是窗体自身的 Left,Top,Width,Height,所以窗体控件从ctrlNo=1开始 89 90 SetSize(mForm, ctrlNo, wScale, hScale); 91 92 } 93 94 private void SetSize(Control con, int ctrlNo, float wScale, float hScale) 95 { 96 int ctrLeft0, ctrTop0, ctrWidth0, ctrHeight0; 97 float ctrFontSize0; 98 int s = con.Controls.Count; 99 string name = con.Name; 100 foreach (Control c in con.Controls) 101 { 102 ctrLeft0 = oldCtrl[ctrlNo].Left; 103 ctrTop0 = oldCtrl[ctrlNo].Top; 104 ctrWidth0 = oldCtrl[ctrlNo].Width; 105 ctrHeight0 = oldCtrl[ctrlNo].Height; 106 ctrFontSize0 = oldCtrl[ctrlNo].FontSize; 107 108 c.Left = (int)((ctrLeft0) * wScale);//新旧控件之间的线性比例。控件位置只相对于窗体,所以不能加 + wLeft1 109 c.Top = (int)((ctrTop0) * hScale);// 110 c.Width = (int)(ctrWidth0 * wScale);//只与最初的大小相关,所以不能与现在的宽度相乘 (int)(c.Width * w); 111 c.Height = (int)(ctrHeight0 * hScale);// 112 c.Font = new Font(c.Font.Name, (float)(ctrFontSize0 * hScale)); 113 ctrlNo += 1; 114 if (ctrlNo >= oldCtrl.Count) return; 115 if (c.GetType().ToString() == "System.Windows.Forms.Panel") 116 { 117 SetSize(c, ctrlNo, wScale, hScale);//设置控件容器中的控件大小。 118 ctrlNo += c.Controls.Count; 119 } 120 } 121 } 122 123}
版权声明:本文为博主原创文章,未经博主允许不得转载。