(六十四)c#Winform自定義控制項-溫度計(工業)

来源:https://www.cnblogs.com/bfyx/archive/2019/09/10/11498225.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

用處及效果

準備工作

依然用GID+畫的,不懂請自行百度

開始

添加一個類UCThermometer,繼承UserControl

添加一個枚舉,來決定顯示的溫度單位

 1  public enum TemperatureUnit
 2     {
 3         /// <summary>
 4         /// 不顯示
 5         /// </summary>
 6         None,
 7         /// <summary>
 8         /// 攝氏度
 9         /// </summary>
10         C,
11         /// <summary>
12         /// 華氏度
13         /// </summary>
14         F,
15         /// <summary>
16         /// 開氏度
17         /// </summary>
18         K,
19         /// <summary>
20         /// 蘭氏度
21         /// </summary>
22         R,
23         /// <summary>
24         /// 列氏度
25         /// </summary>
26         Re
27     }

添加一些屬性

  1 /// <summary>
  2         /// The glass tube color
  3         /// </summary>
  4         private Color glassTubeColor = Color.FromArgb(211, 211, 211);
  5 
  6         /// <summary>
  7         /// Gets or sets the color of the glass tube.
  8         /// </summary>
  9         /// <value>The color of the glass tube.</value>
 10         [Description("玻璃管顏色"), Category("自定義")]
 11         public Color GlassTubeColor
 12         {
 13             get { return glassTubeColor; }
 14             set
 15             {
 16                 glassTubeColor = value;
 17                 Refresh();
 18             }
 19         }
 20 
 21         /// <summary>
 22         /// The mercury color
 23         /// </summary>
 24         private Color mercuryColor = Color.FromArgb(255, 77, 59);
 25 
 26         /// <summary>
 27         /// Gets or sets the color of the mercury.
 28         /// </summary>
 29         /// <value>The color of the mercury.</value>
 30         [Description("水印顏色"), Category("自定義")]
 31         public Color MercuryColor
 32         {
 33             get { return mercuryColor; }
 34             set
 35             {
 36                 mercuryColor = value;
 37                 Refresh();
 38             }
 39         }
 40 
 41         /// <summary>
 42         /// The minimum value
 43         /// </summary>
 44         private decimal minValue = 0;
 45         /// <summary>
 46         /// 左側刻度最小值
 47         /// </summary>
 48         /// <value>The minimum value.</value>
 49         [Description("左側刻度最小值"), Category("自定義")]
 50         public decimal MinValue
 51         {
 52             get { return minValue; }
 53             set
 54             {
 55                 minValue = value;
 56                 Refresh();
 57             }
 58         }
 59 
 60         /// <summary>
 61         /// The maximum value
 62         /// </summary>
 63         private decimal maxValue = 100;
 64         /// <summary>
 65         /// 左側刻度最大值
 66         /// </summary>
 67         /// <value>The maximum value.</value>
 68         [Description("左側刻度最大值"), Category("自定義")]
 69         public decimal MaxValue
 70         {
 71             get { return maxValue; }
 72             set
 73             {
 74                 maxValue = value;
 75                 Refresh();
 76             }
 77         }
 78 
 79         /// <summary>
 80         /// The m value
 81         /// </summary>
 82         private decimal m_value = 10;
 83         /// <summary>
 84         /// 左側刻度值
 85         /// </summary>
 86         /// <value>The value.</value>
 87         [Description("左側刻度值"), Category("自定義")]
 88         public decimal Value
 89         {
 90             get { return m_value; }
 91             set
 92             {
 93                 m_value = value;
 94                 Refresh();
 95             }
 96         }
 97 
 98         /// <summary>
 99         /// The split count
100         /// </summary>
101         private int splitCount = 0;
102         /// <summary>
103         /// 刻度分隔份數
104         /// </summary>
105         /// <value>The split count.</value>
106         [Description("刻度分隔份數"), Category("自定義")]
107         public int SplitCount
108         {
109             get { return splitCount; }
110             set
111             {
112                 if (value <= 0)
113                     return;
114                 splitCount = value;
115                 Refresh();
116             }
117         }
118 
119         /// <summary>
120         /// 獲取或設置控制項顯示的文字的字體。
121         /// </summary>
122         /// <value>The font.</value>
123         /// <PermissionSet>
124         ///   <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
125         ///   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
126         ///   <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
127         ///   <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
128         /// </PermissionSet>
129         [Description("獲取或設置控制項顯示的文字的字體"), Category("自定義")]
130         public override Font Font
131         {
132             get
133             {
134                 return base.Font;
135             }
136             set
137             {
138                 base.Font = value;
139                 Refresh();
140             }
141         }
142 
143         /// <summary>
144         /// 獲取或設置控制項的前景色。
145         /// </summary>
146         /// <value>The color of the fore.</value>
147         /// <PermissionSet>
148         ///   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
149         /// </PermissionSet>
150         [Description("獲取或設置控制項的文字及刻度顏色"), Category("自定義")]
151         public override System.Drawing.Color ForeColor
152         {
153             get
154             {
155                 return base.ForeColor;
156             }
157             set
158             {
159                 base.ForeColor = value;
160                 Refresh();
161             }
162         }
163 
164         /// <summary>
165         /// The left temperature unit
166         /// </summary>
167         private TemperatureUnit leftTemperatureUnit = TemperatureUnit.C;
168         /// <summary>
169         /// 左側刻度單位,不可為none
170         /// </summary>
171         /// <value>The left temperature unit.</value>
172         [Description("左側刻度單位,不可為none"), Category("自定義")]
173         public TemperatureUnit LeftTemperatureUnit
174         {
175             get { return leftTemperatureUnit; }
176             set
177             {
178                 if (value == TemperatureUnit.None)
179                     return;
180                 leftTemperatureUnit = value;
181                 Refresh();
182             }
183         }
184 
185         /// <summary>
186         /// The right temperature unit
187         /// </summary>
188         private TemperatureUnit rightTemperatureUnit = TemperatureUnit.C;
189         /// <summary>
190         /// 右側刻度單位,當為none時,不顯示
191         /// </summary>
192         /// <value>The right temperature unit.</value>
193         [Description("右側刻度單位,當為none時,不顯示"), Category("自定義")]
194         public TemperatureUnit RightTemperatureUnit
195         {
196             get { return rightTemperatureUnit; }
197             set
198             {
199                 rightTemperatureUnit = value;
200                 Refresh();
201             }
202         }
203 
204         /// <summary>
205         /// The m rect working
206         /// </summary>
207         Rectangle m_rectWorking;
208         /// <summary>
209         /// The m rect left
210         /// </summary>
211         Rectangle m_rectLeft;
212         /// <summary>
213         /// The m rect right
214         /// </summary>
215         Rectangle m_rectRight;

改變大小時,設定畫圖區域

1  void UCThermometer_SizeChanged(object sender, EventArgs e)
2         {
3             m_rectWorking = new Rectangle(this.Width / 2 - this.Width / 8, this.Width / 4, this.Width / 4, this.Height - this.Width / 2);
4             m_rectLeft = new Rectangle(0, m_rectWorking.Top + m_rectWorking.Width / 2, (this.Width - this.Width / 4) / 2 - 2, m_rectWorking.Height - m_rectWorking.Width * 2);
5             m_rectRight = new Rectangle(this.Width - (this.Width - this.Width / 4) / 2 + 2, m_rectWorking.Top + m_rectWorking.Width / 2, (this.Width - this.Width / 4) / 2 - 2, m_rectWorking.Height - m_rectWorking.Width * 2);
6         }

重繪

 1  protected override void OnPaint(PaintEventArgs e)
 2         {
 3             base.OnPaint(e);
 4             var g = e.Graphics;
 5             g.SetGDIHigh();
 6 
 7             //玻璃管管
 8             GraphicsPath path = new GraphicsPath();
 9             path.AddLine(m_rectWorking.Left, m_rectWorking.Bottom, m_rectWorking.Left, m_rectWorking.Top + m_rectWorking.Width / 2);
10             path.AddArc(new Rectangle(m_rectWorking.Left, m_rectWorking.Top, m_rectWorking.Width, m_rectWorking.Width), 180, 180);
11             path.AddLine(m_rectWorking.Right, m_rectWorking.Top + m_rectWorking.Width / 2, m_rectWorking.Right, m_rectWorking.Bottom);
12             path.CloseAllFigures();
13             g.FillPath(new SolidBrush(glassTubeColor), path);
14 
15             //底部
16             var rectDi = new Rectangle(this.Width / 2 - m_rectWorking.Width, m_rectWorking.Bottom - m_rectWorking.Width - 2, m_rectWorking.Width * 2, m_rectWorking.Width * 2);
17             g.FillEllipse(new SolidBrush(glassTubeColor), rectDi);
18             g.FillEllipse(new SolidBrush(mercuryColor), new Rectangle(rectDi.Left + 4, rectDi.Top + 4, rectDi.Width - 8, rectDi.Height - 8));
19 
20             //刻度
21             decimal decSplit = (maxValue - minValue) / splitCount;
22             decimal decSplitHeight = m_rectLeft.Height / splitCount;
23             for (int i = 0; i <= splitCount; i++)
24             {
25                 g.DrawLine(new Pen(new SolidBrush(ForeColor), 1), new PointF(m_rectLeft.Left + 2, (float)(m_rectLeft.Bottom - decSplitHeight * i)), new PointF(m_rectLeft.Right, (float)(m_rectLeft.Bottom - decSplitHeight * i)));
26 
27                 var valueLeft = (minValue + decSplit * i).ToString("0.##");
28                 var sizeLeft = g.MeasureString(valueLeft, Font);
29                 g.DrawString(valueLeft, Font, new SolidBrush(ForeColor), new PointF(m_rectLeft.Left, m_rectLeft.Bottom - (float)decSplitHeight * i - sizeLeft.Height - 1));
30 
31                 if (rightTemperatureUnit != TemperatureUnit.None)
32                 {
33                     g.DrawLine(new Pen(new SolidBrush(Color.Black), 1), new PointF(m_rectRight.Left + 2, (float)(m_rectRight.Bottom - decSplitHeight * i)), new PointF(m_rectRight.Right, (float)(m_rectRight.Bottom - decSplitHeight * i)));
34                     var valueRight = GetRightValue(minValue + decSplit * i).ToString("0.##");
35                     var sizeRight = g.MeasureString(valueRight, Font);
36                     g.DrawString(valueRight, Font, new SolidBrush(ForeColor), new PointF(m_rectRight.Right - sizeRight.Width - 1, m_rectRight.Bottom - (float)decSplitHeight * i - sizeRight.Height - 1));
37                 }
38                 if (i != splitCount)
39                 {
40                     if (decSplitHeight > 40)
41                     {
42                         var decSp1 = decSplitHeight / 10;
43                         for (int j = 1; j < 10; j++)
44                         {
45                             if (j == 5)
46                             {
47                                 g.DrawLine(new Pen(new SolidBrush(ForeColor), 1), new PointF(m_rectLeft.Right - 10, (m_rectLeft.Bottom - (float)decSplitHeight * i - ((float)decSp1 * j))), new PointF(m_rectLeft.Right, (m_rectLeft.Bottom - (float)decSplitHeight * i - ((float)decSp1 * j))));
48                                 if (rightTemperatureUnit != TemperatureUnit.None)
49                                 {
50                                     g.DrawLine(new Pen(new SolidBrush(ForeColor), 1), new PointF(m_rectRight.Left + 10, (m_rectRight.Bottom - (float)decSplitHeight * i - ((float)decSp1 * j))), new PointF(m_rectRight.Left, (m_rectRight.Bottom - (float)decSplitHeight * i - ((float)decSp1 * j))));
51                                 }
52                             }
53                             else
54                             {
55                                 g.DrawLine(new Pen(new SolidBrush(ForeColor), 1), new PointF(m_rectLeft.Right - 5, (m_rectLeft.Bottom - (float)decSplitHeight * i - ((float)decSp1 * j))), new PointF(m_rectLeft.Right, (m_rectLeft.Bottom - (float)decSplitHeight * i - ((float)decSp1 * j))));
56                                 if (rightTemperatureUnit != TemperatureUnit.None)
57                                 {
58                                     g.DrawLine(new Pen(new SolidBrush(ForeColor), 1), new PointF(m_rectRight.Left + 5, (m_rectRight.Bottom - (float)decSplitHeight * i - ((float)decSp1 * j))), new PointF(m_rectRight.Left, (m_rectRight.Bottom - (float)decSplitHeight * i - ((float)decSp1 * j))));
59                                 }
60                             }
61                         }
62                     }
63                     else if (decSplitHeight > 10)
64                     {
65                         g.DrawLine(new Pen(new SolidBrush(ForeColor), 1), new PointF(m_rectLeft.Right - 5, (m_rectLeft.Bottom - (float)decSplitHeight * i - (float)decSplitHeight / 2)), new PointF(m_rectLeft.Right, (m_rectLeft.Bottom - (float)decSplitHeight * i - (float)decSplitHeight / 2)));
66                         if (rightTemperatureUnit != TemperatureUnit.None)
67                         {
68                             g.DrawLine(new Pen(new SolidBrush(ForeColor), 1), new PointF(m_rectRight.Left + 5, (m_rectRight.Bottom - (float)decSplitHeight * i - (float)decSplitHeight / 2)), new PointF(m_rectRight.Left, (m_rectRight.Bottom - (float)decSplitHeight * i - (float)decSplitHeight / 2)));
69                         }
70                     }
71                 }
72             }
73             //單位
74             string strLeftUnit = GetUnitChar(leftTemperatureUnit);
75             g.DrawString(strLeftUnit, Font, new SolidBrush(ForeColor), new PointF(m_rectLeft.Left + 2, 2));
76             if (rightTemperatureUnit != TemperatureUnit.None)
77             {
78                 string strRightUnit = GetUnitChar(rightTemperatureUnit);
79                 var rightSize = g.MeasureString(strRightUnit, Font);
80                 g.DrawString(strRightUnit, Font, new SolidBrush(ForeColor), new PointF(m_rectRight.Right - 2 - rightSize.Width, 2));
81             }
82             //
83             float fltHeightValue = (float)(Value / (maxValue - minValue) * m_rectLeft.Height);
84             RectangleF rectValue = new RectangleF(m_rectWorking.Left + 4, m_rectLeft.Top + (m_rectLeft.Height - fltHeightValue), m_rectWorking.Width - 8, fltHeightValue + (m_rectWorking.Height - m_rectWorking.Width / 2 - m_rectLeft.Height));
85             g.FillRectangle(new SolidBrush(mercuryColor), rectValue);
86 
87 
88             var sizeValue = g.MeasureString(m_value.ToString("0.##"), Font);
89             g.DrawString(m_value.ToString("0.##"), Font, new SolidBrush(Color.White), new PointF(rectDi.Left + (rectDi.Width - sizeValue.Width) / 2, rectDi.Top + (rectDi.Height - sizeValue.Height) / 2 + 1));
90         }

輔助函數

 1 private string GetUnitChar(TemperatureUnit unit)
 2         {
 3             string strUnit = "";
 4             switch (unit)
 5             {
 6                 case TemperatureUnit.C:
 7                     strUnit = "";
 8                     break;
 9                 case TemperatureUnit.F:
10                     strUnit = "";
11                     break;
12                 case TemperatureUnit.K:
13                     strUnit = "K";
14                     break;
15                 case TemperatureUnit.R:
16                     strUnit = "°R";
17                     break;
18                 case TemperatureUnit.Re:
19                     strUnit = "°Re";
20                     break;
21             }
22             return strUnit;
23         }
24 
25         private decimal GetRightValue(decimal decValue)
26         {
27             //先將左側的換算為攝氏度
28             var dec = decValue;
29             switch (leftTemperatureUnit)
30             {
31                 case TemperatureUnit.F:
32                     dec = (decValue - 32) / (9M / 5M);
33                     break;
34                 case TemperatureUnit.K:
35                     dec = decValue - 273;
	   

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

-Advertisement-
Play Games
更多相關文章
  • 我不打算解釋什麼是 ,也不解釋為什麼要使用它。我希望你已經在其他地方瞭解過,如果沒有,你可以使用 去搜索它。在本文中,我將告訴您如何使用專門針對 和`RxJava`的響應式編程。讓我們開始吧。 1.預備知識 在你繼續閱讀之前,我希望你能理解如何使用 和`RxJava REST API`。 如果不能, ...
  • 上次在 asp.net core 從單機到集群 一文中提到存儲還不支持分散式,並立了一個 flag > 基於 github 或者 開源中國的碼雲實現一個 storage 於是這兩天就來填坑了。。 ...
  • 一、static關鍵字 下麵我設計了一個房貸利率上浮類(用來計算房貸利率上浮多少): 上面例子的問題在於基準利率這個屬性是所有房貸利率上浮對象共用的屬性,而不是每個房貸利率上浮對象都擁有一個基準利率。所以要把基準利率這個屬性設置成共用的需要使用static關鍵字,第二版房貸利率上浮類: 靜態自動屬性 ...
  • 在java的spring中有自動註入功能,使得代碼變得更加簡潔靈活,所以想把這個功能移植到c#中,接下來逐步分析實現過程 1.使用自動註入場景分析 在asp.net mvc中,無論是什麼代碼邏輯分層,最終的表現層為Controller層,所以我們註入點就是在Controller中,這裡我們需要替換默 ...
  • EF 6及以前的版本是預設支持延遲載入(Lazy Loading)的,早期的EF Core中並不支持,必須使用Include方法來支持導航屬性的數據載入。 當然在 EF Core 2.1 及之後版本中已經引入了延遲載入功能,詳細實現原理可以查看官網( "傳送門" )。 下麵記錄一下,分別使用Incl ...
  • 按照目前的軟體開發發展趨勢中,不管是前後端分離還是提供數據服務,WebApi使用的越來越廣泛,而且.NET Core也是我們.NET開發人員未來發展的趨勢,所以說學會使用.NET Core Api是非常有必要的。 本人作為一個.NET菜鳥,正在慢慢的學習中,將學到的一步一步記錄下來。 一、創建項目 ...
  • 官網地址:https://framework7.io/docs/autocomplete.html#autocomplete-parameters 效果圖: <meta charset="UTF-8"><meta name="viewport" content="width=device-width ...
  • Http請求資源的過程可以看成一個管道:“Pipe”,並不是所有的請求都是合法的、安全的,其於功能、性能或安全方面的考慮,通常需要在這管道中裝配一些處理程式來篩選和加工這些請求。這些處理程式就是中間件。 中間件之間的調用順序就是添加中間件組件的順序,調用順序以於應用程式的安全性、性能、和功能至關重要 ...
一周排行
    -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.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...