(六十二)c#Winform自定義控制項-警燈(工業)

来源:https://www.cnblogs.com/bfyx/archive/2019/09/10/11495688.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+,不懂可以先百度瞭解下

開始

添加一個類UCAlarmLamp,繼承自UserControl

添加屬性

 1  /// <summary>
 2         /// The lamp color
 3         /// </summary>
 4         private Color[] lampColor = new Color[] { Color.FromArgb(255, 77, 59) };
 5 
 6         /// <summary>
 7         /// Gets or sets the color of the lamp.
 8         /// </summary>
 9         /// <value>The color of the lamp.</value>
10         [Description("燈顏色,當需要閃爍時,至少需要2個及以上顏色,不需要閃爍則至少需要1個顏色"), Category("自定義")]
11         public Color[] LampColor
12         {
13             get { return lampColor; }
14             set
15             {
16                 if (value == null || value.Length <= 0)
17                     return;
18                 lampColor = value;
19                 Refresh();
20             }
21         }
22 
23         /// <summary>
24         /// The lampstand
25         /// </summary>
26         private Color lampstand = Color.FromArgb(105, 105, 105);
27 
28         /// <summary>
29         /// Gets or sets the lampstand.
30         /// </summary>
31         /// <value>The lampstand.</value>
32         [Description("燈座顏色"), Category("自定義")]
33         public Color Lampstand
34         {
35             get { return lampstand; }
36             set { lampstand = value; }
37         }
38 
39         /// <summary>
40         /// The twinkle speed
41         /// </summary>
42         private int twinkleSpeed = 0;
43 
44         /// <summary>
45         /// Gets or sets the twinkle speed.
46         /// </summary>
47         /// <value>The twinkle speed.</value>
48         [Description("閃爍間隔時間(毫秒),當為0時不閃爍"), Category("自定義")]
49         public int TwinkleSpeed
50         {
51             get { return twinkleSpeed; }
52             set
53             {
54                 if (value < 0)
55                     return;
56                 twinkleSpeed = value;
57                 if (value == 0 || lampColor.Length <= 1)
58                 {
59                     timer.Enabled = false;
60                 }
61                 else
62                 {
63                     intColorIndex = 0;
64                     timer.Interval = value;
65                     timer.Enabled = true;
66                 }
67                 Refresh();
68             }
69         }
70         /// <summary>
71         /// The timer
72         /// </summary>
73         Timer timer;
74         /// <summary>
75         /// The int color index
76         /// </summary>
77         int intColorIndex = 0;
78         /// <summary>
79         /// The m rect working
80         /// </summary>
81         Rectangle m_rectWorking;

重繪

 1  protected override void OnPaint(PaintEventArgs e)
 2         {
 3             base.OnPaint(e);
 4             var g = e.Graphics;
 5             g.SetGDIHigh();
 6 
 7             Color c1 = lampColor[intColorIndex];
 8             GraphicsPath path = new GraphicsPath();
 9             path.AddLine(new Point(m_rectWorking.Left, m_rectWorking.Bottom), new Point(m_rectWorking.Left, m_rectWorking.Top + m_rectWorking.Width));
10             path.AddArc(new Rectangle(m_rectWorking.Left, m_rectWorking.Top, m_rectWorking.Width, m_rectWorking.Width), 180f, 180f);
11             path.AddLine(new Point(m_rectWorking.Right, m_rectWorking.Top + m_rectWorking.Width), new Point(m_rectWorking.Right, m_rectWorking.Bottom));
12             path.CloseAllFigures();
13             g.FillPath(new SolidBrush(c1), path);
14 
15             g.FillRectangle(new SolidBrush(lampstand), new Rectangle(5, m_rectWorking.Bottom - 19, this.Width - 10, 10));
16             g.FillRectangle(new SolidBrush(lampstand), new Rectangle(0, m_rectWorking.Bottom - 10, this.Width, 10));
17         }

完整代碼

  1 // ***********************************************************************
  2 // Assembly         : HZH_Controls
  3 // Created          : 2019-09-10
  4 //
  5 // ***********************************************************************
  6 // <copyright file="UCAlarmLamp.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
 26 {
 27     /// <summary>
 28     /// Class UCAlarmLamp.
 29     /// Implements the <see cref="System.Windows.Forms.UserControl" />
 30     /// </summary>
 31     /// <seealso cref="System.Windows.Forms.UserControl" />
 32     public class UCAlarmLamp : UserControl
 33     {
 34         /// <summary>
 35         /// The lamp color
 36         /// </summary>
 37         private Color[] lampColor = new Color[] { Color.FromArgb(255, 77, 59) };
 38 
 39         /// <summary>
 40         /// Gets or sets the color of the lamp.
 41         /// </summary>
 42         /// <value>The color of the lamp.</value>
 43         [Description("燈顏色,當需要閃爍時,至少需要2個及以上顏色,不需要閃爍則至少需要1個顏色"), Category("自定義")]
 44         public Color[] LampColor
 45         {
 46             get { return lampColor; }
 47             set
 48             {
 49                 if (value == null || value.Length <= 0)
 50                     return;
 51                 lampColor = value;
 52                 Refresh();
 53             }
 54         }
 55 
 56         /// <summary>
 57         /// The lampstand
 58         /// </summary>
 59         private Color lampstand = Color.FromArgb(105, 105, 105);
 60 
 61         /// <summary>
 62         /// Gets or sets the lampstand.
 63         /// </summary>
 64         /// <value>The lampstand.</value>
 65         [Description("燈座顏色"), Category("自定義")]
 66         public Color Lampstand
 67         {
 68             get { return lampstand; }
 69             set { lampstand = value; }
 70         }
 71 
 72         /// <summary>
 73         /// The twinkle speed
 74         /// </summary>
 75         private int twinkleSpeed = 0;
 76 
 77         /// <summary>
 78         /// Gets or sets the twinkle speed.
 79         /// </summary>
 80         /// <value>The twinkle speed.</value>
 81         [Description("閃爍間隔時間(毫秒),當為0時不閃爍"), Category("自定義")]
 82         public int TwinkleSpeed
 83         {
 84             get { return twinkleSpeed; }
 85             set
 86             {
 87                 if (value < 0)
 88                     return;
 89                 twinkleSpeed = value;
 90                 if (value == 0 || lampColor.Length <= 1)
 91                 {
 92                     timer.Enabled = false;
 93                 }
 94                 else
 95                 {
 96                     intColorIndex = 0;
 97                     timer.Interval = value;
 98                     timer.Enabled = true;
 99                 }
100                 Refresh();
101             }
102         }
103         /// <summary>
104         /// The timer
105         /// </summary>
106         Timer timer;
107         /// <summary>
108         /// The int color index
109         /// </summary>
110         int intColorIndex = 0;
111         /// <summary>
112         /// The m rect working
113         /// </summary>
114         Rectangle m_rectWorking;
115         /// <summary>
116         /// Initializes a new instance of the <see cref="UCAlarmLamp"/> class.
117         /// </summary>
118         public UCAlarmLamp()
119         {
120             this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
121             this.SetStyle(ControlStyles.DoubleBuffer, true);
122             this.SetStyle(ControlStyles.ResizeRedraw, true);
123             this.SetStyle(ControlStyles.Selectable, true);
124             this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
125             this.SetStyle(ControlStyles.UserPaint, true);
126             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
127             this.SizeChanged += UCAlarmLamp_SizeChanged;
128             this.Size = new Size(50, 50);
129             timer = new Timer();
130             timer.Interval = 200;
131             timer.Tick += timer_Tick;
132         }
133 
134         /// <summary>
135         /// Handles the SizeChanged event of the UCAlarmLamp control.
136         /// </summary>
137         /// <param name="sender">The source of the event.</param>
138         /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
139         void UCAlarmLamp_SizeChanged(object sender, EventArgs e)
140         {
141             m_rectWorking = new Rectangle(10, 0, this.Width - 20, this.Height);
142         }
143         /// <summary>
144         /// Handles the Tick event of the timer control.
145         /// </summary>
146         /// <param name="sender">The source of the event.</param>
147         /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
148         void timer_Tick(object sender, EventArgs e)
149         {
150             intColorIndex++;
151             if (intColorIndex >= lampColor.Length)
152                 intColorIndex = 0;
153             Refresh();
154         }
155         /// <summary>
156         /// 引發 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
157         /// </summary>
158         /// <param name="e">包含事件數據的 <see cref="T:System.Windows.Forms.PaintEventArgs" /></param>
159         protected override void OnPaint(PaintEventArgs e)
160         {
161             base.OnPaint(e);
162             var g = e.Graphics;
163             g.SetGDIHigh();
164 
165             Color c1 = lampColor[intColorIndex];
166             GraphicsPath path = new GraphicsPath();
167             path.AddLine(new Point(m_rectWorking.Left, m_rectWorking.Bottom), new Point(m_rectWorking.Left, m_rectWorking.Top + m_rectWorking.Width));
168             path.AddArc(new Rectangle(m_rectWorking.Left, m_rectWorking.Top, m_rectWorking.Width, m_rectWorking.Width), 180f, 180f);
169             path.AddLine(new Point(m_rectWorking.Right, m_rectWorking.Top + m_rectWorking.Width), new Point(m_rectWorking.Right, m_rectWorking.Bottom));
170             path.CloseAllFigures();
171             g.FillPath(new SolidBrush(c1), path);
172 
173             g.FillRectangle(new SolidBrush(lampstand), new Rectangle(5, m_rectWorking.Bottom - 19, this.Width - 10, 10));
174             g.FillRectangle(new SolidBrush(lampstand), new Rectangle(0, m_rectWorking.Bottom - 10, this.Width, 10));
175         }
176     }
177 }
View Code

 

最後的話

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


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

-Advertisement-
Play Games
更多相關文章
  • 背景:臨時提供一個簡單的網頁,供其他人瀏覽資料庫(Oracel、MSSQL)的某些數據,並導出Excel。 ...
  • 依賴註入主要是一種結構性的模式,註重的是類與類之間的結構,它要達到的目的就是設計原則中最少知道和合成復用的原則,減少內部依賴,履行單一職責,最終就是強解耦。依賴註入目前最好的實現就是依賴註入容器。 Unity是微軟Patterns & Practices團隊所開發的一個輕量級的,並且可擴展的依賴註入 ...
  • 場景 DevExpress的PanelControl常用進行窗體頁面的佈局。 一般是拖拽一個PannelControl,然後是再拖拽其他控制項。 如果是由代碼生成控制項並控制佈局的話,怎樣實現。 關註公眾號 霸道的程式猿 獲取編程相關電子書、教程推送與免費下載。 實現 比如說要在PanelContrl中 ...
  • 通過 abp(net core)+easyui+efcore實現倉儲管理系統——菜單-上 (十六)這篇文章,我們已經瞭解了ABP中的菜單相關的類及類的屬性與方法,接下我們通過實例來實現一個動態載入菜單的功能。動態菜單是我們在abp(net core)+easyui+efcore實現倉儲管理系統——領... ...
  • 場景 使用DevExpress的EditText控制項時,需要限制其輸入類型為數字。 正常來說是窗體上拖拽一個TextEdit,然後在設計視窗點擊小三角,選擇Change Mask 但是如果說TextEdit控制項不是拖拽上去而是由代碼生成的,那麼在代碼中怎樣設置只能輸入數字。 關註公眾號 霸道的程式猿 ...
  • 前提 入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_contr ...
  • Win10 IIS預設是.net 4.0,安裝VS2015後,IIS沒有.net 4.5,解決方法,直接在CMD命令行下執行下麵語句 ...
  • Asp.NetCore實現Aop,AspectCore實現Aop ...
一周排行
    -Advertisement-
    Play Games
  • 1. 說明 /* Performs operations on System.String instances that contain file or directory path information. These operations are performed in a cross-pla ...
  • 視頻地址:【WebApi+Vue3從0到1搭建《許可權管理系統》系列視頻:搭建JWT系統鑒權-嗶哩嗶哩】 https://b23.tv/R6cOcDO qq群:801913255 一、在appsettings.json中設置鑒權屬性 /*jwt鑒權*/ "JwtSetting": { "Issuer" ...
  • 引言 集成測試可在包含應用支持基礎結構(如資料庫、文件系統和網路)的級別上確保應用組件功能正常。 ASP.NET Core 通過將單元測試框架與測試 Web 主機和記憶體中測試伺服器結合使用來支持集成測試。 簡介 集成測試與單元測試相比,能夠在更廣泛的級別上評估應用的組件,確認多個組件一起工作以生成預 ...
  • 在.NET Emit編程中,我們探討了運算操作指令的重要性和應用。這些指令包括各種數學運算、位操作和比較操作,能夠在動態生成的代碼中實現對數據的處理和操作。通過這些指令,開發人員可以靈活地進行算術運算、邏輯運算和比較操作,從而實現各種複雜的演算法和邏輯......本篇之後,將進入第七部分:實戰項目 ...
  • 前言 多表頭表格是一個常見的業務需求,然而WPF中卻沒有預設實現這個功能,得益於WPF強大的控制項模板設計,我們可以通過修改控制項模板的方式自己實現它。 一、需求分析 下圖為一個典型的統計表格,統計1-12月的數據。 此時我們有一個需求,需要將月份按季度劃分,以便能夠直觀地看到季度統計數據,以下為該需求 ...
  • 如何將 ASP.NET Core MVC 項目的視圖分離到另一個項目 在當下這個年代 SPA 已是主流,人們早已忘記了 MVC 以及 Razor 的故事。但是在某些場景下 SSR 還是有意想不到效果。比如某些靜態頁面,比如追求首屏載入速度的時候。最近在項目中回歸傳統效果還是不錯。 有的時候我們希望將 ...
  • System.AggregateException: 發生一個或多個錯誤。 > Microsoft.WebTools.Shared.Exceptions.WebToolsException: 生成失敗。檢查輸出視窗瞭解更多詳細信息。 內部異常堆棧跟蹤的結尾 > (內部異常 #0) Microsoft ...
  • 引言 在上一章節我們實戰了在Asp.Net Core中的項目實戰,這一章節講解一下如何測試Asp.Net Core的中間件。 TestServer 還記得我們在集成測試中提供的TestServer嗎? TestServer 是由 Microsoft.AspNetCore.TestHost 包提供的。 ...
  • 在發現結果為真的WHEN子句時,CASE表達式的真假值判斷會終止,剩餘的WHEN子句會被忽略: CASE WHEN col_1 IN ('a', 'b') THEN '第一' WHEN col_1 IN ('a') THEN '第二' ELSE '其他' END 註意: 統一各分支返回的數據類型. ...
  • 在C#編程世界中,語法的精妙之處往往體現在那些看似微小卻極具影響力的符號與結構之中。其中,“_ =” 這一組合突然出現還真不知道什麼意思。本文將深入剖析“_ =” 的含義、工作原理及其在實際編程中的廣泛應用,揭示其作為C#語法奇兵的重要角色。 一、下劃線 _:神秘的棄元符號 下劃線 _ 在C#中並非 ...