(六十三)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://blog.csdn.net/kwwwvagaa/article/details/100586547
用处及效果
准备工作
依然用GDI+,请自行百度了解
开始
添加一个类UCArrow,继承UserControl
添加枚举,控制方向
1 /// <summary>2 /// Enum ArrowDirection3 /// </summary>4 public enum ArrowDirection5 {6 /// <summary>7 /// The left8 /// </summary>9 Left,
10 /// <summary>
11 /// The right
12 /// </summary>
13 Right,
14 /// <summary>
15 /// The top
16 /// </summary>
17 Top,
18 /// <summary>
19 /// The bottom
20 /// </summary>
21 Bottom,
22 /// <summary>
23 /// The left right
24 /// </summary>
25 Left_Right,
26 /// <summary>
27 /// The top bottom
28 /// </summary>
29 Top_Bottom
30 }
一些属性
1 /// <summary>2 /// The arrow color3 /// </summary>4 private Color arrowColor = Color.FromArgb(255, 77, 59);5 6 /// <summary>7 /// Gets or sets the color of the arrow.8 /// </summary>9 /// <value>The color of the arrow.</value>10 [Description("箭头颜色"), Category("自定义")]11 public Color ArrowColor12 {13 get { return arrowColor; }14 set15 {16 arrowColor = value;17 Refresh();18 }19 }20 21 /// <summary>22 /// The border color23 /// </summary>24 private Color? borderColor = null;25 26 /// <summary>27 /// Gets or sets the color of the border.28 /// </summary>29 /// <value>The color of the border.</value>30 [Description("箭头边框颜色,为空则无边框"), Category("自定义")]31 public Color? BorderColor32 {33 get { return borderColor; }34 set35 {36 borderColor = value;37 Refresh();38 }39 }40 41 /// <summary>42 /// The direction43 /// </summary>44 private ArrowDirection direction = ArrowDirection.Right;45 46 /// <summary>47 /// Gets or sets the direction.48 /// </summary>49 /// <value>The direction.</value>50 [Description("箭头方向"), Category("自定义")]51 public ArrowDirection Direction52 {53 get { return direction; }54 set55 {56 direction = value;57 ResetPath();58 Refresh();59 }60 }61 /// <summary>62 /// 获取或设置控件显示的文字的字体。63 /// </summary>64 /// <value>The font.</value>65 /// <PermissionSet>66 /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />67 /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />68 /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />69 /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />70 /// </PermissionSet>71 public override Font Font72 {73 get74 {75 return base.Font;76 }77 set78 {79 base.Font = value;80 Refresh();81 }82 }83 /// <summary>84 /// 获取或设置控件的前景色。85 /// </summary>86 /// <value>The color of the fore.</value>87 /// <PermissionSet>88 /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />89 /// </PermissionSet>90 public override Color ForeColor91 {92 get93 {94 return base.ForeColor;95 }96 set97 {98 base.ForeColor = value;99 Refresh();
100 }
101 }
102 /// <summary>
103 /// The text
104 /// </summary>
105 private string text;
106 /// <summary>
107 /// Gets or sets the text.
108 /// </summary>
109 /// <value>The text.</value>
110 [Bindable(true)]
111 [Browsable(true)]
112 [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
113 [EditorBrowsable(EditorBrowsableState.Always)]
114 [Localizable(true)]
115 [Description("箭头文字"), Category("自定义")]
116 public override string Text
117 {
118 get
119 {
120 return text;
121 }
122 set
123 {
124 text = value;
125 Refresh();
126 }
127 }
128 /// <summary>
129 /// The m path
130 /// </summary>
131 GraphicsPath m_path;
根据方向和大小设置path
1 private void ResetPath()2 {3 Point[] ps = null;4 switch (direction)5 {6 case ArrowDirection.Left:7 ps = new Point[] 8 { 9 new Point(0,this.Height/2),
10 new Point(40,0),
11 new Point(40,this.Height/4),
12 new Point(this.Width-1,this.Height/4),
13 new Point(this.Width-1,this.Height-this.Height/4),
14 new Point(40,this.Height-this.Height/4),
15 new Point(40,this.Height),
16 new Point(0,this.Height/2)
17 };
18 break;
19 case ArrowDirection.Right:
20 ps = new Point[]
21 {
22 new Point(0,this.Height/4),
23 new Point(this.Width-40,this.Height/4),
24 new Point(this.Width-40,0),
25 new Point(this.Width-1,this.Height/2),
26 new Point(this.Width-40,this.Height),
27 new Point(this.Width-40,this.Height-this.Height/4),
28 new Point(0,this.Height-this.Height/4),
29 new Point(0,this.Height/4)
30 };
31 break;
32 case ArrowDirection.Top:
33 ps = new Point[]
34 {
35 new Point(this.Width/2,0),
36 new Point(this.Width,40),
37 new Point(this.Width-this.Width/4,40),
38 new Point(this.Width-this.Width/4,this.Height-1),
39 new Point(this.Width/4,this.Height-1),
40 new Point(this.Width/4,40),
41 new Point(0,40),
42 new Point(this.Width/2,0),
43 };
44 break;
45 case ArrowDirection.Bottom:
46 ps = new Point[]
47 {
48 new Point(this.Width-this.Width/4,0),
49 new Point(this.Width-this.Width/4,this.Height-40),
50 new Point(this.Width,this.Height-40),
51 new Point(this.Width/2,this.Height-1),
52 new Point(0,this.Height-40),
53 new Point(this.Width/4,this.Height-40),
54 new Point(this.Width/4,0),
55 new Point(this.Width-this.Width/4,0),
56 };
57 break;
58 case ArrowDirection.Left_Right:
59 ps = new Point[]
60 {
61 new Point(0,this.Height/2),
62 new Point(40,0),
63 new Point(40,this.Height/4),
64 new Point(this.Width-40,this.Height/4),
65 new Point(this.Width-40,0),
66 new Point(this.Width-1,this.Height/2),
67 new Point(this.Width-40,this.Height),
68 new Point(this.Width-40,this.Height-this.Height/4),
69 new Point(40,this.Height-this.Height/4),
70 new Point(40,this.Height),
71 new Point(0,this.Height/2),
72 };
73 break;
74 case ArrowDirection.Top_Bottom:
75 ps = new Point[]
76 {
77 new Point(this.Width/2,0),
78 new Point(this.Width,40),
79 new Point(this.Width-this.Width/4,40),
80 new Point(this.Width-this.Width/4,this.Height-40),
81 new Point(this.Width,this.Height-40),
82 new Point(this.Width/2,this.Height-1),
83 new Point(0,this.Height-40),
84 new Point(this.Width/4,this.Height-40),
85 new Point(this.Width/4,40),
86 new Point(0,40),
87 new Point(this.Width/2,0),
88 };
89 break;
90 }
91 m_path = new GraphicsPath();
92 m_path.AddLines(ps);
93 m_path.CloseAllFigures();
94 }
重绘
1 protected override void OnPaint(PaintEventArgs e)2 {3 base.OnPaint(e);4 var g = e.Graphics;5 g.SetGDIHigh();6 7 g.FillPath(new SolidBrush(arrowColor), m_path);8 9 if (borderColor != null && borderColor != Color.Empty)
10 g.DrawPath(new Pen(new SolidBrush(borderColor.Value)), m_path);
11 if (!string.IsNullOrEmpty(text))
12 {
13 var size = g.MeasureString(Text, Font);
14 g.DrawString(Text, Font, new SolidBrush(ForeColor), new PointF((this.Width - size.Width) / 2, (this.Height - size.Height) / 2));
15 }
16 }
完整代码
// ***********************************************************************
// Assembly : HZH_Controls
// Created : 2019-09-10
//
// ***********************************************************************
// <copyright file="UCArrow.cs">
// Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
// </copyright>
//
// Blog: https://www.cnblogs.com/bfyx
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
//
// If you use this code, please keep this note.
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;namespace HZH_Controls.Controls
{/// <summary>/// Class UCArrow./// Implements the <see cref="System.Windows.Forms.UserControl" />/// </summary>/// <seealso cref="System.Windows.Forms.UserControl" />public class UCArrow : UserControl{/// <summary>/// The arrow color/// </summary>private Color arrowColor = Color.FromArgb(255, 77, 59);/// <summary>/// Gets or sets the color of the arrow./// </summary>/// <value>The color of the arrow.</value>[Description("箭头颜色"), Category("自定义")]public Color ArrowColor{get { return arrowColor; }set{arrowColor = value;Refresh();}}/// <summary>/// The border color/// </summary>private Color? borderColor = null;/// <summary>/// Gets or sets the color of the border./// </summary>/// <value>The color of the border.</value>[Description("箭头边框颜色,为空则无边框"), Category("自定义")]public Color? BorderColor{get { return borderColor; }set{borderColor = value;Refresh();}}/// <summary>/// The direction/// </summary>private ArrowDirection direction = ArrowDirection.Right;/// <summary>/// Gets or sets the direction./// </summary>/// <value>The direction.</value>[Description("箭头方向"), Category("自定义")]public ArrowDirection Direction{get { return direction; }set{direction = value;ResetPath();Refresh();}}/// <summary>/// 获取或设置控件显示的文字的字体。/// </summary>/// <value>The font.</value>/// <PermissionSet>/// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />/// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />/// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />/// </PermissionSet>public override Font Font{get{return base.Font;}set{base.Font = value;Refresh();}}/// <summary>/// 获取或设置控件的前景色。/// </summary>/// <value>The color of the fore.</value>/// <PermissionSet>/// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />/// </PermissionSet>public override Color ForeColor{get{return base.ForeColor;}set{base.ForeColor = value;Refresh();}}/// <summary>/// The text/// </summary>private string text;/// <summary>/// Gets or sets the text./// </summary>/// <value>The text.</value>[Bindable(true)][Browsable(true)][DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)][EditorBrowsable(EditorBrowsableState.Always)][Localizable(true)][Description("箭头文字"), Category("自定义")]public override string Text{get{return text;}set{text = value;Refresh();}}/// <summary>/// The m path/// </summary>GraphicsPath m_path;/// <summary>/// Initializes a new instance of the <see cref="UCArrow"/> class./// </summary>public UCArrow(){this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);this.SetStyle(ControlStyles.DoubleBuffer, true);this.SetStyle(ControlStyles.ResizeRedraw, true);this.SetStyle(ControlStyles.Selectable, true);this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);this.SetStyle(ControlStyles.UserPaint, true);this.ForeColor = Color.White;this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;this.SizeChanged += UCArrow_SizeChanged;this.Size = new Size(100, 50);}/// <summary>/// Handles the SizeChanged event of the UCArrow control./// </summary>/// <param name="sender">The source of the event.</param>/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>void UCArrow_SizeChanged(object sender, EventArgs e){ResetPath();}/// <summary>/// Resets the path./// </summary>private void ResetPath(){Point[] ps = null;switch (direction){case ArrowDirection.Left:ps = new Point[] { new Point(0,this.Height/2),new Point(40,0),new Point(40,this.Height/4),new Point(this.Width-1,this.Height/4),new Point(this.Width-1,this.Height-this.Height/4),new Point(40,this.Height-this.Height/4),new Point(40,this.Height),new Point(0,this.Height/2)};break;case ArrowDirection.Right:ps = new Point[] {new Point(0,this.Height/4),new Point(this.Width-40,this.Height/4),new Point(this.Width-40,0),new Point(this.Width-1,this.Height/2),new Point(this.Width-40,this.Height),new Point(this.Width-40,this.Height-this.Height/4), new Point(0,this.Height-this.Height/4),new Point(0,this.Height/4)};break;case ArrowDirection.Top:ps = new Point[] {new Point(this.Width/2,0),new Point(this.Width,40),new Point(this.Width-this.Width/4,40),new Point(this.Width-this.Width/4,this.Height-1),new Point(this.Width/4,this.Height-1),new Point(this.Width/4,40),new Point(0,40),new Point(this.Width/2,0),};break;case ArrowDirection.Bottom:ps = new Point[] {new Point(this.Width-this.Width/4,0),new Point(this.Width-this.Width/4,this.Height-40),new Point(this.Width,this.Height-40),new Point(this.Width/2,this.Height-1),new Point(0,this.Height-40),new Point(this.Width/4,this.Height-40),new Point(this.Width/4,0),new Point(this.Width-this.Width/4,0), };break;case ArrowDirection.Left_Right:ps = new Point[] { new Point(0,this.Height/2),new Point(40,0),new Point(40,this.Height/4),new Point(this.Width-40,this.Height/4),new Point(this.Width-40,0),new Point(this.Width-1,this.Height/2),new Point(this.Width-40,this.Height),new Point(this.Width-40,this.Height-this.Height/4),new Point(40,this.Height-this.Height/4),new Point(40,this.Height),new Point(0,this.Height/2), };break;case ArrowDirection.Top_Bottom:ps = new Point[] {new Point(this.Width/2,0),new Point(this.Width,40),new Point(this.Width-this.Width/4,40),new Point(this.Width-this.Width/4,this.Height-40),new Point(this.Width,this.Height-40),new Point(this.Width/2,this.Height-1),new Point(0,this.Height-40),new Point(this.Width/4,this.Height-40),new Point(this.Width/4,40),new Point(0,40),new Point(this.Width/2,0), };break;}m_path = new GraphicsPath();m_path.AddLines(ps);m_path.CloseAllFigures();}/// <summary>/// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。/// </summary>/// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>protected override void OnPaint(PaintEventArgs e){base.OnPaint(e);var g = e.Graphics;g.SetGDIHigh();g.FillPath(new SolidBrush(arrowColor), m_path);if (borderColor != null && borderColor != Color.Empty)g.DrawPath(new Pen(new SolidBrush(borderColor.Value)), m_path);if (!string.IsNullOrEmpty(text)){var size = g.MeasureString(Text, Font);g.DrawString(Text, Font, new SolidBrush(ForeColor), new PointF((this.Width - size.Width) / 2, (this.Height - size.Height) / 2));}}}/// <summary>/// Enum ArrowDirection/// </summary>public enum ArrowDirection{/// <summary>/// The left/// </summary>Left,/// <summary>/// The right/// </summary>Right,/// <summary>/// The top/// </summary>Top,/// <summary>/// The bottom/// </summary>Bottom,/// <summary>/// The left right/// </summary>Left_Right,/// <summary>/// The top bottom/// </summary>Top_Bottom}
}
最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧
(六十三)c#Winform自定义控件-箭头(工业)相关推荐
- (五十六)c#Winform自定义控件-瓶子(工业)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (一)c#Winform自定义控件-基类控件-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...
- stm32 ucosii消息队列 串口_正点原子STM32F407探索者开发板资料连载第六十三章 UCOSII 实验...
1)实验平台:alientek 阿波罗 STM32F767 开发板 2)摘自<STM32F7 开发指南(HAL 库版)>关注官方微信号公众号,获取更多资料:正点原子 http://weix ...
- 孤荷凌寒自学python第六十三天学习mongoDB的基本操作并进行简单封装2
孤荷凌寒自学python第六十三天学习mongoDB的基本操作并进行简单封装2 (完整学习过程屏幕记录视频地址在文末) 今天是学习mongoDB数据库的第九天. 今天继续学习mongoDB的简单操作, ...
- JavaScript学习(六十三)—typeof和instanceof检测数据类型的异同
JavaScript学习(六十三)-typeof和instanceof检测数据类型的异同 一.JavaScript中的数据类型 在JavaScript中,我们把数据可以分为原始类型和引用数据类型. 原 ...
- 鸿蒙系统的平板电脑,亓纪的想法 篇三百六十三:骁龙870+鸿蒙2.0!首款鸿蒙系统平板曝光,支持第二代M-Pencil...
亓纪的想法 篇三百六十三:骁龙870+鸿蒙2.0!首款鸿蒙系统平板曝光,支持第二代M-Pencil 2021-06-02 00:20:04 1点赞 4收藏 0评论 创作立场声明:新品资讯和爆料 6月1 ...
- 信息系统项目管理师必背核心考点(六十三)项目组合管理的主要过程DIPP分析
科科过为您带来软考信息系统项目管理师核心重点考点(六十三)项目组合管理的主要过程&DIPP分析,内含思维导图+真题 [信息系统项目管理师核心考点]项目组合管理的主要过程 1.[评估]项目组合管 ...
- 陈艾盐:《春燕》百集访谈节目第六十三集
<春燕>访谈节目共120集,每月分10集播出,记录了上百位企业家对"慈善"的各种不同见解,通过讲述社会真善美的故事,让更多的人了解慈善.发扬慈善精神,构建更加美好,和谐 ...
- 正点原子linux串口驱动下载,「正点原子Linux连载」第六十三章Linux RS232/485/GPS驱动实验...
1)实验平台:正点原子Linux开发板 2)摘自<正点原子I.MX6U嵌入式Linux驱动开发指南>关注官方微信号公众号,获取更多资料:正点原子 第六十三章Linux RS232/485/ ...
最新文章
- 大盘点 | 2020年「13篇」人脸算法最佳综述
- Remoting和Webservice有什么区别
- linux常用命令笔记(持续更新)
- php中的static
- 设置tomcat过期缓存
- 高并发处理【电商抢购】(转)
- dss中文含义_DSS(中文译名:决策支持系统),这是什么系统?有多少个种类?...
- linux强制删除文件夹
- FFMPEG学习【ffmpeg工具】
- 关于驱动程序与BSP的区别
- STP Security之BPDU Filter、BPDU Guard、Root Guard
- 咪咕盒子链接服务器失败_咪咕打卡正式开启,你的疑问都可以在这里找答案!...
- 基于卷积神经网络进行图像分类
- PCIE4.0 抖动介绍
- Excel批量删除空行的几种方法
- folder.htt
- ESP8266开发之旅 阿里云物联网平台篇⑤ LED智能灯控制系统(使用HTTPS认证再连接)
- 牛B的人到处都是(打击的一沓糊涂)
- 数据分析学习笔记2020/7/20——matplotlib绘制条形图
- 看图工具、测试移动端
热门文章
- Mac10.14安装Jdk11.04版本
- 【ESP8266开发备忘】
- 在家也能健身(01):肱二头肌
- 微软研究让员工真正快乐的原因,结果让人震惊
- 蚂蚁区块链第16课 JS SDK数据模型(账户|合约|交易|收据|日志|区块)
- linux 标准输出 复制,使用LINUX dup2 复制文件描述符到标准输出STDOUT_FILENO
- 解决项目中出现问题 Cannot resolve com.xpand:starter-canal:0.0.1-SNAPSHOT
- linux启snmp服务报错,SNMP协议攻击
- stm32 基于TouchGFX显示视频动画的详细制作过程和讲解
- 纯css3火箭穿越太空动画js特效