Code:
using System;
using System.Windows.Forms;
using System.Drawing;
namespace CsDev
{
class AutoScaleDemo : Form
{
public static void Main()
{
Application.Run(new AutoScaleDemo());
}
public AutoScaleDemo()
{
Text = "自动缩放";
Font = new Font("Arial", 12);
FormBorderStyle = FormBorderStyle.FixedSingle;
int[] aiPointSize = { 8, 12, 16, 24, 32 };
for (int i = 0; i < aiPointSize.Length; i++)
{
Button btn = new Button();
btn.Parent = this;
btn.Text = "Use " + aiPointSize[i] + "-point font";
btn.Tag = aiPointSize[i];
btn.Location = new Point(4, 16 + 24 * i);
btn.Size = new Size(80, 16);
btn.Click += new EventHandler(btn_Click);
}
ClientSize = new Size(88, 16 + 24 * aiPointSize.Length);
AutoScaleBaseSize = new Size(4, 8);//自动缩放的基大小
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), 0, 0);
base.OnPaint(e);
}
void btn_Click(object obj, EventArgs e)
{
Button btn = (Button)obj;
SizeF sizefOld = GetAutoScaleSize(Font);
Font = new Font(Font.FontFamily, (int)btn.Tag);
SizeF sizefNew = GetAutoScaleSize(Font);
//缩放
Scale(new SizeF(sizefNew.Width / sizefOld.Width, sizefNew.Height / sizefOld.Height));
}
}
}
效果图: