zoukankan      html  css  js  c++  java
  • winform中,如何控制控件位置随窗体的大小改变而改变

     winform中,如何控制控件位置随窗体的大小改变而改变
     
    有如下3种方法:

    方法1
    [csharp] view plaincopy
     
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.ComponentModel;  
    4. using System.Data;  
    5. using System.Drawing;  
    6. using System.Text;  
    7. using System.Windows.Forms;  
    8.   
    9. namespace MarkPrinter  
    10. {  
    11.     public partial class ResizeTest : Form  
    12.     {  
    13.         public float X;  
    14.         public float Y;  
    15.         //public float y;  
    16.   
    17.         public ResizeTest()  
    18.         {  
    19.             InitializeComponent();  
    20.         }  
    21.   
    22.         private void setTag(Control cons)  
    23.         {  
    24.             foreach (Control con in cons.Controls)  
    25.             {  
    26.                 con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;  
    27.                 if (con.Controls.Count > 0)  
    28.                     setTag(con);  
    29.             }  
    30.         }  
    31.         private void setControls(float newx, float newy, Control cons)  
    32.         {  
    33.             foreach (Control con in cons.Controls)  
    34.             {  
    35.   
    36.                 string[] mytag = con.Tag.ToString().Split(new char[] { ':' });  
    37.                 float a = Convert.ToSingle(mytag[0]) * newx;  
    38.                 con.Width = (int)a;  
    39.                 a = Convert.ToSingle(mytag[1]) * newy;  
    40.                 con.Height = (int)(a);  
    41.                 a = Convert.ToSingle(mytag[2]) * newx;  
    42.                 con.Left = (int)(a);  
    43.                 a = Convert.ToSingle(mytag[3]) * newy;  
    44.                 con.Top = (int)(a);  
    45.                 Single currentSize = Convert.ToSingle(mytag[4]) * newy;  
    46.   
    47.                 //改变字体大小  
    48.                 con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);  
    49.   
    50.                 if (con.Controls.Count > 0)  
    51.                 {  
    52.                     setControls(newx, newy, con);  
    53.                 }  
    54.             }  
    55.   
    56.         }  
    57.   
    58.         void Form1_Resize(object sender, EventArgs e)  
    59.         {  
    60.             // throw new Exception("The method or operation is not implemented.");  
    61.             float newx = (this.Width) / X;  
    62.             //  float newy = (this.Height - this.statusStrip1.Height) / (Y - y);  
    63.             float newy = this.Height / Y;  
    64.             setControls(newx, newy, this);  
    65.             this.Text = this.Width.ToString() + " " + this.Height.ToString();  
    66.   
    67.         }  
    68.   
    69.         private void ResizeTest_Load(object sender, EventArgs e)  
    70.         {  
    71.             this.Resize += new EventHandler(Form1_Resize);  
    72.   
    73.             X = this.Width;  
    74.             Y = this.Height;  
    75.             //y = this.statusStrip1.Height;  
    76.             setTag(this);  
    77.         }  
    78.     }  
    79. }  




    方法2

    [csharp] view plaincopy
     
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.ComponentModel;  
    4. using System.Data;  
    5. using System.Drawing;  
    6. using System.Text;  
    7. using System.Windows.Forms;  
    8.   
    9. namespace MarkPrinter  
    10. {  
    11.     public partial class ResizeTest : Form  
    12.     {  
    13.         public float X;  
    14.         public float Y;  
    15.         public float y;  
    16.   
    17.         public ResizeTest()  
    18.         {  
    19.             InitializeComponent();  
    20.         }  
    21.   
    22.         private void ResizeTest_Load(object sender, EventArgs e)  
    23.         {  
    24.             AutoScale(this);  
    25.         }  
    26.   
    27.         /// <summary>   
    28.         /// 控件随窗体自动缩放   
    29.         /// </summary>   
    30.         /// <param name="frm"></param>   
    31.         public static void AutoScale(Form frm)  
    32.         {  
    33.             frm.Tag = frm.Width.ToString() + "," + frm.Height.ToString();  
    34.             frm.SizeChanged += new EventHandler(frm_SizeChanged);  
    35.         }  
    36.   
    37.         static void frm_SizeChanged(object sender, EventArgs e)  
    38.         {  
    39.             string[] tmp = ((Form)sender).Tag.ToString().Split(',');  
    40.             float width = (float)((Form)sender).Width / (float)Convert.ToInt16(tmp[0]);  
    41.             float heigth = (float)((Form)sender).Height / (float)Convert.ToInt16(tmp[1]);  
    42.   
    43.             ((Form)sender).Tag = ((Form)sender).Width.ToString() + "," + ((Form)sender).Height;  
    44.   
    45.             foreach (Control control in ((Form)sender).Controls)  
    46.             {  
    47.                 control.Scale(new SizeF(width, heigth));  
    48.   
    49.             }  
    50.         }  
    51.     }  
    52. }  
        1. --转载处:http://blog.csdn.net/xd43100678/article/details/7899047
  • 相关阅读:
    关于博客
    lua中table复制
    logstash收集慢查询日志配置
    logstash的timestamp使用日志中的日期
    logstash收集bash_history历史命令
    使用supervisord监控logstash
    logstash 2.2以上版本,nginx 错误日志切割
    结合ELK进行分析PV,构建异步的WAF
    正则匹配嵌套结构
    一道关于停车计费的问题
  • 原文地址:https://www.cnblogs.com/s115/p/4044749.html
Copyright © 2011-2022 走看看