(六十一)c#Winform自定義控制項-信號燈(工業)

来源:https://www.cnblogs.com/bfyx/archive/2019/09/09/11492401.html
-Advertisement-
Play Games

前提 入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_contr ...


前提

入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。

GitHub:https://github.com/kwwwvagaa/NetWinformControl

碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果覺得寫的還行,請點個 star 支持一下吧

歡迎前來交流探討: 企鵝群568015492 企鵝群568015492

麻煩博客下方點個【推薦】,謝謝

NuGet

Install-Package HZH_Controls

目錄

https://www.cnblogs.com/bfyx/p/11364884.html

用處及效果

準備工作

依然使用GDI+繪畫,這個比較簡單,就是畫圓

開始

新增一個類UCSignalLamp,繼承UserControl

添加屬性

  1 /// <summary>
  2         /// The is show border
  3         /// </summary>
  4         private bool isShowBorder = false;
  5 
  6         /// <summary>
  7         /// Gets or sets a value indicating whether this instance is show border.
  8         /// </summary>
  9         /// <value><c>true</c> if this instance is show border; otherwise, <c>false</c>.</value>
 10         [Description("是否顯示邊框"), Category("自定義")]
 11         public bool IsShowBorder
 12         {
 13             get { return isShowBorder; }
 14             set
 15             {
 16                 isShowBorder = value;
 17                 Refresh();
 18             }
 19         }
 20 
 21         /// <summary>
 22         /// The lamp color
 23         /// </summary>
 24         private Color[] lampColor = new Color[] { Color.FromArgb(255, 77, 59) };
 25 
 26         /// <summary>
 27         /// Gets or sets the color of the lamp.
 28         /// </summary>
 29         /// <value>The color of the lamp.</value>
 30         [Description("燈顏色,當需要閃爍時,至少需要2個及以上顏色,不需要閃爍則至少需要1個顏色"), Category("自定義")]
 31         public Color[] LampColor
 32         {
 33             get { return lampColor; }
 34             set
 35             {
 36                 if (value == null || value.Length <= 0)
 37                     return;
 38                 lampColor = value;
 39                 Refresh();
 40             }
 41         }
 42 
 43         /// <summary>
 44         /// The is highlight
 45         /// </summary>
 46         private bool isHighlight = true;
 47 
 48         /// <summary>
 49         /// Gets or sets a value indicating whether this instance is highlight.
 50         /// </summary>
 51         /// <value><c>true</c> if this instance is highlight; otherwise, <c>false</c>.</value>
 52         [Description("是否高亮顯示"), Category("自定義")]
 53         public bool IsHighlight
 54         {
 55             get { return isHighlight; }
 56             set
 57             {
 58                 isHighlight = value;
 59                 Refresh();
 60             }
 61         }
 62 
 63         /// <summary>
 64         /// The twinkle speed
 65         /// </summary>
 66         private int twinkleSpeed = 0;
 67 
 68         /// <summary>
 69         /// Gets or sets the twinkle speed.
 70         /// </summary>
 71         /// <value>The twinkle speed.</value>
 72         [Description("閃爍間隔時間(毫秒),當為0時不閃爍"), Category("自定義")]
 73         public int TwinkleSpeed
 74         {
 75             get { return twinkleSpeed; }
 76             set
 77             {
 78                 if (value < 0)
 79                     return;
 80                 twinkleSpeed = value;
 81                 if (value == 0 || lampColor.Length <= 1)
 82                 {
 83                     timer.Enabled = false;
 84                 }
 85                 else
 86                 {
 87                     intColorIndex = 0;
 88                     timer.Interval = value;
 89                     timer.Enabled = true;
 90                 }
 91                 Refresh();
 92             }
 93         }
 94         /// <summary>
 95         /// The timer
 96         /// </summary>
 97         Timer timer;
 98         /// <summary>
 99         /// The int color index
100         /// </summary>
101         int intColorIndex = 0;

重繪

 1  protected override void OnPaint(PaintEventArgs e)
 2         {
 3             base.OnPaint(e);
 4             var g = e.Graphics;
 5             g.SetGDIHigh();
 6             Color c1 = lampColor[intColorIndex];
 7             g.FillEllipse(new SolidBrush(c1), this.ClientRectangle);
 8 
 9             if (isHighlight)
10             {
11                 GraphicsPath gp = new GraphicsPath();
12 
13                 Rectangle rec = new Rectangle(5, 5, this.Width - 10, this.Height - 10);
14                 gp.AddEllipse(rec);
15 
16                 Color[] surroundColor = new Color[] { c1 };
17                 PathGradientBrush pb = new PathGradientBrush(gp);
18                 pb.CenterColor = Color.White;
19                 pb.SurroundColors = surroundColor;
20                 g.FillPath(pb, gp);
21             }
22 
23             if (isShowBorder)
24             {
25                 g.DrawEllipse(new Pen(new SolidBrush(this.BackColor), 2), new Rectangle(4, 4, this.Width - 8, this.Height - 8));
26             }
27         }

全部代碼

  1 // ***********************************************************************
  2 // Assembly         : HZH_Controls
  3 // Created          : 2019-09-09
  4 //
  5 // ***********************************************************************
  6 // <copyright file="UCSignalLamp.cs">
  7 //     Copyright by Huang Zhenghui(黃正輝) All, QQ group:568015492 QQ:623128629 Email:[email protected]
  8 // </copyright>
  9 //
 10 // Blog: https://www.cnblogs.com/bfyx
 11 // GitHub:https://github.com/kwwwvagaa/NetWinformControl
 12 // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
 13 //
 14 // If you use this code, please keep this note.
 15 // ***********************************************************************
 16 using System;
 17 using System.Collections.Generic;
 18 using System.Linq;
 19 using System.Text;
 20 using System.Windows.Forms;
 21 using System.Drawing;
 22 using System.Drawing.Drawing2D;
 23 using System.ComponentModel;
 24 
 25 namespace HZH_Controls.Controls.FactoryControls.Lamp
 26 {
 27     /// <summary>
 28     /// Class UCSignalLamp.
 29     /// Implements the <see cref="System.Windows.Forms.UserControl" />
 30     /// </summary>
 31     /// <seealso cref="System.Windows.Forms.UserControl" />
 32     public class UCSignalLamp : UserControl
 33     {
 34         /// <summary>
 35         /// The is show border
 36         /// </summary>
 37         private bool isShowBorder = false;
 38 
 39         /// <summary>
 40         /// Gets or sets a value indicating whether this instance is show border.
 41         /// </summary>
 42         /// <value><c>true</c> if this instance is show border; otherwise, <c>false</c>.</value>
 43         [Description("是否顯示邊框"), Category("自定義")]
 44         public bool IsShowBorder
 45         {
 46             get { return isShowBorder; }
 47             set
 48             {
 49                 isShowBorder = value;
 50                 Refresh();
 51             }
 52         }
 53 
 54         /// <summary>
 55         /// The lamp color
 56         /// </summary>
 57         private Color[] lampColor = new Color[] { Color.FromArgb(255, 77, 59) };
 58 
 59         /// <summary>
 60         /// Gets or sets the color of the lamp.
 61         /// </summary>
 62         /// <value>The color of the lamp.</value>
 63         [Description("燈顏色,當需要閃爍時,至少需要2個及以上顏色,不需要閃爍則至少需要1個顏色"), Category("自定義")]
 64         public Color[] LampColor
 65         {
 66             get { return lampColor; }
 67             set
 68             {
 69                 if (value == null || value.Length <= 0)
 70                     return;
 71                 lampColor = value;
 72                 Refresh();
 73             }
 74         }
 75 
 76         /// <summary>
 77         /// The is highlight
 78         /// </summary>
 79         private bool isHighlight = true;
 80 
 81         /// <summary>
 82         /// Gets or sets a value indicating whether this instance is highlight.
 83         /// </summary>
 84         /// <value><c>true</c> if this instance is highlight; otherwise, <c>false</c>.</value>
 85         [Description("是否高亮顯示"), Category("自定義")]
 86         public bool IsHighlight
 87         {
 88             get { return isHighlight; }
 89             set
 90             {
 91                 isHighlight = value;
 92                 Refresh();
 93             }
 94         }
 95 
 96         /// <summary>
 97         /// The twinkle speed
 98         /// </summary>
 99         private int twinkleSpeed = 0;
100 
101         /// <summary>
102         /// Gets or sets the twinkle speed.
103         /// </summary>
104         /// <value>The twinkle speed.</value>
105         [Description("閃爍間隔時間(毫秒),當為0時不閃爍"), Category("自定義")]
106         public int TwinkleSpeed
107         {
108             get { return twinkleSpeed; }
109             set
110             {
111                 if (value < 0)
112                     return;
113                 twinkleSpeed = value;
114                 if (value == 0 || lampColor.Length <= 1)
115                 {
116                     timer.Enabled = false;
117                 }
118                 else
119                 {
120                     intColorIndex = 0;
121                     timer.Interval = value;
122                     timer.Enabled = true;
123                 }
124                 Refresh();
125             }
126         }
127         /// <summary>
128         /// The timer
129         /// </summary>
130         Timer timer;
131         /// <summary>
132         /// The int color index
133         /// </summary>
134         int intColorIndex = 0;
135         /// <summary>
136         /// Initializes a new instance of the <see cref="UCSignalLamp"/> class.
137         /// </summary>
138         public UCSignalLamp()
139         {
140             this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
141             this.SetStyle(ControlStyles.DoubleBuffer, true);
142             this.SetStyle(ControlStyles.ResizeRedraw, true);
143             this.SetStyle(ControlStyles.Selectable, true);
144             this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
145             this.SetStyle(ControlStyles.UserPaint, true);
146             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
147             this.Size = new Size(50, 50);
148             this.SizeChanged += UCSignalLamp_SizeChanged;
149             timer = new Timer();
150             timer.Interval = 200;
151             timer.Tick += timer_Tick;
152         }
153 
154         /// <summary>
155         /// Handles the Tick event of the timer control.
156         /// </summary>
157         /// <param name="sender">The source of the event.</param>
158         /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
159         void timer_Tick(object sender, EventArgs e)
160         {
161             intColorIndex++;
162             if (intColorIndex >= lampColor.Length)
163                 intColorIndex = 0;
164             Refresh();
165         }
166         /// <summary>
167         /// Handles the SizeChanged event of the UCSignalLamp control.
168         /// </summary>
169         /// <param name="sender">The source of the event.</param>
170         /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
171         void UCSignalLamp_SizeChanged(object sender, EventArgs e)
172         {
173             var maxSize = Math.Min(this.Width, this.Height);
174             if (this.Width != maxSize)
175                 this.Width = maxSize;
176             if (this.Height != maxSize)
177                 this.Height = maxSize;
178         }
179 
180         /// <summary>
181         /// 引發 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
182         /// </summary>
183         /// <param name="e">包含事件數據的 <see cref="T:System.Windows.Forms.PaintEventArgs" /></param>
184         protected override void OnPaint(PaintEventArgs e)
185         {
186             base.OnPaint(e);
187             var g = e.Graphics;
188             g.SetGDIHigh();
189             Color c1 = lampColor[intColorIndex];
190             g.FillEllipse(new SolidBrush(c1), this.ClientRectangle);
191 
192             if (isHighlight)
193             {
194                 GraphicsPath gp = new GraphicsPath();
195 
196                 Rectangle rec = new Rectangle(5, 5, this.Width - 10, this.Height - 10);
197                 gp.AddEllipse(rec);
198 
199                 Color[] surroundColor = new Color[] { c1 };
200                 PathGradientBrush pb = new PathGradientBrush(gp);
201                 pb.CenterColor = Color.White;
202                 pb.SurroundColors = surroundColor;
203                 g.FillPath(pb, gp);
204             }
205 
206             if (isShowBorder)
207             {
208                 g.DrawEllipse(new Pen(new SolidBrush(this.BackColor), 2), new Rectangle(4, 4, this.Width - 8, this.Height - 8));
209             }
210         }
211     }
212 }
View Code

 

最後的話

如果你喜歡的話,請到 https://gitee.com/kwwwvagaa/net_winform_custom_control 點個星星吧


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • pycharm快捷鍵 變數 什麼是變數 變數: 定義世間萬物變化的狀態 IPO I input 輸入(變數) P Process 處理 O Output 輸出 變數的組成 1. 變數名:具有描述意義; 接受變數值 2. 賦值符號:賦值,把變數值傳給變數名 3. 變數值:具體的值 變數名的規範 1. ...
  • Django之Models的class Meta 作者:@skyflask轉載本文請註明出處:https://www.cnblogs.com/skyflask/p/9544898.html 目錄 1、abstract2、app_label3、db_table4、db_tablespace5、defa ...
  • java中的所有map都實現了Map介面,以下方法適用於任何map實現(HashMap, TreeMap, LinkedHashMap, Hashtable, 等等)。 1 HashMap map = new HashMap(); 2 map.put(1, "jack"); 3 map.put(2,... ...
  • 本文介紹使用Git初始化本地倉庫,並首次提交代碼到遠程倉庫GitLab上面。 首先,登錄GitLab,創建一個新項目的私人倉庫; 然後,在本地倉庫(就是你寫代碼文件夾),右鍵,Git Bash Here,打開Git命令視窗; 在Git命令視窗輸入 git init,初始化本地倉庫,初始化完成後,本地 ...
  • 簡要介紹微服務架構及其特點,並引入Spring Cloud與其中一部分核心組件。 ...
  • 前幾天微軟發佈了 .NET Core 3.0 Preview 9 ,這是.NET Core 3.0 最後一個預覽版。 .NET Core 3.0 正式發佈將在.NET Conf 上發佈,.NET Conf 時間是9月23日至25日。 Visual Studio 2019 16.3預覽版3和Visua ...
  • 做了很久碼農,也沒個寫博客的習慣,這次開始第一次寫博客。 這個問題也是折騰了我接近一天時間,網上也沒有任何的相關博文,於是決定分享一下,以供同樣擁有此問題的小伙伴們參考。 內容源於目前在做的一個項目,已經封好的功能里,在生成構件時,會產生以 下彈窗。 原以為是前輩留下的信息提示,沒想到是revit自 ...
  • Prism.Unity 中UnityBootStrapper已經不用了,可以繼承PrismApplication 1.Install-package Prism.Unity -v 7.2.0.1367 2. 3. ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...