WinForm Adaptive screen resolution specific operation and precautions

Source: Internet
Author: User

First step: First with the help of a class file AutoSizeFormClass.cs

Class Autosizeformclass
{
public struct Controlrect
{
public int left;
public int Top;
public int Width;
public int Height;
}
(2). Declaring 1 objects
Note that you cannot use the control list to record list Nctrl; Because of the control's associativity, the record is always the current size.
Public list oldctrl= new list ()///here will be the Latin greater than less than the filter out, can only be changed to Chinese, use to change back to Latin
Public list<controlrect> Oldctrl = new list<controlrect> ();
int ctrlno = 0;//1;
(3). Create two functions
(3.1) record the initial position and size of the form and its controls,
public void Controllinitializesize (Control mform)
{
Controlrect CR;
Cr.left = Mform.left; Cr.top = Mform.top; Cr.width = Mform.width; Cr.height = Mform.height;
Oldctrl.add (CR);//The first one is "form itself", only one time
AddControl (mform);//The remaining controls in the window may also nest controls (such as a panel) and be extracted separately, as they are called recursively
This. WindowState = (System.Windows.Forms.FormWindowState) (2);//After recording the initial position and size of the control, then maximize
0-normalize, 1-minimize,2-maximize
}
private void AddControl (Control ctl)
{
foreach (Control c in CTL.) Controls)
{//** is placed here, the control's child controls are first recorded, and the control itself is recorded
if (C.controls.count > 0)
AddControl (c);//The remaining controls in the window may also nest controls (such as a panel) and be extracted separately, as they are called recursively
Controlrect Objctrl;
Objctrl.left = C.left; Objctrl.top = C.top; Objctrl.width = C.width; Objctrl.height = C.height;
Oldctrl.add (Objctrl);
* * Here, the control itself is recorded before the control's child controls are recorded
if (C.controls.count > 0)
AddControl (c);//The remaining controls in the window may also nest controls (such as a panel) and be extracted separately, as they are called recursively
}
}
(3.2) Control adaptive size,
public void Controlautosize (Control mform)
{
if (Ctrlno = = 0)
{//* If the original size and position of the control are recorded in the Form1_Load of the form, there is no problem, but there is a problem with adding skins, because some controls, such as DataGridView, have not been completed and the number of child controls is small
* To record the original size and position of the control in the form's form1_sizechanged, the first time the size is changed, all the control's child controls have been formed
Controlrect CR;
Cr.left = Mform.left; Cr.top = Mform.top; Cr.width = Mform.width; Cr.height = Mform.height;
Cr.left = 0; cr.top = 0; Cr.width = MForm.PreferredSize.Width; Cr.height = MForm.PreferredSize.Height;

Oldctrl.add (CR);//The first one is "form itself", only one time
AddControl (mform);//The remaining controls within the window may nest other controls (such as a panel), so they are drawn separately for recursive invocation
}
float Wscale = (float) mform.width/(float) oldctrl[0]. width;//the proportions between the old and new forms, with the oldest old form
float Hscale = (float) mform.height/(float) oldctrl[0]. height;//. Height;
Ctrlno = 1;//enters = 1, No. 0 is the form itself, the control in the window, starting with ordinal 1
Autoscalecontrol (Mform, Wscale, Hscale);//The remaining controls in the window may also nest controls (such as a panel) and be extracted separately, as they are called recursively
}
private void Autoscalecontrol (Control ctl, float wscale, float hscale)
{
int ctrLeft0, ctrTop0, CtrWidth0, ctrHeight0;
int Ctrlno = 1;//1th is the left,top,width,height of the form itself, so the form control starts with Ctrlno=1
foreach (Control c in CTL.) Controls)
{//** is placed here, the control's child controls are scaled first, and the control itself is scaled back
if (C.controls.count > 0)
Autoscalecontrol (c, Wscale, Hscale);//The remaining controls in the window may also nest controls (such as a panel) and be extracted separately, as they are called recursively
CtrLeft0 = Oldctrl[ctrlno]. Left;
CtrTop0 = Oldctrl[ctrlno]. Top;
CtrWidth0 = Oldctrl[ctrlno]. Width;
CtrHeight0 = Oldctrl[ctrlno]. Height;
C.left = (int) ((ctrleft0-wleft0) * Wscale) + wleft1;//linear scale between old and new controls
C.top = (int) ((ctrtop0-wtop0) * h) + wTop1;
C.left = (int) ((ctrLeft0) * wscale);//linear scale between old and new controls. The control position is relative to the form, so you cannot add + wLeft1
C.top = (int) ((ctrTop0) * hscale);//
C.width = (int) (CTRWIDTH0 * wscale);//is only related to the initial size, so it cannot be multiplied by the current width (int) (C.width * W);
C.height = (int) (CTRHEIGHT0 * hscale);//
ctrlno++;//Cumulative serial Number
* * Here, it is the child controls that scale the control itself before scaling the control
if (C.controls.count > 0)
Autoscalecontrol (c, Wscale, Hscale);//The remaining controls in the window may also nest controls (such as a panel) and be extracted separately, as they are called recursively

if (CTL is DataGridView)
{
DataGridView DGV = ctl as DataGridView;
Cursor.current = Cursors.waitcursor;

int widths = 0;
for (int i = 0; i < DGV. Columns.count; i++)
{
DGV. AutoResizeColumn (i, datagridviewautosizecolumnmode.allcells); Automatically adjust column widths
Widths + = DGV. Columns[i]. Width; Calculates the width of the cell column after the adjustment column and
}
if (Widths >= ctl. Size.width)//If the width of the adjustment column is greater than the Set column width
DGV. autoSizeColumnsMode = Datagridviewautosizecolumnsmode.displayedcells; Adjust the mode of a column automatically
Else
DGV. autoSizeColumnsMode = Datagridviewautosizecolumnsmode.fill; If it is less than the fill

Cursor.current = Cursors.Default;
}
}
}

}

Step Two:

private void Form_sizechanged (object sender, EventArgs e)
{
Asc.controlautosize (this);
}

private void Form_Load (object sender, EventArgs e)
{

Asc.controllinitializesize (this);

}

Step three: Note some of the settings for the control

Label AutoSize set to True if the parent container panel is set, the AutoSize of the panel is true

Panel/picturebox has background image backgroundimage, BackgroundImageLayout set to Stretch;picturebox if set image, then set SizeMode: StretchImage

WinForm Adaptive screen resolution specific operation and precautions

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.