自动缩放AutoScaleDemo

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));
        }
    }
}

效果图:

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值