C # scale the winform control with the form

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.