[C#] 利用Ctrl鍵+滑鼠滾輪動態調整TextBox的字體大小

摘要:[C#] 利用Ctrl鍵+滑鼠滾輪動態調整TextBox的字體大小



private void Form1_Load(object sender, EventArgs e)
{
    this.label1.Text = this.textBox1.Font.Size.ToString();

    this.textBox1.MouseWheel += new MouseEventHandler(this.textBox1_MouseWheel);
}

private void textBox1_MouseWheel(object sender, MouseEventArgs e)
{
    if (Control.ModifierKeys == Keys.Control)
    {
        FontStyle fs = this.textBox1.Font.Style;
        FontFamily fm = new FontFamily(this.textBox1.Font.Name);
        float em = this.textBox1.Font.Size;

        if (e.Delta > 0)
        {
            em++;

        }
        else
        {
            em--;
            if (em <= 0)
            {
                em = 1;
            }
        }
        Font f = new Font(fm, em, fs);
        this.textBox1.Font = f;
        this.label1.Text = this.textBox1.Font.Size.ToString();
    }
}
專案檔下載