1. 程式人生 > >(五十六)c#Winform自定義控制元件-瓶子

(五十六)c#Winform自定義控制元件-瓶子

前提

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

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

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

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

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

麻煩部落格下方點個【推薦】,謝謝

NuGet

Install-Package HZH_Controls

目錄

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

用處及效果

準備工作

依然是GDI+  不瞭解請先百度一下

開始

新增一個類UCBottle,繼承UserControl

新增幾個控制屬性

  1 //瓶身區域
  2         /// <summary>
  3         /// The m working rect
  4         /// </summary>
  5         Rectangle m_workingRect;
  6 
  7         /// <summary>
  8         /// The title
  9         /// </summary>
 10         string title = "瓶子1";
 11 
 12         /// <summary>
 13         /// Gets or sets the title.
 14         /// </summary>
 15         /// <value>The title.</value>
 16         [Description("標題"), Category("自定義")]
 17         public string Title
 18         {
 19             get { return title; }
 20             set
 21             {
 22                 title = value;
 23                 ResetWorkingRect();
 24                 Refresh();
 25             }
 26         }
 27 
 28         /// <summary>
 29         /// The bottle color
 30         /// </summary>
 31         private Color bottleColor = Color.FromArgb(255, 77, 59);
 32 
 33         /// <summary>
 34         /// Gets or sets the color of the bottle.
 35         /// </summary>
 36         /// <value>The color of the bottle.</value>
 37         [Description("瓶子顏色"), Category("自定義")]
 38         public Color BottleColor
 39         {
 40             get { return bottleColor; }
 41             set
 42             {
 43                 bottleColor = value;
 44                 Refresh();
 45             }
 46         }
 47 
 48         /// <summary>
 49         /// The bottle mouth color
 50         /// </summary>
 51         private Color bottleMouthColor = Color.FromArgb(105, 105, 105);
 52 
 53         /// <summary>
 54         /// Gets or sets the color of the bottle mouth.
 55         /// </summary>
 56         /// <value>The color of the bottle mouth.</value>
 57         [Description("瓶口顏色"), Category("自定義")]
 58         public Color BottleMouthColor
 59         {
 60             get { return bottleMouthColor; }
 61             set { bottleMouthColor = value; }
 62         }
 63 
 64         /// <summary>
 65         /// The liquid color
 66         /// </summary>
 67         private Color liquidColor = Color.FromArgb(3, 169, 243);
 68 
 69         /// <summary>
 70         /// Gets or sets the color of the liquid.
 71         /// </summary>
 72         /// <value>The color of the liquid.</value>
 73         [Description("液體顏色"), Category("自定義")]
 74         public Color LiquidColor
 75         {
 76             get { return liquidColor; }
 77             set
 78             {
 79                 liquidColor = value;
 80                 Refresh();
 81             }
 82         }
 83 
 84 
 85         /// <summary>
 86         /// 獲取或設定控制元件顯示的文字的字型。
 87         /// </summary>
 88         /// <value>The font.</value>
 89         /// <PermissionSet>
 90         ///   <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
 91         ///   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
 92         ///   <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
 93         ///   <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
 94         /// </PermissionSet>
 95         [Description("文字字型"), Category("自定義")]
 96         public override Font Font
 97         {
 98             get
 99             {
100                 return base.Font;
101             }
102             set
103             {
104                 base.Font = value;
105                 ResetWorkingRect();
106                 Refresh();
107             }
108         }
109 
110         /// <summary>
111         /// 獲取或設定控制元件的前景色。
112         /// </summary>
113         /// <value>The color of the fore.</value>
114         /// <PermissionSet>
115         ///   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
116         /// </PermissionSet>
117         [Description("文字顏色"), Category("自定義")]
118         public override System.Drawing.Color ForeColor
119         {
120             get
121             {
122                 return base.ForeColor;
123             }
124             set
125             {
126                 base.ForeColor = value;
127                 Refresh();
128             }
129         }
130 
131         /// <summary>
132         /// The maximum value
133         /// </summary>
134         private decimal maxValue = 100;
135 
136         /// <summary>
137         /// Gets or sets the maximum value.
138         /// </summary>
139         /// <value>The maximum value.</value>
140         [Description("最大值"), Category("自定義")]
141         public decimal MaxValue
142         {
143             get { return maxValue; }
144             set
145             {
146                 if (value < m_value)
147                     return;
148                 maxValue = value;
149                 Refresh();
150             }
151         }
152 
153         /// <summary>
154         /// The m value
155         /// </summary>
156         private decimal m_value = 50;
157 
158         /// <summary>
159         /// Gets or sets the value.
160         /// </summary>
161         /// <value>The value.</value>
162         [Description("值"), Category("自定義")]
163         public decimal Value
164         {
165             get { return m_value; }
166             set
167             {
168                 if (value <= 0 || value > maxValue)
169                     return;
170                 m_value = value;
171                 Refresh();
172             }
173         }
174 
175         /// <summary>
176         /// The m no
177         /// </summary>
178         private string m_NO;
179         /// <summary>
180         /// Gets or sets the NO.
181         /// </summary>
182         /// <value>The no.</value>
183         [Description("編號"), Category("自定義")]
184         public string NO
185         {
186             get { return m_NO; }
187             set
188             {
189                 m_NO = value;
190                 Refresh();
191             }
192         }

重繪

  1  protected override void OnPaint(PaintEventArgs e)
  2         {
  3             base.OnPaint(e);
  4             var g = e.Graphics;
  5             g.SetGDIHigh();
  6             //寫文字
  7             var size = g.MeasureString(title, Font);
  8             g.DrawString(title, Font, new SolidBrush(ForeColor), new PointF((this.Width - size.Width) / 2, 2));
  9 
 10             //畫空瓶子
 11             GraphicsPath pathPS = new GraphicsPath();
 12             Point[] psPS = new Point[] 
 13             {       
 14                 new Point(m_workingRect.Left, m_workingRect.Top),
 15                 new Point(m_workingRect.Right - 1, m_workingRect.Top),
 16                 new Point(m_workingRect.Right - 1, m_workingRect.Bottom - 15),
 17                 new Point(m_workingRect.Right - 1 - m_workingRect.Width / 4, m_workingRect.Bottom),
 18                 new Point(m_workingRect.Left + m_workingRect.Width / 4, m_workingRect.Bottom),
 19                 new Point(m_workingRect.Left, m_workingRect.Bottom - 15),
 20             };
 21             pathPS.AddLines(psPS);
 22             pathPS.CloseAllFigures();
 23             g.FillPath(new SolidBrush(bottleColor), pathPS);
 24             //畫液體
 25             decimal decYTHeight = (m_value / maxValue) * m_workingRect.Height;
 26             GraphicsPath pathYT = new GraphicsPath();
 27             Rectangle rectYT = Rectangle.Empty;
 28             if (decYTHeight < 15)
 29             {
 30                 PointF[] psYT = new PointF[] 
 31                 { 
 32                     new PointF((float)(m_workingRect.Left+(15-decYTHeight))+3,(float)(m_workingRect.Bottom-decYTHeight)),                   
 33                     new PointF((float)(m_workingRect.Right-(15-decYTHeight))-3,(float)(m_workingRect.Bottom-decYTHeight)),  
 34                     new PointF(m_workingRect.Right-1-m_workingRect.Width/4, m_workingRect.Bottom),
 35                     new PointF(m_workingRect.Left+m_workingRect.Width/4, m_workingRect.Bottom),
 36                 };
 37                 pathYT.AddLines(psYT);
 38                 pathYT.CloseAllFigures();
 39                 rectYT = new Rectangle((m_workingRect.Left + (15 - (int)decYTHeight)) + 3, m_workingRect.Bottom - (int)decYTHeight - 5, m_workingRect.Width - (int)(15 - decYTHeight) * 2 - 8, 10);
 40             }
 41             else
 42             {
 43                 PointF[] psYT = new PointF[] 
 44                 { 
 45                     new PointF(m_workingRect.Left,(float)(m_workingRect.Bottom-decYTHeight)),
 46                     new PointF(m_workingRect.Right-1,(float)(m_workingRect.Bottom-decYTHeight)),
 47                     new PointF(m_workingRect.Right-1,m_workingRect.Bottom-15),
 48                     new PointF(m_workingRect.Right-1-m_workingRect.Width/4, m_workingRect.Bottom),
 49                     new PointF(m_workingRect.Left+m_workingRect.Width/4, m_workingRect.Bottom),
 50                     new PointF(m_workingRect.Left,m_workingRect.Bottom-15),
 51                 };
 52                 pathYT.AddLines(psYT);
 53                 pathYT.CloseAllFigures();
 54                 rectYT = new Rectangle(m_workingRect.Left, m_workingRect.Bottom - (int)decYTHeight - 5, m_workingRect.Width, 10);
 55             }
 56 
 57             g.FillPath(new SolidBrush(liquidColor), pathYT);
 58             g.FillPath(new SolidBrush(Color.FromArgb(50, bottleMouthColor)), pathYT);
 59             //畫液體面
 60             g.FillEllipse(new SolidBrush(liquidColor), rectYT);
 61             g.FillEllipse(new SolidBrush(Color.FromArgb(50, Color.White)), rectYT);
 62 
 63             //畫高亮
 64             int intCount = m_workingRect.Width / 2 / 4;
 65             int intSplit = (255 - 100) / intCount;
 66             for (int i = 0; i < intCount; i++)
 67             {
 68                 int _penWidth = m_workingRect.Width / 2 - 4 * i;
 69                 if (_penWidth <= 0)
 70                     _penWidth = 1;
 71                 g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(10, Color.White)), _penWidth), new Point(m_workingRect.Width / 2, m_workingRect.Top), new Point(m_workingRect.Width / 2, m_workingRect.Bottom - 15));
 72                 if (_penWidth == 1)
 73                     break;
 74             }
 75 
 76             //畫瓶底
 77             g.FillEllipse(new SolidBrush(bottleColor), new RectangleF(m_workingRect.Left, m_workingRect.Top - 5, m_workingRect.Width - 2, 10));
 78             g.FillEllipse(new SolidBrush(Color.FromArgb(50, Color.White)), new RectangleF(m_workingRect.Left, m_workingRect.Top - 5, m_workingRect.Width - 2, 10));
 79             //畫瓶口
 80             g.FillRectangle(new SolidBrush(bottleMouthColor), new Rectangle(m_workingRect.Left + m_workingRect.Width / 4, m_workingRect.Bottom, m_workingRect.Width / 2, 15));
 81             //畫瓶頸陰影
 82             GraphicsPath pathPJ = new GraphicsPath();
 83             Point[] psPJ = new Point[] 
 84             {       
 85                 new Point(m_workingRect.Left, m_workingRect.Bottom-15),
 86                 new Point(m_workingRect.Right-1, m_workingRect.Bottom-15),
 87                 new Point(m_workingRect.Right-1-m_workingRect.Width/4, m_workingRect.Bottom),
 88                 new Point(m_workingRect.Left+m_workingRect.Width/4, m_workingRect.Bottom)               
 89             };
 90             pathPJ.AddLines(psPJ);
 91             pathPJ.CloseAllFigures();
 92             g.FillPath(new SolidBrush(Color.FromArgb(50, bottleMouthColor)), pathPJ);
 93 
 94             //寫編號
 95             if (!string.IsNullOrEmpty(m_NO))
 96             {
 97                 var nosize = g.MeasureString(m_NO, Font);
 98                 g.DrawString(m_NO, Font, new SolidBrush(ForeColor), new PointF((this.Width - nosize.Width) / 2, m_workingRect.Top + 10));
 99             }
100         }

程式碼就這麼多

完整程式碼

  1 // ***********************************************************************
  2 // Assembly         : HZH_Controls
  3 // Created          : 2019-09-05
  4 //
  5 // ***********************************************************************
  6 // <copyright file="UCBottle.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 UCBottle.
 29     /// Implements the <see cref="System.Windows.Forms.UserControl" />
 30     /// </summary>
 31     /// <seealso cref="System.Windows.Forms.UserControl" />
 32     public class UCBottle : UserControl
 33     {
 34         //瓶身區域
 35         /// <summary>
 36         /// The m working rect
 37         /// </summary>
 38         Rectangle m_workingRect;
 39 
 40         /// <summary>
 41         /// The title
 42         /// </summary>
 43         string title = "瓶子1";
 44 
 45         /// <summary>
 46         /// Gets or sets the title.
 47         /// </summary>
 48         /// <value>The title.</value>
 49         [Description("標題"), Category("自定義")]
 50         public string Title
 51         {
 52             get { return title; }
 53             set
 54             {
 55                 title = value;
 56                 ResetWorkingRect();
 57                 Refresh();
 58             }
 59         }
 60 
 61         /// <summary>
 62         /// The bottle color
 63         /// </summary>
 64         private Color bottleColor = Color.FromArgb(255, 77, 59);
 65 
 66         /// <summary>
 67         /// Gets or sets the color of the bottle.
 68         /// </summary>
 69         /// <value>The color of the bottle.</value>
 70         [Description("瓶子顏色"), Category("自定義")]
 71         public Color BottleColor
 72         {
 73             get { return bottleColor; }
 74             set
 75             {
 76                 bottleColor = value;
 77                 Refresh();
 78             }
 79         }
 80 
 81         /// <summary>
 82         /// The bottle mouth color
 83         /// </summary>
 84         private Color bottleMouthColor = Color.FromArgb(105, 105, 105);
 85 
 86         /// <summary>
 87         /// Gets or sets the color of the bottle mouth.
 88         /// </summary>
 89         /// <value>The color of the bottle mouth.</value>
 90         [Description("瓶口顏色"), Category("自定義")]
 91         public Color BottleMouthColor
 92         {
 93             get { return bottleMouthColor; }
 94             set { bottleMouthColor = value; }
 95         }
 96 
 97         /// <summary>
 98         /// The liquid color
 99         /// </summary>
100         private Color liquidColor = Color.FromArgb(3, 169, 243);
101 
102         /// <summary>
103         /// Gets or sets the color of the liquid.
104         /// </summary>
105         /// <value>The color of the liquid.</value>
106         [Description("液體顏色"), Category("自定義")]
107         public Color LiquidColor
108         {
109             get { return liquidColor; }
110             set
111             {
112                 liquidColor = value;
113                 Refresh();
114             }
115         }
116 
117 
118         /// <summary>
119         /// 獲取或設定控制元件顯示的文字的字型。
120         /// </summary>
121         /// <value>The font.</value>
122         /// <PermissionSet>
123         ///   <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
124         ///   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
125         ///   <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
126         ///   <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
127         /// </PermissionSet>
128         [Description("文字字型"), Category("自定義")]
129         public override Font Font
130         {
131             get
132             {
133                 return base.Font;
134             }
135             set
136             {
137                 base.Font = value;
138                 ResetWorkingRect();
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 maximum value
166         /// </summary>
167         private decimal maxValue = 100;
168 
169         /// <summary>
170         /// Gets or sets the maximum value.
171         /// </summary>
172         /// <value>The maximum value.</value>
173         [Description("最大值"), Category("自定義")]
174         public decimal MaxValue
175         {
176             get { return maxValue; }
177             set
178             {
179                 if (value < m_value)
180                     return;
181                 maxValue = value;
182                 Refresh();
183             }
184         }
185 
186         /// <summary>
187         /// The m value
188         /// </summary>
189         private decimal m_value = 50;
190 
191         /// <summary>
192         /// Gets or sets the value.
193         /// </summary>
194         /// <value>The value.</value>
195         [Description("值"), Category("自定義")]
196         public decimal Value
197         {
198             get { return m_value; }
199             set
200             {
201                 if (value <= 0 || value > maxValue)
202                     return;
203                 m_value = value;
204                 Refresh();
205             }
206         }
207 
208         /// <summary>
209         /// The m no
210         /// </summary>
211         private string m_NO;
212         /// <summary>
213         /// Gets or sets the NO.
214         /// </summary>
215         /// <value>The no.</value>
216         [Description("編號"), Category("自定義")]
217         public string NO
218         {
219             get { return m_NO; }
220             set
221             {
222                 m_NO = value;
223                 Refresh();
224             }
225         }
226 
227         /// <summary>
228         /// Initializes a new instance of the <see cref="UCBottle" /> class.
229         /// </summary>
230         public UCBottle()
231         {
232             this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
233             this.SetStyle(ControlStyles.DoubleBuffer, true);
234             this.SetStyle(ControlStyles.ResizeRedraw, true);
235             this.SetStyle(ControlStyles.Selectable, true);
236             this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
237             this.SetStyle(ControlStyles.UserPaint, true);
238             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
239             this.SizeChanged += UCBottle_SizeChanged;
240             this.Size = new Size(100, 150);
241         }
242 
243         /// <summary>
244         /// Handles the SizeChanged event of the UCBottle control.
245         /// </summary>
246         /// <param name="sender">The source of the event.</param>
247         /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
248         void UCBottle_SizeChanged(object sender, EventArgs e)
249         {
250             ResetWorkingRect();
251         }
252 
253         /// <summary>
254         /// Resets the working rect.
255         /// </summary>
256         private void ResetWorkingRect()
257         {
258             var g = this.CreateGraphics();
259             var size = g.MeasureString(title, Font);
260             m_workingRect = new Rectangle(0, (int)size.Height + 10, this.Width, this.Height - ((int)size.Height + 10) - 15);
261         }
262 
263         /// <summary>
264         /// 引發 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
265         /// </summary>
266         /// <param name="e">包含事件資料的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
267         protected override void OnPaint(PaintEventArgs e)
268         {
269             base.OnPaint(e);
270             var g = e.Graphics;
271             g.SetGDIHigh();
272             //寫文字
273             var size = g.MeasureString(title, Font);
274             g.DrawString(title, Font, new SolidBrush(ForeColor), new PointF((this.Width - size.Width) / 2, 2));
275 
276             //畫空瓶子
277             GraphicsPath pathPS = new GraphicsPath();
278             Point[] psPS = new Point[] 
279             {       
280                 new Point(m_workingRect.Left, m_workingRect.Top),
281                 new Point(m_workingRect.Right - 1, m_workingRect.Top),
282                 new Point(m_workingRect.Right - 1, m_workingRect.Bottom - 15),
283                 new Point(m_workingRect.Right - 1 - m_workingRect.Width / 4, m_workingRect.Bottom),
284                 new Point(m_workingRect.Left + m_workingRect.Width / 4, m_workingRect.Bottom),
285                 new Point(m_workingRect.Left, m_workingRect.Bottom - 15),
286             };
287             pathPS.AddLines(psPS);
288             pathPS.CloseAllFigures();
289             g.FillPath(new SolidBrush(bottleColor), pathPS);
290             //畫液體
291             decimal decYTHeight = (m_value / maxValue) * m_workingRect.Height;
292             GraphicsPath pathYT = new GraphicsPath();
293             Rectangle rectYT = Rectangle.Empty;
294             if (decYTHeight < 15)
295             {
296                 PointF[] psYT = new PointF[] 
297                 { 
298                     new PointF((float)(m_workingRect.Left+(15-decYTHeight))+3,(float)(m_workingRect.Bottom-decYTHeight)),                   
299                     new PointF((float)(m_workingRect.Right-(15-decYTHeight))-3,(float)(m_workingRect.Bottom-decYTHeight)),  
300                     new PointF(m_workingRect.Right-1-m_workingRect.Width/4, m_workingRect.Bottom),
301                     new PointF(m_workingRect.Left+m_workingRect.Width/4, m_workingRect.Bottom),
302                 };
303                 pathYT.AddLines(psYT);
304                 pathYT.CloseAllFigures();
305                 rectYT = new Rectangle((m_workingRect.Left + (15 - (int)decYTHeight)) + 3, m_workingRect.Bottom - (int)decYTHeight - 5, m_workingRect.Width - (int)(15 - decYTHeight) * 2 - 8, 10);
306             }
307             else
308             {
309                 PointF[] psYT = new PointF[] 
310                 { 
311                     new PointF(m_workingRect.Left,(float)(m_workingRect.Bottom-decYTHeight)),
312                     new PointF(m_workingRect.Right-1,(float)(m_workingRect.Bottom-decYTHeight)),
313                     new PointF(m_workingRect.Right-1,m_workingRect.Bottom-15),
314                     new PointF(m_workingRect.Right-1-m_workingRect.Width/4, m_workingRect.Bottom),
315                     new PointF(m_workingRect.Left+m_workingRect.Width/4, m_workingRect.Bottom),
316                     new PointF(m_workingRect.Left,m_workingRect.Bottom-15),
317                 };
318                 pathYT.AddLines(psYT);
319                 pathYT.CloseAllFigures();
320                 rectYT = new Rectangle(m_workingRect.Left, m_workingRect.Bottom - (int)decYTHeight - 5, m_workingRect.Width, 10);
321             }
322 
323             g.FillPath(new SolidBrush(liquidColor), pathYT);
324             g.FillPath(new SolidBrush(Color.FromArgb(50, bottleMouthColor)), pathYT);
325             //畫液體面
326             g.FillEllipse(new SolidBrush(liquidColor), rectYT);
327             g.FillEllipse(new SolidBrush(Color.FromArgb(50, Color.White)), rectYT);
328 
329             //畫高亮
330             int intCount = m_workingRect.Width / 2 / 4;
331             int intSplit = (255 - 100) / intCount;
332             for (int i = 0; i < intCount; i++)
333             {
334                 int _penWidth = m_workingRect.Width / 2 - 4 * i;
335                 if (_penWidth <= 0)
336                     _penWidth = 1;
337                 g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(10, Color.White)), _penWidth), new Point(m_workingRect.Width / 2, m_workingRect.Top), new Point(m_workingRect.Width / 2, m_workingRect.Bottom - 15));
338                 if (_penWidth == 1)
339                     break;
340             }
341 
342             //畫瓶底
343             g.FillEllipse(new SolidBrush(bottleColor), new RectangleF(m_workingRect.Left, m_workingRect.Top - 5, m_workingRect.Width - 2, 10));
344             g.FillEllipse(new SolidBrush(Color.FromArgb(50, Color.White)), new RectangleF(m_workingRect.Left, m_workingRect.Top - 5, m_workingRect.Width - 2, 10));
345             //畫瓶口
346             g.FillRectangle(new SolidBrush(bottleMouthColor), new Rectangle(m_workingRect.Left + m_workingRect.Width / 4, m_workingRect.Bottom, m_workingRect.Width / 2, 15));
347             //畫瓶頸陰影
348             GraphicsPath pathPJ = new GraphicsPath();
349             Point[] psPJ = new Point[] 
350             {       
351                 new Point(m_workingRect.Left, m_workingRect.Bottom-15),
352                 new Point(m_workingRect.Right-1, m_workingRect.Bottom-15),
353                 new Point(m_workingRect.Right-1-m_workingRect.Width/4, m_workingRect.Bottom),
354                 new Point(m_workingRect.Left+m_workingRect.Width/4, m_workingRect.Bottom)               
355             };
356             pathPJ.AddLines(psPJ);
357             pathPJ.CloseAllFigures();
358             g.FillPath(new SolidBrush(Color.FromArgb(50, bottleMouthColor)), pathPJ);
359 
360             //寫編號
361             if (!string.IsNullOrEmpty(m_NO))
362             {
363                 var nosize = g.MeasureString(m_NO, Font);
364                 g.DrawString(m_NO, Font, new SolidBrush(ForeColor), new PointF((this.Width - nosize.Width) / 2, m_workingRect.Top + 10));
365             }
366         }
367     }
368 }
View Code

 

最後的話

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