前提 入行已经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 用处及效果 调用示例 复制代码 1 this.ucBarChart1.SetDataSource(new double[] { random.Next(50, 100), random.Next(50, 100), random.Next(50, 100), random.Next(50, 100), random.Next(50, 100) }); 2 this.ucBarChart2.SetDataSource(new double[] { random.Next(50, 100), random.Next(50, 100), random.Next(50, 100), random.Next(50, 100), random.Next(50, 100) }, new string[] { "张三", "李四", "王五", "赵六", "田七" }); 3 this.ucBarChart3.SetDataSource(new double[] { random.Next(50, 100), random.Next(50, 100), random.Next(50, 100), random.Next(50, 100), random.Next(50, 100) }, new string[] { "张三", "李四", "王五", "赵六", "田七" }); 4 5 this.ucBarChart4.SetDataSource(new double[] { random.Next(50, 100), random.Next(50, 100), random.Next(50, 100), random.Next(50, 100), random.Next(50, 100) }, new string[] { "张三", "李四", "王五", "赵六", "田七" }); 6 this.ucBarChart4.AddAuxiliaryLine(60, Color.Red, "及格线超长占位符"); 7 8 this.ucBarChart5.SetDataSource(new double[] { random.Next(50, 100), random.Next(50, 100), random.Next(50, 100), random.Next(50, 100), random.Next(50, 100) }, new string[] { "张三", "李四", "王五", "赵六", "田七" }); 9 this.ucBarChart5.AddAuxiliaryLine(50, Color.Black, "及格线"); 10 11 12 var ds = new double[5][]; 13 for (int i = 0; i < ds.Length; i++) 14 { 15 ds[i] = new double[] { random.Next(50, 100), random.Next(50, 100), random.Next(50, 100) }; 16 } 17 this.ucBarChart6.BarChartItems = new HZH_Controls.Controls.BarChartItem[] { new HZH_Controls.Controls.BarChartItem(Color.Red, "语文"), new HZH_Controls.Controls.BarChartItem(Color.Blue, "英语"), new HZH_Controls.Controls.BarChartItem(Color.Orange, "数学") }; 18 this.ucBarChart6.SetDataSource(ds, new string[] { "张三", "李四", "王五", "赵六", "田七" }); 19 this.ucBarChart6.AddAuxiliaryLine(60, Color.Black); 20 21 var ds2 = new List(); 22 for (int i = 0; i < 20; i++) 23 { 24 ds2.Add(new double[] { random.Next(800, 2000), random.Next(100, 200) }); 25 } 26 var titles = new List(); 27 double dblSum = ds2.Sum(p=>p[0]); 28 for (int i = 0; i < ds2.Count; i++) 29 { 30 titles.Add("员工" + (i + 1) + "\n" + (ds2[i][0] / dblSum).ToString("0.0%")); 31 } 32 this.ucBarChart7.BarChartItems = new HZH_Controls.Controls.BarChartItem[] { new HZH_Controls.Controls.BarChartItem(Color.Green, "合格"), new HZH_Controls.Controls.BarChartItem(Color.Red, "次品") }; 33 this.ucBarChart7.SetDataSource(ds2.ToArray(), titles.ToArray()); 34 this.ucBarChart7.AddAuxiliaryLine(1000, Color.Black, "标准线"); 复制代码 准备工作 GDI画图,不懂可以先百度一下 另外用到了(一)c#Winform自定义控件-基类控件 不知道的可以先前往看一下 开始 我们先理一下思路, 我们需要值,x轴,y轴,辅助线,项列表,标题,以及一些颜色等 添加一个类UCBarChart ,继承UCControlBase 添加一些控制属性 复制代码 1 /// 2 /// The auxiliary lines 3 /// 4 private List auxiliary_lines; 5 6 /// 7 /// The value maximum left 8 /// 9 private int value_max_left = -1; 10 11 /// 12 /// The value minimum left 13 /// 14 private int value_min_left = 0; 15 16 /// 17 /// The value segment 18 /// 19 private int value_Segment = 5; 20 21 /// 22 /// The data values 23 /// 24 private double[][] data_values = null; 25 26 /// 27 /// The data texts 28 /// 29 private string[] data_texts = null; 30 31 ///// 32 ///// The data colors 33 ///// 34 //private Color[] data_colors = null; 35 36 /// 37 /// The brush deep 38 /// 39 private Brush brush_deep = null; 40 41 /// 42 /// The pen normal 43 /// 44 private Pen pen_normal = null; 45 46 /// 47 /// The pen dash 48 /// 49 private Pen pen_dash = null; 50 51 /// 52 /// The bar back color 53 /// 54 //private Color barBackColor = Color.FromArgb(255, 77, 59); 55 56 /// 57 /// The use gradient 58 /// 59 private bool useGradient = false; 60 61 /// 62 /// The color deep 63 /// 64 private Color color_deep = Color.FromArgb(150, 255, 77, 59); 65 66 /// 67 /// The color dash 68 /// 69 private Color color_dash = Color.FromArgb(50, 255, 77, 59); 70 71 /// 72 /// The format left 73 /// 74 private StringFormat format_left = null; 75 76 /// 77 /// The format right 78 /// 79 private StringFormat format_right = null; 80 81 /// 82 /// The format center 83 /// 84 private StringFormat format_center = null; 85 86 /// 87 /// The value is render dash line 88 /// 89 private bool value_IsRenderDashLine = true; 90 91 /// 92 /// The is show bar value 93 /// 94 private bool isShowBarValue = true; 95 96 /// 97 /// The show bar value format 98 /// 99 private string showBarValueFormat = "{0}"; 100 101 /// 102 /// The value title 103 /// 104 private string value_title = ""; 105 106 /// 107 /// The bar percent width 108 /// 109 private float barPercentWidth = 0.9f; 110 111 /// 112 /// The components 113 /// 114 private IContainer components = null; 115 116 /// 117 /// 获取或设置控件的背景色。 118 /// 119 /// The color of the back. 120 /// 121 /// 122 /// 123 [Browsable(true)] 124 [Description("获取或设置控件的背景色")] 125 [Category("自定义")] 126 [DefaultValue(typeof(Color), "Transparent")] 127 [EditorBrowsable(EditorBrowsableState.Always)] 128 public override Color BackColor 129 { 130 get 131 { 132 return base.BackColor; 133 } 134 set 135 { 136 base.BackColor = value; 137 } 138 } 139 140 /// 141 /// Gets or sets the text. 142 /// 143 /// The text. 144 [Browsable(true)] 145 [Description("获取或设置当前控件的文本")] 146 [Category("自定义")] 147 [EditorBrowsable(EditorBrowsableState.Always)] 148 [Bindable(true)] 149 [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] 150 public override string Text 151 { 152 get 153 { 154 return base.Text; 155 } 156 set 157 { 158 base.Text = value; 159 Invalidate(); 160 } 161 } 162 163 /// 164 /// 获取或设置控件的前景色。 165 /// 166 /// The color of the fore. 167 /// 168 /// 169 /// 170 [Browsable(true)] 171 [Description("获取或设置控件的前景色")] 172 [Category("自定义")] 173 [EditorBrowsable(EditorBrowsableState.Always)] 174 public override Color ForeColor 175 { 176 get 177 { 178 return base.ForeColor; 179 } 180 set 181 { 182 base.ForeColor = value; 183 } 184 } 185 186 /// 187 /// Gets or sets the color lines and text. 188 /// 189 /// The color lines and text. 190 [Category("自定义")] 191 [Description("获取或设置坐标轴及相关信息文本的颜色")] 192 [Browsable(true)] 193 [DefaultValue(typeof(Color), "DimGray")] 194 public Color ColorLinesAndText 195 { 196 get 197 { 198 return color_deep; 199 } 200 set 201 { 202 color_deep = value; 203 InitializationColor(); 204 Invalidate(); 205 } 206 } 207 208 /// 209 /// Gets or sets a value indicating whether [use gradient]. 210 /// 211 /// true if [use gradient]; otherwise, false. 212 [Category("自定义")] 213 [Description("获取或设置本条形图控件是否使用渐进色")] 214 [Browsable(true)] 215 [DefaultValue(false)] 216 public bool UseGradient 217 { 218 get 219 { 220 return useGradient; 221 } 222 set 223 { 224 useGradient = value; 225 Invalidate(); 226 } 227 } 228 229 /// 230 /// Gets or sets the color dash lines. 231 /// 232 /// The color dash lines. 233 [Category("自定义")] 234 [Description("获取或设置虚线的颜色")] 235 [Browsable(true)] 236 [DefaultValue(typeof(Color), "LightGray")] 237 public Color ColorDashLines 238 { 239 get 240 { 241 return color_dash; 242 } 243 set 244 { 245 color_dash = value; 246 if (pen_dash != null) 247 pen_dash.Dispose(); 248 pen_dash = new Pen(color_dash); 249 Invalidate(); 250 } 251 } 252 253 /// 254 /// Gets or sets a value indicating whether this instance is render dash line. 255 /// 256 /// true if this instance is render dash line; otherwise, false. 257 [Category("自定义")] 258 [Description("获取或设置虚线是否进行显示")] 259 [Browsable(true)] 260 [DefaultValue(true)] 261 public bool IsRenderDashLine 262 { 263 get 264 { 265 return value_IsRenderDashLine; 266 } 267 set 268 { 269 value_IsRenderDashLine = value; 270 Invalidate(); 271 } 272 } 273 274 /// 275 /// Gets or sets the value segment. 276 /// 277 /// The value segment. 278 [Category("自定义")] 279 [Description("获取或设置图形的纵轴分段数")] 280 [Browsable(true)] 281 [DefaultValue(5)] 282 public int ValueSegment 283 { 284 get 285 { 286 return value_Segment; 287 } 288 set 289 { 290 value_Segment = value; 291 Invalidate(); 292 } 293 } 294 295 /// 296 /// Gets or sets the value maximum left. 297 /// 298 /// The value maximum left. 299 [Category("自定义")] 300 [Description("获取或设置图形的左纵坐标的最大值,该值必须大于最小值,该值为负数,最大值即为自动适配。")] 301 [Browsable(true)] 302 [DefaultValue(-1)] 303 public int ValueMaxLeft 304 { 305 get 306 { 307 return value_max_left; 308 } 309 set 310 { 311 value_max_left = value; 312 Invalidate(); 313 } 314 } 315 316 /// 317 /// Gets or sets the value minimum left. 318 /// 319 /// The value minimum left. 320 [Category("自定义")] 321 [Description("获取或设置图形的左纵坐标的最小值,该值必须小于最大值")] 322 [Browsable(true)] 323 [DefaultValue(0)] 324 public int ValueMinLeft 325 { 326 get 327 { 328 return value_min_left; 329 } 330 set 331