WPF的成长日记

最近学习wpf编程,学习了让控件跟随窗体的变化而变化。刚开始写还请多见谅!

主要有两种方式,

1,label,image,textbox等单独切少的控件可以根据添加viewbox.

2,如果想要对多控件进行操作,那么就需要先遍历窗体内的控件(注意xaml里面的添加canvas,定位使用canvans.left,canvans.top而不是用margin)cs里面写的代码贴在下面

窗体打开的时候获取初始的尺寸

xvalues = this.ActualWidth;
yvalues = this.ActualHeight;
SetTag(gd_mode.Children);

private void SetTag(UIElementCollection uiControls)
{
typecon[0] = textBoxMaxSet;
typecon[1] = textBox1;
typecon[2] = txt_result;
typecon[3] = textBox2;
typecon[4] = button_Set;

foreach (Control i in typecon)
{
con = i;
con.Tag = con.Width + ":" + con.Height + ":" + Canvas.GetLeft(con) + ":" + Canvas.GetTop(con) + ":" + con.FontSize;
}
}

在窗体Window_SizeChanged里面写上

if (xvalues != 0)
{
double newX = this.ActualWidth / xvalues;
double newY = this.ActualHeight / yvalues;
SetControls(newX, newY, gd_mode.Children);
}

private void SetControls(double newX, double newY, UIElementCollection uiControls)//改变控件的大小
{

foreach (Control i in typecon)
{

con = i;
string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
double a = Convert.ToSingle(mytag[0]) * newX;
con.Width = (int)a;

a = Convert.ToSingle(mytag[1]) * newY;
con.Height = (int)a;

a = Convert.ToSingle(mytag[2]) * newX;
con.SetValue(Canvas.LeftProperty, (double)a);

a = Convert.ToSingle(mytag[3]) * newY;
con.SetValue(Canvas.TopProperty, (double)(a));

double currentSize = Convert.ToSingle(mytag[4]) * newY;
con.FontSize = currentSize;
}

}

猜你喜欢

转载自www.cnblogs.com/xiehaha123/p/11640101.html
今日推荐