創(chuàng)建C#自定義控件:旋轉(zhuǎn)按鈕
在C#中,自定義控件可以幫助開(kāi)發(fā)者根據(jù)特定需求擴(kuò)展標(biāo)準(zhǔn)控件的功能。本文將介紹如何創(chuàng)建一個(gè)旋轉(zhuǎn)按鈕(Rotary Button),這種按鈕在用戶點(diǎn)擊或拖動(dòng)時(shí)能夠旋轉(zhuǎn),并觸發(fā)特定事件。我們將使用Windows Forms來(lái)實(shí)現(xiàn)這一控件。

一、環(huán)境準(zhǔn)備
- Visual Studio 2019或更高版本
 - .NET Framework 4.7.2或更高版本
 
二、創(chuàng)建自定義控件項(xiàng)目
- 打開(kāi)Visual Studio,創(chuàng)建一個(gè)新的“Windows Forms 控件庫(kù)”項(xiàng)目,命名為 RotaryButtonLibrary。
 - 在項(xiàng)目中添加一個(gè)新的“用戶控件”,命名為 RotaryButton.cs。
 
三、設(shè)計(jì)旋轉(zhuǎn)按鈕
1. 修改RotaryButton.cs的設(shè)計(jì)
首先,我們需要讓我們的控件繼承自UserControl,并在設(shè)計(jì)視圖中進(jìn)行一些基本設(shè)置。
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace RotaryButtonLibrary
{
    public partial class RotaryButton : UserControl
    {
        private Timer timer;
        private float angle = 0;
        private bool isRotating = false;
        public RotaryButton()
        {
            InitializeComponent();
            this.DoubleBuffered = true; // 防止重畫閃爍
            this.Size = new Size(100, 100); // 默認(rèn)大小
            this.BackColor = Color.LightGray; // 默認(rèn)背景色
            this.Cursor = Cursors.Hand; // 鼠標(biāo)指針樣式
            // 初始化計(jì)時(shí)器
            timer = new Timer();
            timer.Interval = 10; // 每10ms觸發(fā)一次
            timer.Tick += Timer_Tick;
        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            if (isRotating)
            {
                angle += 5; // 每次增加5度
                if (angle >= 360) angle = 0;
                this.Invalidate(); // 觸發(fā)重繪
            }
        }
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            Graphics g = pe.Graphics;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            // 繪制旋轉(zhuǎn)后的按鈕外觀,這里簡(jiǎn)單用一個(gè)矩形表示
            g.TranslateTransform(this.Width / 2, this.Height / 2); // 將旋轉(zhuǎn)中心點(diǎn)移動(dòng)到控件中心
            g.RotateTransform(angle); // 按指定角度旋轉(zhuǎn)
            g.FillRectangle(Brushes.Blue, -this.Width / 2, -this.Height / 2, this.Width, this.Height); // 繪制按鈕
            g.ResetTransform(); // 重置變換
        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            isRotating = true;
            timer.Start();
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            isRotating = false;
            timer.Stop();
        }
    }
}2. 添加控件到工具箱
構(gòu)建項(xiàng)目后,RotaryButton控件會(huì)自動(dòng)出現(xiàn)在工具箱中。如果沒(méi)有,可以右鍵工具箱,選擇“選擇項(xiàng)...”,然后在“瀏覽”中添加生成的DLL文件。
3. 使用旋轉(zhuǎn)按鈕
創(chuàng)建一個(gè)新的Windows Forms應(yīng)用程序,從工具箱中拖出RotaryButton控件并放到窗體上。運(yùn)行程序,你會(huì)看到點(diǎn)擊按鈕時(shí),按鈕開(kāi)始旋轉(zhuǎn),松開(kāi)鼠標(biāo)按鈕時(shí),旋轉(zhuǎn)停止。
四、擴(kuò)展功能
為了讓旋轉(zhuǎn)按鈕更加實(shí)用,我們可以添加一些額外屬性和事件。例如,可以提供一個(gè)RotationSpeed屬性來(lái)控制旋轉(zhuǎn)速度,或者提供一個(gè)RotationCompleted事件來(lái)在旋轉(zhuǎn)一圈完成時(shí)觸發(fā)。
1. 添加RotationSpeed屬性
private int rotationSpeed = 5; // 默認(rèn)旋轉(zhuǎn)速度
[Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
public int RotationSpeed
{
    get { return rotationSpeed; }
    set { rotationSpeed = value; }
}修改Timer_Tick方法以使用rotationSpeed:
private void Timer_Tick(object sender, EventArgs e)
{
    if (isRotating)
    {
        angle += rotationSpeed; // 使用RotationSpeed
        if (angle >= 360) 
        {
            angle = 0;
            OnRotationCompleted(EventArgs.Empty); // 觸發(fā)旋轉(zhuǎn)完成事件
        }
        this.Invalidate(); // 觸發(fā)重繪
    }
}2. 添加RotationCompleted事件
public event EventHandler RotationCompleted;
protected virtual void OnRotationCompleted(EventArgs e)
{
    RotationCompleted?.Invoke(this, e);
}五、完整代碼示例
以下是完整的RotaryButton.cs代碼:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace RotaryButtonLibrary
{
    public partial class RotaryButton : UserControl
    {
        private Timer timer;
        private float angle = 0;
        private bool isRotating = false;
        private int rotationSpeed = 5; // 默認(rèn)旋轉(zhuǎn)速度
        public RotaryButton()
        {
            InitializeComponent();
            this.DoubleBuffered = true; // 防止重畫閃爍
            this.Size = new Size(100, 100); // 默認(rèn)大小
            this.BackColor = Color.LightGray; // 默認(rèn)背景色
            this.Cursor = Cursors.Hand; // 鼠標(biāo)指針樣式
            // 初始化計(jì)時(shí)器
            timer = new Timer();
            timer.Interval = 10; // 每10ms觸發(fā)一次
            timer.Tick += Timer_Tick;
        }
        [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
        public int RotationSpeed
        {
            get { return rotationSpeed; }
            set { rotationSpeed = value; }
        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            if (isRotating)
            {
                angle += rotationSpeed; // 使用RotationSpeed
                if (angle >= 360) 
                {
                    angle = 0;
                    OnRotationCompleted(EventArgs.Empty); // 觸發(fā)旋轉(zhuǎn)完成事件
                }
                this.Invalidate(); // 觸發(fā)重繪
            }
        }
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            Graphics g = pe.Graphics;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            // 繪制旋轉(zhuǎn)后的按鈕外觀,這里簡(jiǎn)單用一個(gè)矩形表示
            g.TranslateTransform(this.Width / 2, this.Height / 2); // 將旋轉(zhuǎn)中心點(diǎn)移動(dòng)到控件中心
            g.RotateTransform(angle); // 按指定角度旋轉(zhuǎn)
            g.FillRectangle(Brushes.Blue, -this.Width / 2, -this.Height / 2, this.Width, this.Height); // 繪制按鈕
            g.ResetTransform(); // 重置變換
        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            isRotating = true;
            timer.Start();
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            isRotating = false;
            timer.Stop();
        }
        public event EventHandler RotationCompleted;
        protected virtual void OnRotationCompleted(EventArgs e)
        {
            RotationCompleted?.Invoke(this, e);
        }
    }
}六、總結(jié)
通過(guò)本文,我們了解了如何創(chuàng)建一個(gè)基本的旋轉(zhuǎn)按鈕自定義控件。你可以根據(jù)實(shí)際需求進(jìn)一步擴(kuò)展這個(gè)控件的功能,例如添加更多的外觀定制選項(xiàng),處理不同的鼠標(biāo)事件,或者集成動(dòng)畫效果。自定義控件在復(fù)雜用戶界面和交互式應(yīng)用程序中非常有用,希望這篇文章對(duì)你有所幫助。















 
 
 
 
 
 
 