Clever Use of C # to achieve free Transformation of All Form Controls following the form size

Source: Internet
Author: User
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. collections;
Namespace windowsapplication3
{
Public partial class form1: Form
{
/******************** Program Dynamic Array used to store initial data and related private variables ************************ ******/
Private arraylist initialcrl = new arraylist (); // used to store the names of all controls in the form
Private arraylist crllocationx = new arraylist (); // used to store the original location of all controls in the form
Private arraylist crllocationy = new arraylist (); // used to store the original location of all controls in the form
Private arraylist crlsizewidth = new arraylist (); // used to store the original horizontal size of all controls in the form
Private arraylist crlsizeheight = new arraylist (); // used to store the original vertical size of all controls in the form
Private int formsizewidth; // used to store the original horizontal size of the form
Private int formsizeheight; // used to store the original vertical size of the form
Private double formsizechangedx; // used to store the horizontal variation of the relevant parent form/Container
Private double formsizechangedy; // used to store the vertical variation of the relevant parent form/Container
Private int wcounter = 0; // to prevent confusion during recursive traversal, A Global Counter is specially set.
/*************************************** **************************************** *********************************/
Public form1 ()
{
Initializecomponent ();
}
Private void form1_load (Object sender, eventargs E)
{
Getinitialformsize ();
// This. autoscroll = true;
// This. setautosizemode (formsizewidth, formsizeheight );
// This. autoscrollminsize. width = formsizewidth;
// This. autoscrollminsize. Height = formsizeheight;
Getallcrllocation (this );
Getallcrlsize (this );
}
Public void getallcrllocation (control crlcontainer) // obtain and store the initial position of each control in the form
{
Foreach (control icrl in crlcontainer. Controls)
{
If (icrl. Controls. Count> 0)
Getallcrllocation (icrl );
Initialcrl. Add (icrl );
Crllocationx. Add (icrl. Location. X );
Crllocationy. Add (icrl. Location. y );
}
}
Public void getallcrlsize (control crlcontainer) // obtain and store the initial size of each control in the form
{
Foreach (control icrl in crlcontainer. Controls)
{
If (icrl. Controls. Count> 0)
Getallcrlsize (icrl );
Crlsizewidth. Add (icrl. width );
Crlsizeheight. Add (icrl. Height );
}
}
Public void getinitialformsize () // obtain and store the initial size of the form
{
Formsizewidth = This. Size. width;
Formsizeheight = This. Size. height;
}
Private void form1_sizechanged (Object sender, eventargs E)
{
// MessageBox. Show (" ");
Wcounter = 0;
Int counter = 0;
If (this. Size. width <formsizewidth | this. Size. height <formsizeheight)
// If the size of the form is smaller than the initial value of the form size, the controls in the form are automatically reset to the initial size, and the form is automatically added with the scroll bar
{
Foreach (control inicrl in initialcrl)
{
Inicrl. width = (INT) crlsizewidth [Counter];
Inicrl. Height = (INT) crlsizeheight [Counter];
Point point = new point ();
Point. x = (INT) crllocationx [Counter];
Point. Y = (INT) crllocationy [Counter];
Inicrl. bounds = new rectangle (point, inicrl. size );
Counter ++;
}
This. autoscroll = true;
}
Else
// Otherwise, reset the size of all controls in the form (the size of all controls in the form varies with the size of the form)
{
This. autoscroll = false;
Resetallcrlstate (this );
}
}
Public void resetallcrlstate (control crlcontainer) // reset the status of each control in the form (calculated in comparison with the original status)
{
Formsizechangedx = (double) This. Size. width/(double) formsizewidth;
Formsizechangedy = (double) This. Size. Height/(double) formsizeheight;
Foreach (control kcrl in crlcontainer. Controls)
{
/* String name = kcrl. Name. tostring ();
MessageBox. Show (name );
MessageBox. Show (wcounter. tostring ());*/
If (kcrl. Controls. Count> 0)
{
Resetallcrlstate (kcrl );
}
Point point = new point ();
Point. x = (INT) crllocationx [wcounter] * formsizechangedx );
Point. Y = (INT) crllocationy [wcounter] * formsizechangedy );
Kcrl. width = (INT) crlsizewidth [wcounter] * formsizechangedx );
Kcrl. Height = (INT) crlsizeheight [wcounter] * formsizechangedy );
Kcrl. bounds = new rectangle (point, kcrl. size );
Wcounter ++;
// MessageBox. Show (wcounter. tostring ());
}
}
}
}

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.