WinForm, how to control the position of a control changes with the size of the form There are 3 ways to do this:
Method 1
[CSharp]View Plaincopy
- Using System;
- Using System.Collections.Generic;
- Using System.ComponentModel;
- Using System.Data;
- Using System.Drawing;
- Using System.Text;
- Using System.Windows.Forms;
- Namespace Markprinter
- {
- Public partial class Resizetest:form
- {
- public float X;
- public float Y;
- //public float y;
- Public resizetest ()
- {
- InitializeComponent ();
- }
- private void Settag (Control cons)
- {
- foreach (Control con in cons. Controls)
- {
- Con. Tag = con. Width + ":" + con. Height + ":" + con. Left + ":" + con. Top + ":" + con. Font.Size;
- if (con. Controls.Count > 0)
- Settag (con);
- }
- }
- private void Setcontrols (float newx, float newy, Control cons)
- {
- foreach (Control con in cons. Controls)
- {
- string[] MyTag = con. Tag.tostring (). Split (new char[] { ': '});
- float 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. left = (int) (a);
- A = Convert.tosingle (mytag[3]) * NEWY;
- Con. Top = (int) (a);
- Single currentsize = Convert.tosingle (mytag[4]) * NEWY;
- //Change font size
- Con. Font = new Font (con. Font.Name, CurrentSize, con. Font.style, Con. Font.unit);
- if (con. Controls.Count > 0)
- {
- Setcontrols (newx, newy, con);
- }
- }
- }
- void Form1_Resize (object sender, EventArgs e)
- {
- //throw new Exception ("The method or operation is not implemented.");
- float newx = (this. Width)/X;
- //Float Newy = (this. Height-this.statusstrip1.height)/(Y-Y);
- float Newy = this . height/y;
- Setcontrols (Newx, Newy, this );
- This . Text = This . Width.tostring () + "" + This . Height.tostring ();
- }
- private void Resizetest_load (object sender, EventArgs e)
- {
- This . Resize + = new EventHandler (form1_resize);
- X = This . Width;
- Y = This . Height;
- //y = this.statusStrip1.Height;
- Settag (this);
- }
- }
- }
Method 2
[CSharp]View Plaincopy
- Using System;
- Using System.Collections.Generic;
- Using System.ComponentModel;
- Using System.Data;
- Using System.Drawing;
- Using System.Text;
- Using System.Windows.Forms;
- Namespace Markprinter
- {
- Public partial class Resizetest:form
- {
- public float X;
- public float Y;
- public float y;
- Public resizetest ()
- {
- InitializeComponent ();
- }
- private void Resizetest_load (object sender, EventArgs e)
- {
- Autoscale (this);
- }
- // <summary>
- /// controls automatically scaled with the form
- // </summary>
- /// <param name= "frm" ></param>
- public static void Autoscale (Form frm)
- {
- frm. Tag = frm. Width.tostring () + "," + frm. Height.tostring ();
- frm. SizeChanged + = new EventHandler (frm_sizechanged);
- }
- static void frm_sizechanged (object sender, EventArgs e)
- {
- 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));
- }
- }
- }
- }
-
- --Reprint Office: http://blog.csdn.net/xd43100678/article/details/7899047
WinForm, how to control the position of a control changes with the size of the form