C#自定義控件旋轉按鈕功能
C#用戶控件之旋轉按鈕
按鈕功能:手自動旋轉,標簽文本顯示、點擊二次彈框確認(源碼在最后邊);

【制作方法】
- 找到控件的中心坐標,畫背景外環(huán)、內圓;再繪制矩形開關,進行角度旋轉即可獲得;
【關鍵節(jié)點】
- No.1 獲取中心坐標,思考要繪制圖形的相對坐標、寬度、高度;
- No.2 更改坐標系原點,以此原點為坐標,繪制矩形開關,再旋轉指定角度
//方法中獲取原點
Point centerPoint = GetCenterPoint();
#region 獲取中心原點
private Point GetCenterPoint()
{
if (this.height > this.width)
{
return new Point(this.width / 2, this.width / 2);
}
else
{
return new Point(this.height / 2, this.height / 2);
}
}
#endregion//更改坐標系原點
g.TranslateTransform(centerPoint.X, centerPoint.Y);
//旋轉指定角度
if (switchStatus)
{
g.RotateTransform(36.0f);
}
else
{
g.RotateTransform(-36.0f);
}【1】按鈕的背景(外環(huán)<g.DrawEllipse>、內圓<g.FillEllipse>)繪制方法與指示燈的方法一樣;
注意:此坐標系以控件左上角為準
//繪制外環(huán)—(Pen)-DrawEllipse p = new Pen(this.cirInColor, this.cirOutWidth); RectangleF rec = new RectangleF(this.cirOutGap, this.cirOutGap, (centerPoint.X - this.cirOutGap) * 2, (centerPoint.X - this.cirOutGap) * 2); g.DrawEllipse(p, rec); //填充內圓—(SolidBrush)-FillEllipse sb = new SolidBrush(this.cirInColor); rec = new RectangleF(this.cirInGap, this.cirInGap, (centerPoint.X - this.cirInGap) * 2, (centerPoint.X - this.cirInGap) * 2); g.FillEllipse(sb, rec);
【2】繪制中間矩形及圓點,畫刷填充指定區(qū)域(g.FillRectangle、g.FillEllipse)
注意:此坐標系以中心點為準
//更改坐標系原點 g.TranslateTransform(centerPoint.X, centerPoint.Y); //填充矩形開關 rec = new RectangleF(-this.togWidth * 0.5f, this.togGap - centerPoint.Y, togWidth, (centerPoint.Y - togGap) * 2); g.FillRectangle(new SolidBrush(this.togColor), rec); //填充矩形開關圓點 rec = new RectangleF(-this.togWidth * 0.5f + togForeGap, this.togGap - centerPoint.Y + togForeGap, togWidth - 2 * togForeGap, togForeHeight); g.FillEllipse(new SolidBrush(this.togForeColor), rec);
【3】繪制文本,在指定的矩形中繪制指定的字符串(g.DrawString)
//指定字符串 rec = new RectangleF(this.width * 0.05f, 1, this.width, 20); g.DrawString(this.textLeft, this.textFont, new SolidBrush(this.textColor), rec, sf); rec = new RectangleF(this.width * 0.63f, 1, this.width, 20); g.DrawString(this.textRight, this.textFont, new SolidBrush(this.textColor), rec, sf);
【4】創(chuàng)建鼠標點擊事件,添加鼠標點擊事件處理<更改屬性值>,在屬性中觸發(fā)事件(Event)
#region 添加事件
[Browsable(true)]
[Category("操作_G")]
[Description("雙擊進入事件")]
public event EventHandler MouseDown_G; //事件聲明
//初始化函數(shù)添加鼠標點擊事件處理 this.MouseDown += Switch_MouseDown; ;
//鼠標點擊事件處理邏輯
private void Switch_MouseDown(object sender, MouseEventArgs e)
{
DialogResult dr = MessageBox.Show("二次確認操作?", "提示您", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (dr == DialogResult.OK)
{
SwitchStatus = !SwitchStatus; //此處屬性值,不是字段
}
else return;
}
#endregion//開關狀態(tài)屬性
private bool switchStatus = false;
[Browsable(true)]
[Category("布局_G")]
[Description("開關狀態(tài)")]
public bool SwitchStatus
{
get { return switchStatus; }
set
{
switchStatus = value; this.Invalidate();
//激活觸發(fā)事件
this.MouseDown_G?.Invoke(this, null);
}
}備忘:指定默認事件(在應用時點擊鼠標即可進入自定義事件,否則進入‘load’事件)
[DefaultEvent("MouseDown_G")]最后生成

下一個:一個標題面板,方便用戶界面的布局

【1】新建用戶組件

【2】更改組件繼承為Panel

【3】定義屬性(標題的顏色、字體、高度;抬頭背景色;邊框顏色)
private Font titleFont = new Font("微軟雅黑", 12);
[Browsable(true)]
[Category("布局_G")]
[Description("標題字體")]
public Font TitleFont
{
get { return titleFont; }
set
{
titleFont = value;
this.Invalidate();
}
}【4】重繪畫布
//畫外邊框 g.DrawRectangle(new Pen(this.colorBorder), new Rectangle(0, 0, this.Width - 1, this.Height - 1)); //填充抬頭矩形 RectangleF rec = new RectangleF(0.5f, 0.5f, this.Width - 2, this.titleHeight); g.FillRectangle(new SolidBrush(this.colorBack), rec); //文本繪制 g.DrawString(this.titleText, this.titleFont, new SolidBrush(this.colorTitle), rec, sf);
【5】備注說明
- 初始化字體格式-需要再兩個方法中定義文本對齊格式
//字體對齊格式 this.sf = new StringFormat(); this.sf.Alignment = StringAlignment.Center; this.sf.LineAlignment = StringAlignment.Center; //指定控件大小 this.Size = new System.Drawing.Size(300, 150);
最后生成并應用

源碼鏈接

到此這篇關于C#自定義控件旋轉按鈕的文章就介紹到這了,更多相關C#旋轉按鈕內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

