官网

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. Linux服务器部署.Net Core笔记:五、安装Nginx

    我们搜索一下yum库关于nginx的rpm包:yum list | grep nginx 找到rpm安装包,我们就可以使用yum直接安装了:yum install nginx 修改nginx配置文件: ...

  2. WPF特点

    前言:为什么要学习WPF呢?因为随着现阶段硬件技术的升级以及客户对体验的要求越来越高,传统的GDI和USERS(或者是GDI+.USERS)已经不能满足这个需求,因此,WPF技术应运而生. WPF的特 ...

  3. elementui移动dialog

    1.在创建Vue对象时添加全局属性 Vue.directive('dialogDrag', { bind(el, binding, vnode, oldVnode) { const dialogHea ...

  4. JavaScript图形实例:四瓣花型图案

    设有坐标计算公式如下: X=L*(1+SIN(4α))*COS(α) Y=L*(1+SIN(4α))*SIN(α) 用循环依次取α值为0~2π,计算出X和Y,在canvas画布中对坐标位置(X,Y)描 ...

  5. 《Netty Zookeeper Redis 高并发实战》 图书简介

    <Netty Zookeeper Redis 高并发实战> 图书简介 本书为 高并发社群 -- 疯狂创客圈 倾力编著, 高度剖析底层原理,深度解读面试难题 疯狂创客圈 Java 高并发[ ...

  6. python通过http下载文件的方法

    1.通过requests.get方法 r = requests.get("http://200.20.3.20:8080/job/Compile/job/aaa/496/artifact/b ...

  7. 高阶函数 - Higher Order Function

    一个函数如果有 参数是函数 或 返回值是函数,就称为高阶函数. 这篇文章介绍高阶函数的一个子集:输入 fn,输出 fn'. 按 fn 与 fn' 功能是否一致,即相同输入是否始终对应相同输出,把这类高 ...

  8. 精通awk系列(10):awk筛选行和处理字段的示例

    回到: Linux系列文章 Shell系列文章 Awk系列文章 awk数据筛选示例 筛选行 # 1.根据行号筛选 awk 'NR==2' a.txt # 筛选出第二行 awk 'NR>=2' a ...

  9. windows 本地链接 VMware虚拟机 redis服务

    使用本地Windows链接 VMware虚拟机 redis服务 我用的虚拟机系统是:windows Server 2012 先把Redis服务器拷贝到服务器并解压,目录如下 这里仅仅作为演示,所以就不 ...

  10. mysql order by limit 的一个坑

    分页查询的时候遇到的坑: 发现的问题: 对单个无索引的字段进行排序后limit .发现当被排序字段有相同值时并且在limit范围内,取的值并不是正常排序后的值, 也就是说,当排在第N行的数据可取key ...