主頁 > .NET開發 > (七十)c#Winform自定義控制元件-餅狀圖-HZHControls

(七十)c#Winform自定義控制元件-餅狀圖-HZHControls

2020-09-11 17:38:42 .NET開發

官網

http://www.hzhcontrols.com

前提

入行已經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+畫的控制元件,不了解可以先百度下

開始

添加一個類UCPieChart ,繼承UserControl

添加一些屬性

  1  /// <summary>  2         /// The pie items  3         /// </summary>  4         private PieItem[] pieItems = new PieItem[0];  5   6         /// <summary>  7         /// The random  8         /// </summary>  9         private Random random = null; 10  11         /// <summary> 12         /// The format center 13         /// </summary> 14         private StringFormat formatCenter = null; 15  16         /// <summary> 17         /// The margin 18         /// </summary> 19         private int margin = 50; 20  21         /// <summary> 22         /// The m is render percent 23         /// </summary> 24         private bool m_IsRenderPercent = false; 25  26         /// <summary> 27         /// The percen format 28         /// </summary> 29         private string percenFormat = "{0:F2}%"; 30  31         /// <summary> 32         /// The components 33         /// </summary> 34         private IContainer components = null; 35  36         /// <summary> 37         /// Gets or sets a value indicating whether this instance is render percent. 38         /// </summary> 39         /// <value><c>true</c> if this instance is render percent; otherwise, <c>false</c>.</value> 40         [Browsable(true)] 41         [Category("自定義")] 42         [DefaultValue(false)] 43         [Description("獲取或設定是否顯示百分比占用")] 44         public bool IsRenderPercent 45         { 46             get 47             { 48                 return m_IsRenderPercent; 49             } 50             set 51             { 52                 m_IsRenderPercent = value; 53                 Invalidate(); 54             } 55         } 56  57  58         /// <summary> 59         /// Gets or sets the text margin. 60         /// </summary> 61         /// <value>The text margin.</value> 62         [Browsable(true)] 63         [Category("自定義")] 64         [Description("獲取或設定文本距離,單位為像素,默認50")] 65         [DefaultValue(50)] 66         public int TextMargin 67         { 68             get 69             { 70                 return margin; 71             } 72             set 73             { 74                 margin = value; 75                 Invalidate(); 76             } 77         } 78  79  80         /// <summary> 81         /// Gets or sets the percent format. 82         /// </summary> 83         /// <value>The percent format.</value> 84         [Browsable(true)] 85         [Category("自定義")] 86         [Description("獲取或設定文百分比文字的格式化資訊")] 87         [DefaultValue("{0:F2}%")] 88         public string PercentFormat 89         { 90             get 91             { 92                 return percenFormat; 93             } 94             set 95             { 96                 percenFormat = value; 97                 Invalidate(); 98             } 99         }100 101         /// <summary>102         /// The center of circle color103         /// </summary>104         private Color centerOfCircleColor = Color.White;105         /// <summary>106         /// Gets or sets the color of the center of circle.107         /// </summary>108         /// <value>The color of the center of circle.</value>109         [Browsable(true)]110         [Category("自定義")]111         [Description("獲取或設定圓心顏色")]112         public Color CenterOfCircleColor113         {114             get { return centerOfCircleColor; }115             set116             {117                 centerOfCircleColor = value;118                 Invalidate();119             }120         }121 122         /// <summary>123         /// The center of circle width124         /// </summary>125         private int centerOfCircleWidth = 0;126         /// <summary>127         /// Gets or sets the width of the center of circle.128         /// </summary>129         /// <value>The width of the center of circle.</value>130         [Browsable(true)]131         [Category("自定義")]132         [Description("獲取或設定圓心寬度")]133         public int CenterOfCircleWidth134         {135             get { return centerOfCircleWidth; }136             set137             {138                 if (value < 0)139                     return;140                 centerOfCircleWidth = value;141                 Invalidate();142             }143         }144 145         /// <summary>146         /// The title147         /// </summary>148         private string title;149         /// <summary>150         /// Gets or sets the ti tle.151         /// </summary>152         /// <value>The ti tle.</value>153         [Browsable(true)]154         [Category("自定義")]155         [Description("獲取或設定標題")]156         public string TiTle157         {158             get { return title; }159             set160             {161                 title = value;162                 ResetTitleHeight();163                 Invalidate();164             }165         }166         /// <summary>167         /// The title font168         /// </summary>169         private Font titleFont = new Font("微軟雅黑", 12);170         /// <summary>171         /// Gets or sets the title font.172         /// </summary>173         /// <value>The title font.</value>174         [Browsable(true)]175         [Category("自定義")]176         [Description("獲取或設定標題字體")]177         public Font TitleFont178         {179             get { return titleFont; }180             set181             {182                 titleFont = value;183                 ResetTitleHeight();184                 Invalidate();185             }186         }187 188         /// <summary>189         /// The title froe color190         /// </summary>191         private Color titleFroeColor = Color.Black;192         /// <summary>193         /// Gets or sets the color of the title froe.194         /// </summary>195         /// <value>The color of the title froe.</value>196         [Browsable(true)]197         [Category("自定義")]198         [Description("獲取或設定標題顏色")]199         public Color TitleFroeColor200         {201             get { return titleFroeColor; }202             set203             {204                 titleFroeColor = value;205                 Invalidate();206             }207         }208 209         /// <summary>210         /// The title size211         /// </summary>212         private SizeF titleSize = SizeF.Empty;213         /// <summary>214         /// Resets the height of the title.215         /// </summary>216         private void ResetTitleHeight()217         {218             if (string.IsNullOrEmpty(title))219                 titleSize = SizeF.Empty;220             else221             {222                 using (var g = this.CreateGraphics())223                 {224                     titleSize = g.MeasureString(title, titleFont);225                 }226             }227         }228 229         /// <summary>230         /// Gets or sets the data source.231         /// </summary>232         /// <value>The data source.</value>233         [Browsable(true)]234         [Category("自定義")]235         [Description("獲取或設定標題顏色")]236         [Localizable(true)]237         public PieItem[] DataSource238         {239             get { return pieItems; }240             set241             {242                 pieItems = value;243                 Invalidate();244             }245         }

重繪

  1  protected override void OnPaint(PaintEventArgs e)  2         {  3             e.Graphics.SetGDIHigh();  4   5             int width;  6             Point centerPoint = GetCenterPoint(out width);  7             Rectangle rectangle = new Rectangle(centerPoint.X - width, centerPoint.Y - width, width * 2, width * 2);  8             if (width > 0 && pieItems.Length != 0)  9             { 10                 if (!string.IsNullOrEmpty(title)) 11                     e.Graphics.DrawString(title, titleFont, new SolidBrush(titleFroeColor), new PointF((this.Width - titleSize.Width) / 2, 5));                12                 Rectangle rect = new Rectangle(rectangle.X - centerPoint.X, rectangle.Y - centerPoint.Y, rectangle.Width, rectangle.Height); 13                 e.Graphics.TranslateTransform(centerPoint.X, centerPoint.Y); 14                 e.Graphics.RotateTransform(90f); 15                 int num = pieItems.Sum((PieItem item) => item.Value); 16                 float num2 = 0f; 17                 float num3 = -90f; 18                 for (int i = 0; i < pieItems.Length; i++) 19                 { 20                     Color cItem = pieItems[i].PieColor ?? ControlHelper.Colors[i]; 21                     Pen pen = new Pen(cItem, 1f); 22                     SolidBrush solidBrush = new SolidBrush(cItem); 23                     SolidBrush solidBrush2 = new SolidBrush(cItem); 24                     Brush percentBrush = new SolidBrush(cItem); 25                     float num4 = e.Graphics.MeasureString(pieItems[i].Name, Font).Width + 3f; 26                     float num5 = (num != 0) ? Convert.ToSingle((double)pieItems[i].Value * 1.0 / (double)num * 360.0) : ((float)(360 / pieItems.Length)); 27                     e.Graphics.FillPie(solidBrush, rect, 0f, 0f - num5); 28                     e.Graphics.DrawPie(new Pen(solidBrush), rect, 0f, 0f - num5); 29                     e.Graphics.RotateTransform(0f - num5 / 2f); 30                     if (num5 < 2f) 31                     { 32                         num2 += num5; 33                     } 34                     else 35                     { 36                         num2 += num5 / 2f; 37                         int num6 = 15; 38                         if (num2 < 45f || num2 > 315f) 39                         { 40                             num6 = 20; 41                         } 42                         if (num2 > 135f && num2 < 225f) 43                         { 44                             num6 = 20; 45                         } 46                         e.Graphics.DrawLine(pen, width * 2 / 3, 0, width + num6, 0); 47                         e.Graphics.TranslateTransform(width + num6, 0f); 48                         if (num2 - num3 < 5f) 49                         { 50                         } 51                         num3 = num2; 52                         if (num2 < 180f) 53                         { 54                             e.Graphics.RotateTransform(num2 - 90f); 55                             e.Graphics.DrawLine(pen, 0f, 0f, num4, 0f); 56                             e.Graphics.DrawString(pieItems[i].Name, Font, solidBrush2, new Point(0, -Font.Height)); 57                             if (IsRenderPercent) 58                             { 59                                 e.Graphics.DrawString(string.Format(percenFormat, num5 * 100f / 360f), Font, percentBrush, new Point(0, 1)); 60                             } 61                             e.Graphics.RotateTransform(90f - num2); 62                         } 63                         else 64                         { 65                             e.Graphics.RotateTransform(num2 - 270f); 66                             e.Graphics.DrawLine(pen, 0f, 0f, num4, 0f); 67                             e.Graphics.TranslateTransform(num4 - 3f, 0f); 68                             e.Graphics.RotateTransform(180f); 69                             e.Graphics.DrawString(pieItems[i].Name, Font, solidBrush2, new Point(0, -Font.Height)); 70                             if (IsRenderPercent) 71                             { 72                                 e.Graphics.DrawString(string.Format(percenFormat, num5 * 100f / 360f), Font, percentBrush, new Point(0, 1)); 73                             } 74                             e.Graphics.RotateTransform(-180f); 75                             e.Graphics.TranslateTransform(0f - num4 + 3f, 0f); 76                             e.Graphics.RotateTransform(270f - num2); 77                         } 78                         e.Graphics.TranslateTransform(-width - num6, 0f); 79                         e.Graphics.RotateTransform(0f - num5 / 2f); 80                         num2 += num5 / 2f; 81                     } 82                     solidBrush.Dispose(); 83                     pen.Dispose(); 84                     solidBrush2.Dispose(); 85                     percentBrush.Dispose(); 86                 } 87                 e.Graphics.ResetTransform(); 88  89                 if (centerOfCircleWidth > 0) 90                 { 91                     Rectangle rectCenter = new Rectangle(rect.Left + rect.Width / 2 - centerOfCircleWidth / 2, rect.Top + rect.Height / 2 - centerOfCircleWidth / 2, centerOfCircleWidth, centerOfCircleWidth); 92                     e.Graphics.FillEllipse(new SolidBrush(centerOfCircleColor), rectCenter); 93                 } 94             } 95             else 96             { 97                 e.Graphics.FillEllipse(Brushes.AliceBlue, rectangle); 98                 e.Graphics.DrawEllipse(Pens.DodgerBlue, rectangle); 99                 e.Graphics.DrawString("無資料", Font, Brushes.DimGray, rectangle, formatCenter);100             }101             base.OnPaint(e);102         }

一些輔助函式

 1  /// <summary> 2         /// Sets the data source. 3         /// </summary> 4         /// <param name="source">The source.</param> 5         public void SetDataSource(PieItem[] source) 6         { 7             if (source != null) 8             { 9                 DataSource = source;10             }11         }12         /// <summary>13         /// Sets the data source.14         /// </summary>15         /// <param name="names">The names.</param>16         /// <param name="values">The values.</param>17         /// <exception cref="System.ArgumentNullException">18         /// names19         /// or20         /// values21         /// </exception>22         /// <exception cref="System.Exception">兩個陣列的長度不一致!</exception>23         public void SetDataSource(string[] names, int[] values)24         {25             if (names == null)26             {27                 throw new ArgumentNullException("names");28             }29             if (values == null)30             {31                 throw new ArgumentNullException("values");32             }33             if (names.Length != values.Length)34             {35                 throw new Exception("兩個陣列的長度不一致!");36             }37             pieItems = new PieItem[names.Length];38             for (int i = 0; i < names.Length; i++)39             {40                 pieItems[i] = new PieItem41                 {42                     Name = names[i],43                     Value =https://www.cnblogs.com/bfyx/p/ values[i]44                 };45             }46             Invalidate();47         }

完整代碼

  1 // ***********************************************************************  2 // Assembly         : HZH_Controls  3 // Created          : 2019-09-23  4 //  5 // ***********************************************************************  6 // <copyright file="UCPieChart.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.ComponentModel; 18 using System.ComponentModel.Design; 19 using System.Drawing; 20 using System.Drawing.Drawing2D; 21 using System.Drawing.Text; 22 using System.Linq; 23 using System.Windows.Forms; 24  25 namespace HZH_Controls.Controls 26 { 27     /// <summary> 28     /// Class UCPieChart. 29     /// Implements the <see cref="System.Windows.Forms.UserControl" /> 30     /// </summary> 31     /// <seealso cref="System.Windows.Forms.UserControl" /> 32     public class UCPieChart : UserControl 33     { 34         /// <summary> 35         /// The pie items 36         /// </summary> 37         private PieItem[] pieItems = new PieItem[0]; 38  39         /// <summary> 40         /// The random 41         /// </summary> 42         private Random random = null; 43  44         /// <summary> 45         /// The format center 46         /// </summary> 47         private StringFormat formatCenter = null; 48  49         /// <summary> 50         /// The margin 51         /// </summary> 52         private int margin = 50; 53  54         /// <summary> 55         /// The m is render percent 56         /// </summary> 57         private bool m_IsRenderPercent = false; 58  59         /// <summary> 60         /// The percen format 61         /// </summary> 62         private string percenFormat = "{0:F2}%"; 63  64         /// <summary> 65         /// The components 66         /// </summary> 67         private IContainer components = null; 68  69         /// <summary> 70         /// Gets or sets a value indicating whether this instance is render percent. 71         /// </summary> 72         /// <value><c>true</c> if this instance is render percent; otherwise, <c>false</c>.</value> 73         [Browsable(true)] 74         [Category("自定義")] 75         [DefaultValue(false)] 76         [Description("獲取或設定是否顯示百分比占用")] 77         public bool IsRenderPercent 78         { 79             get 80             { 81                 return m_IsRenderPercent; 82             } 83             set 84             { 85                 m_IsRenderPercent = value; 86                 Invalidate(); 87             } 88         } 89  90  91         /// <summary> 92         /// Gets or sets the text margin. 93         /// </summary> 94         /// <value>The text margin.</value> 95         [Browsable(true)] 96         [Category("自定義")] 97         [Description("獲取或設定文本距離,單位為像素,默認50")] 98         [DefaultValue(50)] 99         public int TextMargin100         {101             get102             {103                 return margin;104             }105             set106             {107                 margin = value;108                 Invalidate();109             }110         }111 112 113         /// <summary>114         /// Gets or sets the percent format.115         /// </summary>116         /// <value>The percent format.</value>117         [Browsable(true)]118         [Category("自定義")]119         [Description("獲取或設定文百分比文字的格式化資訊")]120         [DefaultValue("{0:F2}%")]121         public string PercentFormat122         {123             get124             {125                 return percenFormat;126             }127             set128             {129                 percenFormat = value;130                 Invalidate();131             }132         }133 134         /// <summary>135         /// The center of circle color136         /// </summary>137         private Color centerOfCircleColor = Color.White;138         /// <summary>139         /// Gets or sets the color of the center of circle.140         /// </summary>141         /// <value>The color of the center of circle.</value>142         [Browsable(true)]143         [Category("自定義")]144         [Description("獲取或設定圓心顏色")]145         public Color CenterOfCircleColor146         {147             get { return centerOfCircleColor; }148             set149             {150                 centerOfCircleColor = value;151                 Invalidate();152             }153         }154 155         /// <summary>156         /// The center of circle width157         /// </summary>158         private int centerOfCircleWidth = 0;159         /// <summary>160         /// Gets or sets the width of the center of circle.161         /// </summary>162         /// <value>The width of the center of circle.</value>163         [Browsable(true)]164         [Category("自定義")]165         [Description("獲取或設定圓心寬度")]166         public int CenterOfCircleWidth167         {168             get { return centerOfCircleWidth; }169             set170             {171                 if (value < 0)172                     return;173                 centerOfCircleWidth = value;174                 Invalidate();175             }176         }177 178         /// <summary>179         /// The title180         /// </summary>181         private string title;182         /// <summary>183         /// Gets or sets the ti tle.184         /// </summary>185         /// <value>The ti tle.</value>186         [Browsable(true)]187         [Category("自定義")]188         [Description("獲取或設定標題")]189         public string TiTle190         {191             get { return title; }192             set193             {194                 title = value;195                 ResetTitleHeight();196                 Invalidate();197             }198         }199         /// <summary>200         /// The title font201         /// </summary>202         private Font titleFont = new Font("微軟雅黑", 12);203         /// <summary>204         /// Gets or sets the title font.205         /// </summary>206         /// <value>The title font.</value>207         [Browsable(true)]208         [Category("自定義")]209         [Description("獲取或設定標題字體")]210         public Font TitleFont211         {212             get { return titleFont; }213             set214             {215                 titleFont = value;216                 ResetTitleHeight();217                 Invalidate();218             }219         }220 221         /// <summary>222         /// The title froe color223         /// </summary>224         private Color titleFroeColor = Color.Black;225         /// <summary>226         /// Gets or sets the color of the title froe.227         /// </summary>228         /// <value>The color of the title froe.</value>229         [Browsable(true)]230         [Category("自定義")]231         [Description("獲取或設定標題顏色")]232         public Color TitleFroeColor233         {234             get { return titleFroeColor; }235             set236             {237                 titleFroeColor = value;238                 Invalidate();239             }240         }241 242         /// <summary>243         /// The title size244         /// </summary>245         private SizeF titleSize = SizeF.Empty;246         /// <summary>247         /// Resets the height of the title.248         /// </summary>249         private void ResetTitleHeight()250         {251             if (string.IsNullOrEmpty(title))252                 titleSize = SizeF.Empty;253             else254             {255                 using (var g = this.CreateGraphics())256                 {257                     titleSize = g.MeasureString(title, titleFont);258                 }259             }260         }261 262         /// <summary>263         /// Gets or sets the data source.264         /// </summary>265         /// <value>The data source.</value>266         [Browsable(true)]267         [Category("自定義")]268         [Description("獲取或設定標題顏色")]269         [Localizable(true)]270         public PieItem[] DataSource271         {272             get { return pieItems; }273             set274             {275                 pieItems = value;276                 Invalidate();277             }278         }279 280         /// <summary>281         /// Initializes a new instance of the <see cref="UCPieChart"/> class.282         /// </summary>283         public UCPieChart()284         {285             InitializeComponent();286             random = new Random();287             formatCenter = new StringFormat();288             formatCenter.Alignment = StringAlignment.Center;289             formatCenter.LineAlignment = StringAlignment.Center;290             SetStyle(ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true);291             SetStyle(ControlStyles.ResizeRedraw, true);292             SetStyle(ControlStyles.OptimizedDoubleBuffer, true);293             SetStyle(ControlStyles.AllPaintingInWmPaint, true);294             pieItems = new PieItem[0];295             if (GetService(typeof(IDesignerHost)) != null || LicenseManager.UsageMode == LicenseUsageMode.Designtime)296             {297                 pieItems = new PieItem[5];298 299                 for (int i = 0; i < 5; i++)300                 {301                     pieItems[i] = new PieItem302                     {303                         Name = "Source" + (i + 1),304                         Value = https://www.cnblogs.com/bfyx/p/random.Next(10, 100)305                     };306                 }307             }308         }309 310    311 312         /// <summary>313         /// Gets the center point.314         /// </summary>315         /// <param name="width">The width.</param>316         /// <returns>Point.</returns>317         private Point GetCenterPoint(out int width)318         {319             width = Math.Min(base.Width, base.Height - (titleSize != SizeF.Empty ? ((int)titleSize.Height) : 0)) / 2 - margin - 8;320             return new Point(base.Width / 2 - 1, base.Height / 2 + (titleSize != SizeF.Empty ? ((int)titleSize.Height) : 0) - 1);321         }322 323 324         /// <summary>325         /// 引發 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件,326         /// </summary>327         /// <param name="e">包含事件資料的 <see cref="T:System.Windows.Forms.PaintEventArgs" /></param>328         protected override void OnPaint(PaintEventArgs e)329         {330             e.Graphics.SetGDIHigh();331 332             int width;333             Point centerPoint = GetCenterPoint(out width);334             Rectangle rectangle = new Rectangle(centerPoint.X - width, centerPoint.Y - width, width * 2, width * 2);335             if (width > 0 && pieItems.Length != 0)336             {337                 if (!string.IsNullOrEmpty(title))338                     e.Graphics.DrawString(title, titleFont, new SolidBrush(titleFroeColor), new PointF((this.Width - titleSize.Width) / 2, 5));               339                 Rectangle rect = new Rectangle(rectangle.X - centerPoint.X, rectangle.Y - centerPoint.Y, rectangle.Width, rectangle.Height);340                 e.Graphics.TranslateTransform(centerPoint.X, centerPoint.Y);341                 e.Graphics.RotateTransform(90f);342                 int num = pieItems.Sum((PieItem item) => item.Value);343                 float num2 = 0f;344                 float num3 = -90f;345                 for (int i = 0; i < pieItems.Length; i++)346                 {347                     Color cItem = pieItems[i].PieColor ?? ControlHelper.Colors[i];348                     Pen pen = new Pen(cItem, 1f);349                     SolidBrush solidBrush = new SolidBrush(cItem);350                     SolidBrush solidBrush2 = new SolidBrush(cItem);351                     Brush percentBrush = new SolidBrush(cItem);352                     float num4 = e.Graphics.MeasureString(pieItems[i].Name, Font).Width + 3f;353                     float num5 = (num != 0) ? Convert.ToSingle((double)pieItems[i].Value * 1.0 / (double)num * 360.0) : ((float)(360 / pieItems.Length));354                     e.Graphics.FillPie(solidBrush, rect, 0f, 0f - num5);355                     e.Graphics.DrawPie(new Pen(solidBrush), rect, 0f, 0f - num5);356                     e.Graphics.RotateTransform(0f - num5 / 2f);357                     if (num5 < 2f)358                     {359                         num2 += num5;360                     }361                     else362                     {363                         num2 += num5 / 2f;364                         int num6 = 15;365                         if (num2 < 45f || num2 > 315f)366                         {367                             num6 = 20;368                         }369                         if (num2 > 135f && num2 < 225f)370                         {371                             num6 = 20;372                         }373                         e.Graphics.DrawLine(pen, width * 2 / 3, 0, width + num6, 0);374                         e.Graphics.TranslateTransform(width + num6, 0f);375                         if (num2 - num3 < 5f)376                         {377                         }378                         num3 = num2;379                         if (num2 < 180f)380                         {381                             e.Graphics.RotateTransform(num2 - 90f);382                             e.Graphics.DrawLine(pen, 0f, 0f, num4, 0f);383                             e.Graphics.DrawString(pieItems[i].Name, Font, solidBrush2, new Point(0, -Font.Height));384                             if (IsRenderPercent)385                             {386                                 e.Graphics.DrawString(string.Format(percenFormat, num5 * 100f / 360f), Font, percentBrush, new Point(0, 1));387                             }388                             e.Graphics.RotateTransform(90f - num2);389                         }390                         else391                         {392                             e.Graphics.RotateTransform(num2 - 270f);393                             e.Graphics.DrawLine(pen, 0f, 0f, num4, 0f);394                             e.Graphics.TranslateTransform(num4 - 3f, 0f);395                             e.Graphics.RotateTransform(180f);396                             e.Graphics.DrawString(pieItems[i].Name, Font, solidBrush2, new Point(0, -Font.Height));397                             if (IsRenderPercent)398                             {399                                 e.Graphics.DrawString(string.Format(percenFormat, num5 * 100f / 360f), Font, percentBrush, new Point(0, 1));400                             }401                             e.Graphics.RotateTransform(-180f);402                             e.Graphics.TranslateTransform(0f - num4 + 3f, 0f);403                             e.Graphics.RotateTransform(270f - num2);404                         }405                         e.Graphics.TranslateTransform(-width - num6, 0f);406                         e.Graphics.RotateTransform(0f - num5 / 2f);407                         num2 += num5 / 2f;408                     }409                     solidBrush.Dispose();410                     pen.Dispose();411                     solidBrush2.Dispose();412                     percentBrush.Dispose();413                 }414                 e.Graphics.ResetTransform();415 416                 if (centerOfCircleWidth > 0)417                 {418                     Rectangle rectCenter = new Rectangle(rect.Left + rect.Width / 2 - centerOfCircleWidth / 2, rect.Top + rect.Height / 2 - centerOfCircleWidth / 2, centerOfCircleWidth, centerOfCircleWidth);419                     e.Graphics.FillEllipse(new SolidBrush(centerOfCircleColor), rectCenter);420                 }421             }422             else423             {424                 e.Graphics.FillEllipse(Brushes.AliceBlue, rectangle);425                 e.Graphics.DrawEllipse(Pens.DodgerBlue, rectangle);426                 e.Graphics.DrawString("無資料", Font, Brushes.DimGray, rectangle, formatCenter);427             }428             base.OnPaint(e);429         }430         /// <summary>431         /// Sets the data source.432         /// </summary>433         /// <param name="source">The source.</param>434         public void SetDataSource(PieItem[] source)435         {436             if (source != null)437             {438                 DataSource = source;439             }440         }441         /// <summary>442         /// Sets the data source.443         /// </summary>444         /// <param name="names">The names.</param>445         /// <param name="values">The values.</param>446         /// <exception cref="System.ArgumentNullException">447         /// names448         /// or449         /// values450         /// </exception>451         /// <exception cref="System.Exception">兩個陣列的長度不一致!</exception>452         public void SetDataSource(string[] names, int[] values)453         {454             if (names == null)455             {456                 throw new ArgumentNullException("names");457             }458             if (values == null)459             {460                 throw new ArgumentNullException("values");461             }462             if (names.Length != values.Length)463             {464                 throw new Exception("兩個陣列的長度不一致!");465             }466             pieItems = new PieItem[names.Length];467             for (int i = 0; i < names.Length; i++)468             {469                 pieItems[i] = new PieItem470                 {471                     Name = names[i],472                     Value =https://www.cnblogs.com/bfyx/p/ values[i]473                 };474             }475             Invalidate();476         }477 478         /// <summary>479         /// Releases unmanaged and - optionally - managed resources.480         /// </summary>481         /// <param name="disposing">為 true 則釋放托管資源和非托管資源;為 false 則僅釋放非托管資源,</param>482         protected override void Dispose(bool disposing)483         {484             if (disposing && components != null)485             {486                 components.Dispose();487             }488             base.Dispose(disposing);489         }490 491         /// <summary>492         /// Initializes the component.493         /// </summary>494         private void InitializeComponent()495         {496             SuspendLayout();497             base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;498             BackColor = System.Drawing.Color.Transparent;499             base.Name = "UCPieChart";500             ResumeLayout(false);501         }502     }503 }
View Code

 

最后的話

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

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/6453.html

標籤:WinForm

上一篇:Winform中設定ZedGraph的字體和間距不隨圖形的縮放而縮放

下一篇:Winform中設定和獲取DevExpress的RadioGroup的選中項的value值

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • WebAPI簡介

    Web體系結構: 有三個核心:資源(resource),URL(統一資源識別符號)和表示 他們的關系是這樣的:一個資源由一個URL進行標識,HTTP客戶端使用URL定位資源,表示是從資源回傳資料,媒體型別是資源回傳的資料格式。 接下來我們說下HTTP. HTTP協議的系統是一種無狀態的方式,使用請求/ ......

    uj5u.com 2020-09-09 22:07:47 more
  • asp.net core 3.1 入口:Program.cs中的Main函式

    本文分析Program.cs 中Main()函式中代碼的運行順序分析asp.net core程式的啟動,重點不是剖析原始碼,而是理清程式開始時執行的順序。到呼叫了哪些實體,哪些法方。asp.net core 3.1 的程式入口在專案Program.cs檔案里,如下。ususing System; us ......

    uj5u.com 2020-09-09 22:07:49 more
  • asp.net網站作為websocket服務端的應用該如何寫

    最近被websocket的一個問題困擾了很久,有一個需求是在web網站中搭建websocket服務。客戶端通過網頁與服務器建立連接,然后服務器根據ip給客戶端網頁發送資訊。 其實,這個需求并不難,只是剛開始對websocket的內容不太了解。上網搜索了一下,有通過asp.net core 實作的、有 ......

    uj5u.com 2020-09-09 22:08:02 more
  • ASP.NET 開源匯入匯出庫Magicodes.IE Docker中使用

    Magicodes.IE在Docker中使用 更新歷史 2019.02.13 【Nuget】版本更新到2.0.2 【匯入】修復單列匯入的Bug,單元測驗“OneColumnImporter_Test”。問題見(https://github.com/dotnetcore/Magicodes.IE/is ......

    uj5u.com 2020-09-09 22:08:05 more
  • 在webform中使用ajax

    如果你用過Asp.net webform, 說明你也算是.NET 開發的老兵了。WEBform應該是2011 2013左右,當時還用visual studio 2005、 visual studio 2008。后來基本都用的是MVC。 如果是新開發的專案,估計沒人會用webform技術。但是有些舊版 ......

    uj5u.com 2020-09-09 22:08:50 more
  • iis添加asp.net網站,訪問提示:由于擴展配置問題而無法提供您請求的

    今天在iis服務器配置asp.net網站,遇到一個問題,記錄一下: 問題:由于擴展配置問題而無法提供您請求的頁面。如果該頁面是腳本,請添加處理程式。如果應下載檔案,請添加 MIME 映射。 WindowServer2012服務器,添加角色安裝完.netframework和iis之后,運行aspx頁面 ......

    uj5u.com 2020-09-09 22:10:00 more
  • WebAPI-處理架構

    帶著問題去思考,大家好! 問題1:HTTP請求和回傳相應的HTTP回應資訊之間發生了什么? 1:首先是最底層,托管層,位于WebAPI和底層HTTP堆疊之間 2:其次是 訊息處理程式管道層,這里比如日志和快取。OWIN的參考是將訊息處理程式管道的一些功能下移到堆疊下端的OWIN中間件了。 3:控制器處理 ......

    uj5u.com 2020-09-09 22:11:13 more
  • 微信門戶開發框架-使用指導說明書

    微信門戶應用管理系統,采用基于 MVC + Bootstrap + Ajax + Enterprise Library的技術路線,界面層采用Boostrap + Metronic組合的前端框架,資料訪問層支持Oracle、SQLServer、MySQL、PostgreSQL等資料庫。框架以MVC5,... ......

    uj5u.com 2020-09-09 22:15:18 more
  • WebAPI-HTTP編程模型

    帶著問題去思考,大家好!它是什么?它包含什么?它能干什么? 訊息 HTTP編程模型的核心就是訊息抽象,表示為:HttPRequestMessage,HttpResponseMessage.用于客戶端和服務端之間交換請求和回應訊息。 HttpMethod類包含了一組靜態屬性: private stat ......

    uj5u.com 2020-09-09 22:15:23 more
  • 部署WebApi隨筆

    一、跨域 NuGet參考Microsoft.AspNet.WebApi.Cors WebApiConfig.cs中配置: // Web API 配置和服務 config.EnableCors(new EnableCorsAttribute("*", "*", "*")); 二、清除默認回傳XML格式 ......

    uj5u.com 2020-09-09 22:15:48 more
最新发布
  • C#多執行緒學習(二) 如何操縱一個執行緒

    <a href="https://www.cnblogs.com/x-zhi/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/2943582/20220801082530.png" alt="" /></...

    uj5u.com 2023-04-19 09:17:20 more
  • C#多執行緒學習(二) 如何操縱一個執行緒

    C#多執行緒學習(二) 如何操縱一個執行緒 執行緒學習第一篇:C#多執行緒學習(一) 多執行緒的相關概念 下面我們就動手來創建一個執行緒,使用Thread類創建執行緒時,只需提供執行緒入口即可。(執行緒入口使程式知道該讓這個執行緒干什么事) 在C#中,執行緒入口是通過ThreadStart代理(delegate)來提供的 ......

    uj5u.com 2023-04-19 09:16:49 more
  • 記一次 .NET某醫療器械清洗系統 卡死分析

    <a href="https://www.cnblogs.com/huangxincheng/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/214741/20200614104537.png" alt="" /&g...

    uj5u.com 2023-04-18 08:39:04 more
  • 記一次 .NET某醫療器械清洗系統 卡死分析

    一:背景 1. 講故事 前段時間協助訓練營里的一位朋友分析了一個程式卡死的問題,回過頭來看這個案例比較經典,這篇稍微整理一下供后來者少踩坑吧。 二:WinDbg 分析 1. 為什么會卡死 因為是表單程式,理所當然就是看主執行緒此時正在做什么? 可以用 ~0s ; k 看一下便知。 0:000> k # ......

    uj5u.com 2023-04-18 08:33:10 more
  • SignalR, No Connection with that ID,IIS

    <a href="https://www.cnblogs.com/smartstar/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/u36196.jpg" alt="" /></a>...

    uj5u.com 2023-03-30 17:21:52 more
  • 一次對pool的誤用導致的.net頻繁gc的診斷分析

    <a href="https://www.cnblogs.com/dotnet-diagnostic/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/3115652/20230225090434.png" alt=""...

    uj5u.com 2023-03-28 10:15:33 more
  • 一次對pool的誤用導致的.net頻繁gc的診斷分析

    <a href="https://www.cnblogs.com/dotnet-diagnostic/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/3115652/20230225090434.png" alt=""...

    uj5u.com 2023-03-28 10:13:31 more
  • C#遍歷指定檔案夾中所有檔案的3種方法

    <a href="https://www.cnblogs.com/xbhp/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/957602/20230310105611.png" alt="" /></a&...

    uj5u.com 2023-03-27 14:46:55 more
  • C#/VB.NET:如何將PDF轉為PDF/A

    <a href="https://www.cnblogs.com/Carina-baby/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/2859233/20220427162558.png" alt="" />...

    uj5u.com 2023-03-27 14:46:35 more
  • 武裝你的WEBAPI-OData聚合查詢

    <a href="https://www.cnblogs.com/podolski/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/616093/20140323000327.png" alt="" /><...

    uj5u.com 2023-03-27 14:46:16 more