官网

http://www.hzhcontrols.com

前提

入行已经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的更多相关文章

  1. (四十六)c#Winform自定义控件-水波进度条-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  2. (十六)c#Winform自定义控件-文本框哪里去了?-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  3. (二十六)c#Winform自定义控件-有确定取消的窗体(二)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  4. (八十六)c#Winform自定义控件-表格优化

    出处:http://www.hzhcontrols.com/原文:http://www.hzhcontrols.com/blog-149.html本文版权归www.hzhcontrols.com所有欢 ...

  5. (五十六)c#Winform自定义控件-瓶子(工业)-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  6. (三十六)c#Winform自定义控件-步骤控件-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  7. (六)c#Winform自定义控件-单选框

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  8. (七十六)c#Winform自定义控件-表单验证组件

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  9. (八十一)c#Winform自定义控件-时间轴-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

随机推荐

  1. Caffe源码-InsertSplits()函数

    InsertSplits()函数 在Net初始化的过程中,存在一个特殊的修改网络结构的操作,那就是当某层的输出blob对应多个其他层的输入blob时,会在输出blob所在层的后面插入一个新的Split ...

  2. tyvj 1387 迷你火车头

    dp百题进度条[1/100] 一列火车有一个火车头拖着一长串的车厢,每个车厢有若干个乘客. 一旦火车头出了故障,所有的车厢就只能停在铁轨上了,因此铁路局给每列火车配备了三个迷你火车头,每个迷你火车头可 ...

  3. .Net配置Ajax跨域访问

    1.在web.config文件中的 system.webServer 节点下 增加如下配置 <httpProtocol> <customHeaders> <add nam ...

  4. Maven——向Maven本地仓库中手动添加依赖包(ps:ojdbc.jar)

    maven中央仓库中并非包含所有现有的依赖包和插件,部分依赖包和插件需要手动地进行添加(如ojdbc.jar) 一.添加JDK系统环境变量(maven是基于Java的,可参考:https://www. ...

  5. python基础之元组讲解

    概念讲解: 1.Python 的元组与列表十分相似,但是元组的元素只可读不可修改: 2.元组使用小括号,列表使用方括号: 3.元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可. (1)创建一 ...

  6. SpringBoot2.0整合WebSocket,实现后端数据实时推送!

    之前公司的某个系统为了实现推送技术,所用的技术都是Ajax轮询,这种方式浏览器需要不断的向服务器发出请求,显然这样会浪费很多的带宽等资源,所以研究了下WebSocket,本文将详细介绍下. 一.什么是 ...

  7. Hadoop生态体系组件

    目录: 一.本地数据集上传到数据仓库Hive 二.Hive的基本操作 三.Hive.Mysql.HBase数据互导 正文: 一.本地数据集上传到数据仓库Hive 1.实验数据集的下载 2.数据集的预处 ...

  8. echarts 部分美化配置项使用记录

    一.图表背景色配置项,如背景颜色渐变 https://www.echartsjs.com/zh/option.html#backgroundColor 二.图表中图形的颜色,如柱状图行采用渐变颜色显示 ...

  9. java 初学 :求 s=a+aa+aaa+aaaa+aa...a 的值,其中 a 是一个数字。几个 数相加由键盘控制。

    import java.util.Scanner; public static void main(String[] args) {       Scanner input=new Scanner(S ...

  10. 【问题解决】vim 打开文档后提醒 E325: ATTENTION 怎么办?

    这是经典的 vi/vim 的报错情形. 在 Linux 下,使用 vim 或是 vi 查看文件时,可能每次都会出现下面贴出的 E325 错误提醒,然后按 E 进行 Edit anyway 才能继续读写 ...