当前位置:  首页>> 技术小册>> C#学习笔记

public partial class Form1 : Form {在此粘贴代码},即可实现控件自适应窗口的效果

  1. //默认主窗口类名称为Form1,在此添加代码
  2. public partial class Form1: Form {
  3. public Form1() {
  4. InitializeComponent();
  5. }
  6. //从这里开始复制下面的代码!!!
  7. //第一步,定义控件自适应窗口类AutoSizeFormClass
  8. class AutoSizeFormClass {
  9. public struct controlRect {
  10. public int Left;
  11. public int Top;
  12. public int Width;
  13. public int Height;
  14. }
  15. public List < controlRect > oldCtrl = new List < controlRect > ();
  16. int ctrlNo = 0;
  17. //重要方法一:记录窗体和其控件的初始位置和大小controllInitializeSize()
  18. public void controllInitializeSize(Control mForm) {
  19. controlRect cR;
  20. cR.Left = mForm.Left;
  21. cR.Top = mForm.Top;
  22. cR.Width = mForm.Width;
  23. cR.Height = mForm.Height;
  24. oldCtrl.Add(cR);
  25. AddControl(mForm);
  26. }
  27. private void AddControl(Control ctl) {
  28. foreach(Control c in ctl.Controls) {
  29. controlRect objCtrl;
  30. objCtrl.Left = c.Left;
  31. objCtrl.Top = c.Top;
  32. objCtrl.Width = c.Width;
  33. objCtrl.Height = c.Height;
  34. oldCtrl.Add(objCtrl);
  35. if (c.Controls.Count > 0)
  36. AddControl(c);
  37. }
  38. }
  39. //重要方法二:控件自适应大小方法controlAutoSize()
  40. public void controlAutoSize(Control mForm) {
  41. if (ctrlNo == 0) {
  42. controlRect cR;
  43. cR.Left = 0;
  44. cR.Top = 0;
  45. cR.Width = mForm.PreferredSize.Width;
  46. cR.Height = mForm.PreferredSize.Height;
  47. oldCtrl.Add(cR);
  48. AddControl(mForm);
  49. }
  50. float wScale = (float) mForm.Width / (float) oldCtrl[0].Width;
  51. float hScale = (float) mForm.Height / (float) oldCtrl[0].Height;
  52. ctrlNo = 1;
  53. AutoScaleControl(mForm, wScale, hScale);
  54. }
  55. private void AutoScaleControl(Control ctl, float wScale, float hScale) {
  56. int ctrLeft0, ctrTop0, ctrWidth0, ctrHeight0;
  57. foreach(Control c in ctl.Controls) {
  58. ctrLeft0 = oldCtrl[ctrlNo].Left;
  59. ctrTop0 = oldCtrl[ctrlNo].Top;
  60. ctrWidth0 = oldCtrl[ctrlNo].Width;
  61. ctrHeight0 = oldCtrl[ctrlNo].Height;
  62. c.Left = (int)((ctrLeft0) * wScale);
  63. c.Top = (int)((ctrTop0) * hScale); //
  64. c.Width = (int)(ctrWidth0 * wScale);
  65. c.Height = (int)(ctrHeight0 * hScale); //
  66. ctrlNo++;
  67. if (c.Controls.Count > 0)
  68. AutoScaleControl(c, wScale, hScale);
  69. if (ctl is DataGridView) {
  70. DataGridView dgv = ctl as DataGridView;
  71. Cursor.Current = Cursors.WaitCursor;
  72. int widths = 0;
  73. for (int i = 0; i < dgv.Columns.Count; i++) {
  74. dgv.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.AllCells); // 自动调整列宽
  75. widths += dgv.Columns[i].Width; // 计算调整列后单元列的宽度和
  76. }
  77. if (widths >= ctl.Size.Width) // 如果调整列的宽度大于设定列宽
  78. dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells; // 调整列的模式 自动
  79. else
  80. dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; // 如果小于 则填充
  81. Cursor.Current = Cursors.Default;
  82. }
  83. }
  84. }
  85. }
  86. //第二步,实例化上面定义的类
  87. AutoSizeFormClass asc = new AutoSizeFormClass();
  88. //第三步,初始化窗口时记录控件的大小和位置
  89. private void Form1_Load(object sender, EventArgs e) {
  90. asc.controllInitializeSize(this);
  91. }
  92. //第四步,主窗口大小发生变化触发的事件
  93. private void Form1_SizeChanged(object sender, EventArgs e) {
  94. asc.controlAutoSize(this);
  95. }
  96. }

该分类下的相关小册推荐:

暂无相关推荐.