? ? ? ??在開(kāi)始閱讀本文之前,如果您有學(xué)習(xí)創(chuàng)建自定義控件庫(kù)并在其他項(xiàng)目中引用的需求,請(qǐng)參考:在Visual Studio中創(chuàng)建自定義Winform控件庫(kù)并在其他解決方案中引用https://blog.csdn.net/YMGogre/article/details/126508042
目錄
1、應(yīng)用場(chǎng)景:?
1.1、本文的應(yīng)用場(chǎng)景:?
2、所需資源:?
3、源代碼:?
4、使用方法:?
5、效果演示:
1、應(yīng)用場(chǎng)景:?
- 當(dāng)我們需要在窗口上通過(guò)一個(gè)指示燈來(lái)表示一個(gè)硬件設(shè)備或者程序的運(yùn)行狀態(tài)時(shí);
- 當(dāng)我們需要一個(gè)可以顯示不同顏色的指示燈來(lái)表征硬件設(shè)備或程序不同的狀態(tài)時(shí);
- 當(dāng)我們需要指示燈可以對(duì)用戶的"點(diǎn)擊"之類(lèi)的交互做出響應(yīng)時(shí);?
1.1、本文的應(yīng)用場(chǎng)景:?
? ? ? ? 此控件設(shè)計(jì)初衷是:采用一個(gè)四色指示燈來(lái)表示某件事情的執(zhí)行狀態(tài)。共設(shè)置有四個(gè)狀態(tài):"waiting(等待中)"、"underway(正在進(jìn)行)"、"completed(已完成)"、"pick(選中)"。分別通過(guò)四種顏色來(lái)表征:"DimGray(暗灰色)"、"Cornsilk(玉米絲色)"、"Aquamarine(碧綠色)"、"Coral(珊瑚色)"。
????????此外,如果我們對(duì)某些"已完成"的事件不滿意,我們可以選中那些事件對(duì)應(yīng)的指示燈,以方便程序?qū)ξ覀冞x中的事件做后續(xù)操作:
????????我們可以通過(guò)鼠標(biāo)點(diǎn)擊選中"已完成"狀態(tài)下的指示燈,此時(shí)指示燈狀態(tài)會(huì)改為"選中",當(dāng)然也可以取消選中;而其他狀態(tài)下的指示燈無(wú)法被選中。?
2、所需資源:?
(無(wú))?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-444471.html
3、源代碼:?
/* IndicatorLight.cs */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//注意命名空間修改為自己項(xiàng)目的命名空間
namespace WindowsFormsControlLibraryMadeByXJY
{
public partial class IndicatorLight : UserControl
{
private bool _Light_clickable = true;
private string _Light_text = "";
private int Curr_StatusCode; //當(dāng)前狀態(tài)編碼
private enum Light_States
{
waiting = 0,
underway,
completed,
pick
}
private string[] Get_Status = new string[] { "waiting", "underway", "completed", "pick" };
private Color Curr_Color; //當(dāng)前顏色
private Color[] lightColors = new Color[] {Color.DimGray, Color.Cornsilk, Color.Aquamarine, Color.Coral};
/// <summary>
/// 自定義的text屬性
/// </summary>
[Category("Text"), Description("文本框里的提示文字"), Browsable(true)]
public string Light_text
{
get { return _Light_text; }
set
{
if (value == null) throw new ArgumentNullException("value");
_Light_text = value;
this.Invalidate();
}
}
/// <summary>
/// 設(shè)置指示燈是否可以被點(diǎn)擊,這取決于你是否想要指示燈對(duì)用戶交互作出反應(yīng)
/// </summary>
public bool Light_clickable
{
get { return _Light_clickable; }
set
{
_Light_clickable = value;
}
}
/// <summary>
/// 設(shè)置四色燈狀態(tài),暗灰色表示"等待中";玉米絲色表示"進(jìn)行中";碧綠色表示"已完成";珊瑚色表示"選中"
/// 可以自行添加更多的case來(lái)給指示燈添加更多的顏色以及這些顏色對(duì)應(yīng)的狀態(tài);
/// 這需要你:1、在Light_States添加你想使用指示燈來(lái)表示的狀態(tài)項(xiàng);
/// 2、在Get_Status中同樣添加你想使用指示燈來(lái)表示的狀態(tài)項(xiàng);
/// 3、在lightColors中添加你想用于表征該狀態(tài)的顏色。
/// </summary>
/// <param name="s">狀態(tài):"waiting"表示"等待中";"underway"表示"進(jìn)行中";"completed"表示"已完成";"pick"表示"選中";以上都不是則默認(rèn)進(jìn)入"waiting"狀態(tài)</param>
public void SetStatus(string s)
{
switch (s)
{
case "waiting":
Curr_StatusCode = (int)Light_States.waiting;
break;
case "underway":
Curr_StatusCode = (int)Light_States.underway;
break;
case "completed":
Curr_StatusCode = (int)Light_States.completed;
break;
case "pick":
Curr_StatusCode = (int)Light_States.pick;
break;
default: //對(duì)于其他輸入均默認(rèn)進(jìn)入"等待中"狀態(tài)
Curr_StatusCode = (int)Light_States.waiting;
break;
}
try
{
Curr_Color = lightColors[Curr_StatusCode];
this.Invalidate();
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}
/// <summary>
/// 獲取信號(hào)燈狀態(tài),根據(jù)當(dāng)前狀態(tài)返回對(duì)應(yīng)的字符串(比如當(dāng)前狀態(tài)為"waiting",則返回"waiting")
/// </summary>
public string GetStatus
{
get { return Get_Status[Curr_StatusCode]; }
}
/// <summary>
/// 重置信號(hào)燈狀態(tài)為"waiting"
/// </summary>
public void ResetState()
{
this.SetStatus("waiting");
}
public IndicatorLight()
{
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.Selectable, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.Cursor = Cursors.Hand;
this.Size = new Size(50, 50);
Curr_StatusCode = (int)Light_States.waiting;
Curr_Color = lightColors[Curr_StatusCode];
}
/// <summary>
/// 重繪控件
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.DrawEllipse(new Pen(new SolidBrush(Color.Blue),2), new Rectangle(4, 4, this.Width - 8, this.Height - 8));
g.FillEllipse(new SolidBrush(Curr_Color), new Rectangle(4, 4, this.Width - 8, this.Height - 8));
TextRenderer.DrawText(g, Light_text, this.Font, new Rectangle(4, 4, this.Width - 8, this.Height - 8), SystemColors.InfoText);
}
/// <summary>
/// 處理四色燈控件的SizeChanged事件中針對(duì)只調(diào)整單邊大小的情況。
/// </summary>
/// <param name="sender">事件的來(lái)源</param>
/// <param name="e">The <see cref="EventArgs"/>包含事件數(shù)據(jù)的實(shí)例</param>
void UCSignalLamp_SizeChanged(object sender, EventArgs e)
{
this.Height = this.Width;
}
/// <summary>
/// 是否選中,當(dāng)該控件狀態(tài)為"已完成"時(shí),點(diǎn)擊該控件會(huì)將狀態(tài)修改為"選中",當(dāng)然再次點(diǎn)擊可以取消選中。對(duì)于其他狀態(tài)("等待中"、"進(jìn)行中")則不做動(dòng)作。
/// 注意:當(dāng)且僅當(dāng)用戶設(shè)置 Light_clickable = true 時(shí)該事件處理代碼才會(huì)得到執(zhí)行
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void lightClick(object sender, EventArgs e)
{
if (_Light_clickable)
{
if (this.Curr_StatusCode == (int)Light_States.completed)
{
this.SetStatus("pick"); //選中
}
else if(this.Curr_StatusCode == (int)Light_States.pick)
{
this.SetStatus("completed"); //取消選中
}
else { }
}
}
}
}
/* IndicatorLight.Designer.cs */
//注意命名空間修改為自己項(xiàng)目的命名空間
namespace WindowsFormsControlLibraryMadeByXJY
{
partial class IndicatorLight
{
/// <summary>
/// 必需的設(shè)計(jì)器變量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
/// <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region 組件設(shè)計(jì)器生成的代碼
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要修改
/// 使用代碼編輯器修改此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Click += new System.EventHandler(this.lightClick);
this.SizeChanged += new System.EventHandler(this.UCSignalLamp_SizeChanged);
}
#endregion
}
}
4、使用方法:?
- 在代碼中通過(guò)設(shè)置該類(lèi)實(shí)例化對(duì)象的Cursor屬性可以更改鼠標(biāo)指針位于控件上時(shí)顯示的光標(biāo);
/* 假設(shè)我們有一個(gè) IndicatorLight1 對(duì)象 */ IndicatorLight1.Cursor = Cursors.Arrow; ...
- 通過(guò)設(shè)置Light_clickable屬性可以更改該控件是否可以被點(diǎn)擊;
IndicatorLight1.Light_clickable = false; IndicatorLight1.Light_clickable = true;
- 通過(guò)調(diào)用SetStatus()方法可以修改指示燈的狀態(tài)(外部表現(xiàn)為顏色發(fā)生改變);
IndicatorLight1.SetStatus("underway"); IndicatorLight1.SetStatus("completed"); ...
- 通過(guò)獲取GetStatus屬性可以使用指示燈狀態(tài)做一些簡(jiǎn)單的判斷操作;
if (IndicatorLight1.GetStatus == "pick") { ... }
5、效果演示:?
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-444471.html
到了這里,關(guān)于Winform自定義控件 —— 指示燈的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!