可以考虑处理一下该控件的SizeChanged事件。
程序代码:
using System;
using System.Collections.Generic;
using using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
/*
* 设置Label1
*/
this.label1.AutoSize = false;
this.label1.BorderStyle = BorderStyle.FixedSingle;
this.label1.BackColor = Color.Yellow;
/*
* 设置Label2
*/
this.label2.AutoSize = false;
this.label2.BorderStyle = BorderStyle.Fixed3D;
this.label2.BackColor = Color.Green;
this.Load += new EventHandler(UserControl1_Load);
this.SizeChanged += new EventHandler(UserControl1_SizeChanged);
}
private void UserControl1_Load(object sender, EventArgs e)
{
SetLabels();
}
private void UserControl1_SizeChanged(object sender, EventArgs e)
{
SetLabels();
}
private void SetLabels()
{
this.label2.Visible = this.label1.Visible = false;
/*
* 这里设置lable1和label2各占控件的3/8和5/8
*/
int pControlWidth = this.Width; // 获取控件的宽度
int PControlHeight = this.Height; // 获取控件的高度
int lbl1Width = 3 * pControlWidth / 8; // label1的宽度
int lbl2Width = 5 * pControlWidth / 8; // label2的宽度
int lbl1X = 0; // label1 X坐标
int lbl2X = lbl1X + lbl1Width; // label2 X坐标
this.label1.Location = new Point(lbl1X, 0);
this.label2.Location = new Point(lbl2X, 0);
this.label1.Size = new Size(lbl1Width, PControlHeight);
this.label2.Size = new Size(lbl2Width, PControlHeight);
this.label2.Visible = this.label1.Visible = true;
}
}
}
图片附件: 游客没有浏览图片的权限,请
登录 或
注册