界面缩放后,如何使在界面里的button按钮也随着变化大小和位置!!
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
/// <summary>
/// 0:界面1;1:界面2;2:界面3;
/// </summary>
private int _UIFlag = 0;
/// <summary>
/// 刷新界面控件
/// </summary>
private void RefreshUI()
{
if (_UIFlag == 0)
{
btn_Enter.Visible = true;
btn_Office.Visible = false;
btn_Name.Visible = false;
btn_Num.Visible = false;
}
else if (_UIFlag == 1)
{
btn_Enter.Visible = false;
btn_Office.Visible = true;
btn_Name.Visible = true;
btn_Num.Visible = true;
}
}
/// <summary>
/// 刷新界面控件大小和位置
/// </summary>
private void RefreshUIPosition()
{ //界面缩放后,button的位置和大小随之改变
}
private void btn_Enter_Click(object sender, EventArgs e)
{
this.BackgroundImage = System.Drawing.Image.FromFile(@"F:\image\版面-02.jpg");
_UIFlag = 1;
RefreshUI();
}
private void Form4_ClientSizeChanged(object sender, EventArgs e)
{
RefreshUIPosition();
}
求大神,这是部分代码。。主要想实现改变了背景大小后,怎么使背景上的button按钮也会改变大小。。。
------解决方案--------------------public void AutoScales(Form frm)
{
frm.Tag = frm.Width.ToString() + "," + frm.Height.ToString();
frm.SizeChanged += new EventHandler(frmScreen_SizeChanged);
}
private void frmScreen_SizeChanged(object sender, EventArgs e)
{
if (flage == 1)
{
flage++;
string[] tmp = ((Form)sender).Tag.ToString().Split(',');
float width = (float)((Form)sender).Width / (float)Convert.ToInt16(tmp[0]);
float heigth = (float)((Form)sender).Height / (float)Convert.ToInt16(tmp[1]);
((Form)sender).Tag = ((Form)sender).Width.ToString() + "," + ((Form)sender).Height;
foreach (Control control in ((Form)sender).Controls)
{
control.Scale(new SizeF(width, heigth));
}
}
}
------解决方案--------------------
代码来了,亲测可行
C# code
private void Form1_SizeChanged(object sender, EventArgs e)
{
float x = this.Width / oldsize.Width; //oldsize为窗口原来大小
float y = this.Height / oldsize.Height;
foreach (Control ctr in this.Controls)
{
if (ctr.GetType() == typeof(Button))
{
ctr.Left = (int)(ctr.Location.X * x);
ctr.Top = (int)(ctr.Location.Y * y);
ctr.Width = (int)(ctr.Width * x);
ctr.Height = (int)(ctr.Height * y);
}
oldsize = this.Size;
}
}