C#自定義控件旋轉(zhuǎn)按鈕功能
C#用戶控件之旋轉(zhuǎn)按鈕
按鈕功能:手自動(dòng)旋轉(zhuǎn),標(biāo)簽文本顯示、點(diǎn)擊二次彈框確認(rèn)(源碼在最后邊);
【制作方法】
- 找到控件的中心坐標(biāo),畫(huà)背景外環(huán)、內(nèi)圓;再繪制矩形開(kāi)關(guān),進(jìn)行角度旋轉(zhuǎn)即可獲得;
【關(guān)鍵節(jié)點(diǎn)】
- No.1 獲取中心坐標(biāo),思考要繪制圖形的相對(duì)坐標(biāo)、寬度、高度;
- No.2 更改坐標(biāo)系原點(diǎn),以此原點(diǎn)為坐標(biāo),繪制矩形開(kāi)關(guān),再旋轉(zhuǎn)指定角度
//方法中獲取原點(diǎn) Point centerPoint = GetCenterPoint(); #region 獲取中心原點(diǎn) 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
//更改坐標(biāo)系原點(diǎn) g.TranslateTransform(centerPoint.X, centerPoint.Y); //旋轉(zhuǎn)指定角度 if (switchStatus) { g.RotateTransform(36.0f); } else { g.RotateTransform(-36.0f); }
【1】按鈕的背景(外環(huán)<g.DrawEllipse>、內(nèi)圓<g.FillEllipse>)繪制方法與指示燈的方法一樣;
注意:此坐標(biāo)系以控件左上角為準(zhǔn)
//繪制外環(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); //填充內(nèi)圓—(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】繪制中間矩形及圓點(diǎn),畫(huà)刷填充指定區(qū)域(g.FillRectangle、g.FillEllipse)
注意:此坐標(biāo)系以中心點(diǎn)為準(zhǔn)
//更改坐標(biāo)系原點(diǎn) g.TranslateTransform(centerPoint.X, centerPoint.Y); //填充矩形開(kāi)關(guān) rec = new RectangleF(-this.togWidth * 0.5f, this.togGap - centerPoint.Y, togWidth, (centerPoint.Y - togGap) * 2); g.FillRectangle(new SolidBrush(this.togColor), rec); //填充矩形開(kāi)關(guān)圓點(diǎn) 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)建鼠標(biāo)點(diǎn)擊事件,添加鼠標(biāo)點(diǎn)擊事件處理<更改屬性值>,在屬性中觸發(fā)事件(Event)
#region 添加事件 [Browsable(true)] [Category("操作_G")] [Description("雙擊進(jìn)入事件")] public event EventHandler MouseDown_G; //事件聲明
//初始化函數(shù)添加鼠標(biāo)點(diǎn)擊事件處理 this.MouseDown += Switch_MouseDown; ;
//鼠標(biāo)點(diǎn)擊事件處理邏輯 private void Switch_MouseDown(object sender, MouseEventArgs e) { DialogResult dr = MessageBox.Show("二次確認(rèn)操作?", "提示您", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (dr == DialogResult.OK) { SwitchStatus = !SwitchStatus; //此處屬性值,不是字段 } else return; } #endregion
//開(kāi)關(guān)狀態(tài)屬性 private bool switchStatus = false; [Browsable(true)] [Category("布局_G")] [Description("開(kāi)關(guān)狀態(tài)")] public bool SwitchStatus { get { return switchStatus; } set { switchStatus = value; this.Invalidate(); //激活觸發(fā)事件 this.MouseDown_G?.Invoke(this, null); } }
備忘:指定默認(rèn)事件(在應(yīng)用時(shí)點(diǎn)擊鼠標(biāo)即可進(jìn)入自定義事件,否則進(jìn)入‘load’事件)
[DefaultEvent("MouseDown_G")]
最后生成
下一個(gè):一個(gè)標(biāo)題面板,方便用戶界面的布局
【1】新建用戶組件
【2】更改組件繼承為Panel
【3】定義屬性(標(biāo)題的顏色、字體、高度;抬頭背景色;邊框顏色)
private Font titleFont = new Font("微軟雅黑", 12); [Browsable(true)] [Category("布局_G")] [Description("標(biāo)題字體")] public Font TitleFont { get { return titleFont; } set { titleFont = value; this.Invalidate(); } }
【4】重繪畫(huà)布
//畫(huà)外邊框 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】備注說(shuō)明
- 初始化字體格式-需要再兩個(gè)方法中定義文本對(duì)齊格式
//字體對(duì)齊格式 this.sf = new StringFormat(); this.sf.Alignment = StringAlignment.Center; this.sf.LineAlignment = StringAlignment.Center; //指定控件大小 this.Size = new System.Drawing.Size(300, 150);
最后生成并應(yīng)用
源碼鏈接
到此這篇關(guān)于C#自定義控件旋轉(zhuǎn)按鈕的文章就介紹到這了,更多相關(guān)C#旋轉(zhuǎn)按鈕內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
那些年,我還在學(xué)習(xí)C# 學(xué)習(xí)筆記續(xù)
那些年學(xué)習(xí)C#,就是對(duì)C#相關(guān)的一些知識(shí)有一個(gè)了解,等到要用時(shí)才不會(huì)找不到方向,比如說(shuō)擴(kuò)展方法,開(kāi)始時(shí)怎么覺(jué)得沒(méi)有用,后來(lái)了解到asp.net MVC,它可以用來(lái)擴(kuò)展Html類,比如做一個(gè)分頁(yè)的方法;所以對(duì)一門(mén)語(yǔ)言了解寬一些是沒(méi)有壞處的2012-03-03C#中WPF顏色對(duì)話框控件的實(shí)現(xiàn)
在 C# WPF開(kāi)發(fā)中顏色對(duì)話框控件(ColorDialog)用于對(duì)界面中的背景、文字…(擁有顏色屬性的所有控件)設(shè)置顏色,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03C# 讀取ttf字體文件里的Unicode實(shí)現(xiàn)
這篇文章主要介紹了C# 讀取 ttf字體文件里的 Unicode實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09