您的位置:首页 > 编程语言 > C#

C#根据窗体大小改变控件大小

2014-12-10 16:10 274 查看
首先定义全局变量

<span style="white-space:pre"></span><pre name="code" class="csharp">int label1_width, label1_height, label1_left, label1_top;
int label2_width, label2_height, label2_left, label2_top;
int label4_width, label4_height, label4_left, label4_top;
int textBox1_width, textBox1_height, textBox1_left, textBox1_top;
int textBox2_width, textBox2_height, textBox2_left, textBox2_top;
int textBox3_width, textBox3_height, textBox3_left, textBox3_top;
int textBox4_width, textBox4_height, textBox4_left, textBox4_top;
int button1_width, button1_height, button1_left, button1_top;
int button2_width, button2_height, button2_left, button2_top;
int button3_width, button3_height, button3_left, button3_top;<span style="font-family: Arial, Helvetica, sans-serif;"> </span>


然后在private void Form1_Load(object sender, EventArgs e)函数里边

label1_width = label1.Width;
label1_height = label1.Height;
label1_left = label1.Left;
label1_top = label1.Top;

label2_width = label2.Width;
label2_height = label2.Height;
label2_left = label2.Left;
label2_top = label2.Top;

label4_width = label4.Width;
label4_height = label4.Height;
label4_left = label4.Left;
label4_top = label4.Top;

textBox1_width = textBox1.Width;
textBox1_height = textBox1.Height;
textBox1_left = textBox1.Left;
textBox1_top = textBox1.Top;

textBox2_width = textBox2.Width;
textBox2_height = textBox2.Height;
textBox2_left = textBox2.Left;
textBox2_top = textBox2.Top;

textBox3_width = textBox3.Width;
textBox3_height = textBox3.Height;
textBox3_left = textBox3.Left;
textBox3_top = textBox3.Top;

textBox4_width = textBox4.Width;
textBox4_height = textBox4.Height;
textBox4_left = textBox4.Left;
textBox4_top = textBox4.Top;

button1_width = button1.Width;
button1_height = button1.Height;
button1_left = button1.Left;
button1_top = button1.Top;

button2_width = button2.Width;
button2_height = button2.Height;
button2_left = button2.Left;
button2_top = button2.Top;

button3_width = button3.Width;
button3_height = button3.Height;
button3_left = button3.Left;
button3_top = button3.Top;

写函数
private void SizeRefresh()
{
label1.Left = label1_left * this.Width / form1_width;
label1.Top = label1_top * this.Height / form1_height;
label1.Width = label1_width * this.Width / form1_width;
label1.Height = label1_height * this.Height / form1_height;

label2.Left = label2_left * this.Width / form1_width;
label2.Top = label2_top * this.Height / form1_height;
label2.Width = label2_width * this.Width / form1_width;
label2.Height = label2_height * this.Height / form1_height;

label4.Left = label4_left * this.Width / form1_width;
label4.Top = label4_top * this.Height / form1_height;
label4.Width = label4_width * this.Width / form1_width;
label4.Height = label4_height * this.Height / form1_height;

textBox1.Left = textBox1_left * this.Width / form1_width;
textBox1.Top = textBox1_top * this.Height / form1_height;
textBox1.Width = textBox1_width * this.Width / form1_width;
textBox1.Height = textBox1_height * this.Height / form1_height;

textBox2.Left = textBox2_left * this.Width / form1_width;
textBox2.Top = textBox2_top * this.Height / form1_height;
textBox2.Width = textBox2_width * this.Width / form1_width;
textBox2.Height = textBox2_height * this.Height / form1_height;

textBox3.Left = textBox3_left * this.Width / form1_width;
textBox3.Top = textBox3_top * this.Height / form1_height;
textBox3.Width = textBox3_width * this.Width / form1_width;
textBox3.Height = textBox3_height * this.Height / form1_height;

textBox4.Left = textBox4_left * this.Width / form1_width;
textBox4.Top = textBox4_top * this.Height / form1_height;
textBox4.Width = textBox4_width * this.Width / form1_width;
textBox4.Height = textBox4_height * this.Height / form1_height;

button1.Left = button1_left * this.Width / form1_width;
button1.Top = button1_top * this.Height / form1_height;
button1.Width = button1_width * this.Width / form1_width;
button1.Height = button1_height * this.Height / form1_height;

button2.Left = button2_left * this.Width / form1_width;
button2.Top = button2_top * this.Height / form1_height;
button2.Width = button2_width * this.Width / form1_width;
button2.Height = button2_height * this.Height / form1_height;

button3.Left = button3_left * this.Width / form1_width;
button3.Top = button3_top * this.Height / form1_height;
button3.Width = button3_width * this.Width / form1_width;
button3.Height = button3_height * this.Height / form1_height;

}

调用函数
protected override void OnPaint(PaintEventArgs e) //主要重绘在这里体现
{
SizeRefresh();
}

将窗体的DoubleBufferfd改为True,否则会卡。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: