1. 程式人生 > >C# GDI 控制元件重繪

C# GDI 控制元件重繪

最近有個小需求,就是將checkbox選擇框畫大一點,網上找了點資料整理了下,程式碼如下。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsFormsApp11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.checkBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);


        }
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
           
            if (checkBox1.CheckState == CheckState.Unchecked)
            {
                ControlPaint.DrawCheckBox(e.Graphics, new Rectangle(40,80, 30, 30), ButtonState.Normal);

            }
            else
            {
                ControlPaint.DrawCheckBox(e.Graphics, new Rectangle(40, 80, 30, 30), ButtonState.Checked);

            }

        }
        private void panel2_Paint(object sender, PaintEventArgs e)
        {
            


        }
        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = checkBox1.CheckState.ToString();
        }

        
    }
}