Steps:
1. Place a container (such as panel) in the form and set the container's Dock attribute to fill. Other controls in the form are placed in this container.
2. Create a form class that inherits from the original form class and adds the following to the new form class:CodeThe subsequent forms are inherited from the new form class:
# Region Control Scaling
Double Formwidth; // Original form width
Double Formheight; // Original form height
Double Scalex; // Horizontal scaling
Double Scaley; // Vertical Scaling Ratio
Dictionary < String , String > Controlinfo = New Dictionary < String , String > (); // Control center left, top, control width, control height, control font size
/// <Summary>
/// Obtain all raw data
/// </Summary>
Protected Void Getallinitinfo (control crlcontainer)
{
If (Crlcontainer. Parent = This )
{
Formwidth = convert. todouble (crlcontainer. width );
Formheight = convert. todouble (crlcontainer. Height );
}
Foreach (Control item In Crlcontainer. Controls)
{
If (Item. Name. Trim ()! = "" )
Controlinfo. Add (item. Name, (item. Left + item. width/ 2 ) + " , " + (Item. Top + item. Height/ 2 ) + " , " + Item. Width + " , " + Item. height + " , " + Item. Font. size );
If ( (Item As Usercontrol) = Null && Item. Controls. Count> 0 ) Getallinitinfo (item );
}
}
Private Void Controlschangeinit (control crlcontainer)
{
Scalex = (convert. todouble (crlcontainer. width)/formwidth );
Scaley = (convert. todouble (crlcontainer. Height)/formheight );
}
Private Void Controlschange (control crlcontainer)
{
Double [] Pos = New Double [ 5 ]; // The POS array stores the left, top, width, height, and font size of the current control.
Foreach (Control item In Crlcontainer. Controls)
{
If (Item. Name. Trim ()! = "" )
{
If ( (Item As Usercontrol) = Null && Item. Controls. Count> 0 )
Controlschange (item );
String [] STRs = controlinfo [item. Name]. Split ( ' , ' );
For ( Int J = 0 ; J < 5 ; J ++)
{
Pos [J] = convert. todouble (STRs [J]);
}
Double Itemwidth = POS [ 2 ] * Scalex;
Double Itemheight = POS [ 3 ] * Scaley;
Item. Left = convert. toint32 (Pos [ 0 ] * Scalex-itemwidth/ 2 );
Item. Top = convert. toint32 (Pos [ 1 ] * Scaley-itemheight/ 2 );
Item. width = convert. toint32 (itemwidth );
Item. Height = convert. toint32 (itemheight );
Item. font = New Font (item. Font. Name, Float . Parse (Pos [ 4 ] * Math. Min (scalex, scaley). tostring ()));
}
}
}
# Endregion
3. Override onsizechanged in the new form classEvent and callControlschangeinit andThe code for controlschange is as follows:
Protected Override Void Onsizechanged (eventargs E)
{
Base . Onsizechanged (E );
If (Controlinfo. Count> 0 )
{
Controlschangeinit ( This . Controls [ 0 ]);
Controlschange ( This . Controls [ 0 ]);
}
}
4. Call in the form ConstructorThe code for getallinitinfo is as follows:
Getallinitinfo (This. Controls [0]);
Note: original, reprinted, please specify the source.