C # winform window adaptive resolution class

Source: Internet
Author: User

Recently, when I was working on a project, the developed winform can be normally displayed on my computer. If I share it with another computer, I cannot display it:

1. When the resolution of the two computers is the same, the display cannot be completely displayed. Check whether the autoscalemode attribute of form is none and set it to none.

2. When the resolution is different, you can directly call the initialization function in the form constructor and add AutoReSizeForm. SetFormSize (this); (this is also applicable to user control)

Public class AutoReSizeForm

{
Static float SH
{
Get
{
Return (float) Screen. PrimaryScreen. Bounds. Height/Properties. Settings. Default. Y;
}
}
Static float SW
{
Get
{
Return (float) Screen. PrimaryScreen. Bounds. Width/Properties. Settings. Default. X;
}
}

 


Public static void SetFormSize (Control fm)
{
Fm. Location = new Point (int) (fm. Location. X * SW), (int) (fm. Location. Y * SH ));
Fm. Size = new Size (int) (fm. Size. Width * SW), (int) (fm. Size. Height * SH ));
Fm. font = new Font (fm. font. name, fm. font. size * SH, fm. font. style, fm. font. unit, fm. font. gdiCharSet, fm. font. gdiVerticalFont );
If (fm. Controls. Count! = 0)
{
SetControlSize (fm );
}
}


Private static void SetControlSize (Control InitC)
{
Foreach (Control c in InitC. Controls)
{
C. Location = new Point (int) (c. Location. X * SW), (int) (c. Location. Y * SH ));
C. Size = new Size (int) (c. Size. Width * SW), (int) (c. Size. Height * SH ));
C. font = new Font (c. font. name, c. font. size * SH, c. font. style, c. font. unit, c. font. gdiCharSet, c. font. gdiVerticalFont );
If (c. Controls. Count! = 0)
{
SetControlSize (c );
}
}
}
}

Related Article

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.