When we change the font of the system to a large font size, if the text and control on our form do not grow, it will make our interface very ugly. Fortunately, this function has been provided in winform and is easy to use. You only need to set the font of the form as the system font in the form load event.
The result is as follows: (when the system font is too large)
After modification:
Before modification:
Three related Code 1: using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Namespace winformautoscale
This. style. Display = 'none'; codehighlighter=193_455_open_text.style.display = 'none'; inline = 'inline'; "src =" http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif "align = top> {
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}
Private void form1_load (Object sender, eventargs E)
{
This. font = systemfonts. icontitlefont;
}
}
}
Code 2: namespace winformautoscale
{
Partial class form1
{
/** // <Summary>
/// Required designer variable.
/// </Summary>
Private system. componentmodel. icontainer components = NULL;
/** // <Summary>
/// Clean up any resources being used.
/// </Summary>
/// <Param name = "disposing"> true if managed resources shocould be disposed; otherwise, false. </param>
Protected override void dispose (bool disposing)
{
If (disposing & (components! = NULL ))
{
Components. Dispose ();
}
Base. Dispose (disposing );
}
Windows Form Designer generated code # region windows Form Designer generated code
/** // <Summary>
/// Required method for designer support-do not modify
/// The contents of this method with the code editor.
/// </Summary>
Private void initializecomponent ()
{
System. componentmodel. componentresourcemanager resources = new system. componentmodel. componentresourcemanager (typeof (form1 ));
This. label1 = new system. Windows. Forms. Label ();
This. button1 = new system. Windows. Forms. Button ();
This. suspendlayout ();
//
// Label1
//
Resources. applyresources (this. label1, "label1 ");
This. label1.borderstyle = system. Windows. Forms. borderstyle. fixedsingle;
This. label1.name = "label1 ";
//
// Button1
//
Resources. applyresources (this. button1, "button1 ");
This. button1.name = "button1 ";
This. button1.usevisualstylebackcolor = true;
//
// Form1
//
Resources. applyresources (this, "$ this ");
This. autoscalemode = system. Windows. Forms. autoscalemode. Font;
This. Controls. Add (this. button1 );
This. Controls. Add (this. label1 );
This. Name = "form1 ";
This. Load + = new system. eventhandler (this. form#load );
This. resumelayout (false );
This. initialize mlayout ();
}
# Endregion
Private system. Windows. Forms. Label label1;
Private system. Windows. Forms. Button button1;
}
}
(Note the settings of the autoscalemode attribute of form in the code above)
Iv. help: the dialog in MFC does not provide similar functions. To achieve automatic scaling of the dialog, the texts and controls on the dialog must be scaled according to the system font size, it is also related to fonts. There is a good way. Thank you!
Five references:
Http://www.cppblog.com/mzty/archive/2006/11/13/15131.html
Http://msdn2.microsoft.com/en-us/library/ms229605.aspx