(六十三)c#Winform自定义控件-箭头(工业)-HZHControls
官网
前提
入行已经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+,请自行百度了解
开始
添加一个类UCArrow,继承UserControl
添加枚举,控制方向
/// <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
}
一些属性
/// <summary>
/// The arrow color
/// </summary>
private Color arrowColor = Color.FromArgb(, , ); /// <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;
根据方向和大小设置path
private void ResetPath()
{
Point[] ps = null;
switch (direction)
{
case ArrowDirection.Left:
ps = new Point[]
{
new Point(,this.Height/),
new Point(,),
new Point(,this.Height/),
new Point(this.Width-,this.Height/),
new Point(this.Width-,this.Height-this.Height/),
new Point(,this.Height-this.Height/),
new Point(,this.Height),
new Point(,this.Height/)
};
break;
case ArrowDirection.Right:
ps = new Point[]
{
new Point(,this.Height/),
new Point(this.Width-,this.Height/),
new Point(this.Width-,),
new Point(this.Width-,this.Height/),
new Point(this.Width-,this.Height),
new Point(this.Width-,this.Height-this.Height/),
new Point(,this.Height-this.Height/),
new Point(,this.Height/)
};
break;
case ArrowDirection.Top:
ps = new Point[]
{
new Point(this.Width/,),
new Point(this.Width,),
new Point(this.Width-this.Width/,),
new Point(this.Width-this.Width/,this.Height-),
new Point(this.Width/,this.Height-),
new Point(this.Width/,),
new Point(,),
new Point(this.Width/,),
};
break;
case ArrowDirection.Bottom:
ps = new Point[]
{
new Point(this.Width-this.Width/,),
new Point(this.Width-this.Width/,this.Height-),
new Point(this.Width,this.Height-),
new Point(this.Width/,this.Height-),
new Point(,this.Height-),
new Point(this.Width/,this.Height-),
new Point(this.Width/,),
new Point(this.Width-this.Width/,),
};
break;
case ArrowDirection.Left_Right:
ps = new Point[]
{
new Point(,this.Height/),
new Point(,),
new Point(,this.Height/),
new Point(this.Width-,this.Height/),
new Point(this.Width-,),
new Point(this.Width-,this.Height/),
new Point(this.Width-,this.Height),
new Point(this.Width-,this.Height-this.Height/),
new Point(,this.Height-this.Height/),
new Point(,this.Height),
new Point(,this.Height/),
};
break;
case ArrowDirection.Top_Bottom:
ps = new Point[]
{
new Point(this.Width/,),
new Point(this.Width,),
new Point(this.Width-this.Width/,),
new Point(this.Width-this.Width/,this.Height-),
new Point(this.Width,this.Height-),
new Point(this.Width/,this.Height-),
new Point(,this.Height-),
new Point(this.Width/,this.Height-),
new Point(this.Width/,),
new Point(,),
new Point(this.Width/,),
};
break;
}
m_path = new GraphicsPath();
m_path.AddLines(ps);
m_path.CloseAllFigures();
}
重绘
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) / , (this.Height - size.Height) / ));
}
}
完整代码
// ***********************************************************************
// 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(, , ); /// <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(, );
} /// <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(,this.Height/),
new Point(,),
new Point(,this.Height/),
new Point(this.Width-,this.Height/),
new Point(this.Width-,this.Height-this.Height/),
new Point(,this.Height-this.Height/),
new Point(,this.Height),
new Point(,this.Height/)
};
break;
case ArrowDirection.Right:
ps = new Point[]
{
new Point(,this.Height/),
new Point(this.Width-,this.Height/),
new Point(this.Width-,),
new Point(this.Width-,this.Height/),
new Point(this.Width-,this.Height),
new Point(this.Width-,this.Height-this.Height/),
new Point(,this.Height-this.Height/),
new Point(,this.Height/)
};
break;
case ArrowDirection.Top:
ps = new Point[]
{
new Point(this.Width/,),
new Point(this.Width,),
new Point(this.Width-this.Width/,),
new Point(this.Width-this.Width/,this.Height-),
new Point(this.Width/,this.Height-),
new Point(this.Width/,),
new Point(,),
new Point(this.Width/,),
};
break;
case ArrowDirection.Bottom:
ps = new Point[]
{
new Point(this.Width-this.Width/,),
new Point(this.Width-this.Width/,this.Height-),
new Point(this.Width,this.Height-),
new Point(this.Width/,this.Height-),
new Point(,this.Height-),
new Point(this.Width/,this.Height-),
new Point(this.Width/,),
new Point(this.Width-this.Width/,),
};
break;
case ArrowDirection.Left_Right:
ps = new Point[]
{
new Point(,this.Height/),
new Point(,),
new Point(,this.Height/),
new Point(this.Width-,this.Height/),
new Point(this.Width-,),
new Point(this.Width-,this.Height/),
new Point(this.Width-,this.Height),
new Point(this.Width-,this.Height-this.Height/),
new Point(,this.Height-this.Height/),
new Point(,this.Height),
new Point(,this.Height/),
};
break;
case ArrowDirection.Top_Bottom:
ps = new Point[]
{
new Point(this.Width/,),
new Point(this.Width,),
new Point(this.Width-this.Width/,),
new Point(this.Width-this.Width/,this.Height-),
new Point(this.Width,this.Height-),
new Point(this.Width/,this.Height-),
new Point(,this.Height-),
new Point(this.Width/,this.Height-),
new Point(this.Width/,),
new Point(,),
new Point(this.Width/,),
};
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) / , (this.Height - size.Height) / ));
}
}
} /// <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自定义控件-箭头(工业)-HZHControls的更多相关文章
- (四十六)c#Winform自定义控件-水波进度条-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...
- (十六)c#Winform自定义控件-文本框哪里去了?-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...
- (二十六)c#Winform自定义控件-有确定取消的窗体(二)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (八十六)c#Winform自定义控件-表格优化
出处:http://www.hzhcontrols.com/原文:http://www.hzhcontrols.com/blog-149.html本文版权归www.hzhcontrols.com所有欢 ...
- (五十六)c#Winform自定义控件-瓶子(工业)-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...
- (三十六)c#Winform自定义控件-步骤控件-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...
- (六)c#Winform自定义控件-单选框
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (七十六)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 ...
随机推荐
- [FPGA] Verilog 燃气灶控制器的设计与实现
燃气灶控制器的设计与实现 一.引述 本次实验所用可编程器件型号为MAXII EPM1270T144C5(其引脚表见本人另一博文:可编程实验板EPM1270T144C5使用说明),通过可编程实验板实现一 ...
- Linux服务器部署.Net Core笔记:六、安装MySQL
接下来我们在 Centos7 系统下使用 yum 命令安装 MySQL,需要注意的是 CentOS 7 版本中 MySQL数据库已从默认的程序列表中移除,所以在安装前我们需要先去官网下载 Yum 资源 ...
- 安装Visual Studio Code并汉化
安装很简单,直接点击安装文件,然后一直点击next就可以了.这款软件是免费的,不需要破解. 下载地址 这里需要使用快捷键[Ctrl+Shift+P],在弹出的搜索框中输入[configure lang ...
- Winform中实现向窗体中拖放照片并显示以及拖放文件夹显示树形结构(附代码下载)
场景 向窗体中拖拽照片并显示效果 向窗体中拖拽文件夹并显示树形结构效果 注: 博客主页: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 ...
- Bash脚本编程之脚本基础和bash配置文件
脚本基础 参考资料:Shell Scripts (Bash Reference Manual) 不严谨地说,编程语言根据代码运行的方式,可以分为两种方式: 编译运行:需要先将人类可识别的代码文件编译成 ...
- gorm操练记录
这个数据库的定义蛮全的,先作个记录. package main import ( "time" "github.com/jinzhu/gorm" _ " ...
- React组件的属性
组件的三大属性 state props refs 写组件的要求: 1>组件必须大写 2>组件必须只有一个根元素 state是组件的重要对象 值可以是对象 组件被称之为 状态机 通过跟新组件 ...
- 一文带你深入了解JVM性能调优以及对JVM调优的全面总结
目录 JVM调优 概念 基本垃圾回收算法 垃圾回收面临的问题 分代垃圾回收详述1 分代垃圾回收详述2 典型配置举例1 典型配置举例2 新一代的垃圾回收算法 调优方法 反思 一.JVM调优的一些概念 数 ...
- js实现弹出框跟随鼠标移动
又是新的一天网上冲浪,在bing的搜索页面下看到这样一个效果: 即弹出框随着鼠标的移动而移动.思路大概为: 调用onmousemove函数,将鼠标的当前位置赋予弹出框即可 //html <div ...
- 12-19 js
js是一个脚本语言 可插入到HTML网页文件中 在浏览器中执行 1 如何插入到网页中 1. 内部穿插 script 标签 type属性在网页中使用 2. 外部引入 script src属性引用js文 ...