WPF's growth diary

Recent study wpf programming, learning to let the control to follow the form of changes. Just started writing also like him to forgive me!

There are two main ways,

1, label, image, textbox, alone cut control may add less viewbox.

2, if you want to control the operation of the multi, you will need to traverse the window controls the body (note add xaml inside the canvas, positioning using canvans.left, canvans.top rather than margin) to write the code attached to the inside cs below

Get the initial size of the form when opened

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;
}
}

Written on the inside in the form 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) // change the size of the control
{

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;
}

}

 

Guess you like

Origin www.cnblogs.com/xiehaha123/p/11640101.html