1. /*
    2. * 窗体最大化问题解决方案
    3. * */
    4. //下列为窗体名
    5. public Login()
    6. {
    7. InitializeComponent();
    8. changeSize();
    9. }
    10. //改变大小
    11. void changeSize()
    12. {
    13. SetStyle(ControlStyles.UserPaint, true);
    14. SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
    15. SetStyle(ControlStyles.OptimizedDoubleBuffer, true); // 双缓冲DoubleBuffer
    16. //GetContentFromExcel.projectUrl = Path.GetDirectoryName(Application.ExecutablePath);
    17. x = this.Width;
    18. y = this.Height;
    19. setTag(this);
    20. if (this.WindowState == FormWindowState.Maximized)
    21. {
    22. this.WindowState = FormWindowState.Normal;
    23. }
    24. else
    25. {
    26. this.FormBorderStyle = FormBorderStyle.Sizable;
    27. this.WindowState = FormWindowState.Normal;
    28. }
    29. }
    30. #region 控件大小随窗体大小等比例缩放
    31. private float x;//定义当前窗体的宽度
    32. private float y;//定义当前窗体的高度
    33. private void setTag(Control cons)
    34. {
    35. foreach (Control con in cons.Controls)
    36. {
    37. con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
    38. if (con.Controls.Count > 0)
    39. {
    40. setTag(con);
    41. }
    42. }
    43. }
    44. //设置双缓冲区、解决闪屏问题
    45. public static void SetDouble(Control cc)
    46. {
    47. cc.GetType().GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance |
    48. System.Reflection.BindingFlags.NonPublic).SetValue(cc, true, null);
    49. }
    50. private void setControls(float newx, float newy, Control cons)
    51. {
    52. //遍历窗体中的控件,重新设置控件的值
    53. foreach (Control con in cons.Controls)
    54. {
    55. //获取控件的Tag属性值,并分割后存储字符串数组
    56. SetDouble(this);
    57. SetDouble(con);
    58. if (con.Tag != null)
    59. {
    60. string[] mytag = con.Tag.ToString().Split(new char[] { ';' });
    61. //根据窗体缩放的比例确定控件的值
    62. con.Width = Convert.ToInt32(System.Convert.ToSingle(mytag[0]) * newx);//宽度
    63. con.Height = Convert.ToInt32(System.Convert.ToSingle(mytag[1]) * newy);//高度
    64. con.Left = Convert.ToInt32(System.Convert.ToSingle(mytag[2]) * newx);//左边距
    65. con.Top = Convert.ToInt32(System.Convert.ToSingle(mytag[3]) * newy);//顶边距
    66. Single currentSize = System.Convert.ToSingle(mytag[4]) * newy;//字体大小
    67. con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
    68. if (con.Controls.Count > 0)
    69. {
    70. setControls(newx, newy, con);
    71. }
    72. }
    73. }
    74. }
    75. //在窗体的的事件中添加Resize事件
    76. private void Login_Resize(object sender, EventArgs e)
    77. {
    78. float newx = (this.Width) / x;
    79. float newy = (this.Height) / y;
    80. setControls(newx, newy, this);
    81. }
    82. #endregion

    注意:需要在窗体的Resize事件中添加Login_Resize方法,否则不起作用。

    窗体的常用事件 事件可以理解为用户的操作,如点击鼠标键盘 C# Winform窗体最大化,控件等比例放大 - 图1 应用程序需要在事件发生时进行响应,因此事件分为: 注册事件:必须为对象注册事件才会被执行(如为某控件添加一个单击的事件) 触发事件:注册后的事件必须触发才可发生(如当用户单击该控件时触发该事件)

    快速添加事件: 选中某控件,打开事件面板,在某一事件后面空白处双击即可。 常用事件:Click MouseDown KeyDown等等 一般根据字面意思即可理解事件意义