C#使用Stateless和箭头控件实现状态机的控制及显示
之前开发一个小工具,内部实现一个状态机,并显示状态机当前状态及状态间的转移过程。我使用了Stateless开源类库及一个开源自定义箭头控件。自定义箭头控件是HZHControls其中一个控件,我单独把它从源码中独立出来。主要对代码做了以下改动:
- 添加、删除、替换了一部分注释。
- 更改了一些内部事件触发逻辑,时间长了忘了具体改了什么,但不会影响主体功能。
- 在 protected override void OnPaint(PaintEventArgs e) 中内联了外部依赖的 public static void SetGDIHigh(Graphics g) 函数,我在原作者博客的评论下有说明。
以上涉及到的相关项目的链接如下
- Stateless开源类库:https://github.com/dotnet-state-machine/stateless
- HZHControls控件库:https://gitee.com/kwwwvagaa/net_winform_custom_control
- Stateless使用方法:.NET中的状态机库Stateless
- 自定义箭头控件介绍:(六十三)c#Winform自定义控件-箭头(工业)-HZHControls
自定义箭头控件的代码很有学习价值,可以在其基础上扩展出其它形状的控件。使用时新建一个用户控件然后复制代码即可,代码如下:
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 ExtControls
{
public partial class Arrow : UserControl
{
private Color arrowColor = Color.DarkGray;
/// <summary>
/// 获取设置箭头颜色
/// </summary>
[Description("箭头颜色"), Category("自定义")]
public Color ArrowColor
{
get { return arrowColor; }
set
{
arrowColor = value;
Refresh();
}
}
private Color? borderColor = null;
/// <summary>
/// 获取或设置箭头边框颜色
/// </summary>
[Description("箭头边框颜色,为空则无边框"), Category("自定义")]
public Color? BorderColor
{
get { return borderColor; }
set
{
borderColor = value;
Refresh();
}
}
/// <summary>
/// 箭头方向
/// </summary>
private ArrowDirection direction = ArrowDirection.Right;
/// <summary>
/// 获取或设置箭头方向
/// </summary>
/// <value>The direction.</value>
[Description("获取或设置箭头方向"), Category("自定义")]
public ArrowDirection Direction
{
get { return direction; }
set
{
direction = value;
ResetMyPath();
Refresh();
}
}
private string text;
/// <summary>
/// 与控件关联的文本。
/// </summary>
[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>
/// 图形空间
/// </summary>
GraphicsPath myPath;
/// <summary>
/// 初始化UCArrow类的新实例。
/// </summary>
public Arrow()
{
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_NeedResetChanged;
this.Size = new Size(100, 50);
this.ForeColorChanged += UCArrow_NeedResetChanged;
this.ForeColor = Color.Black;
this.FontChanged += UCArrow_NeedResetChanged;
}
/// <summary>
/// 需要重绘控件的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void UCArrow_NeedResetChanged(object sender, EventArgs e)
{
ResetMyPath();
}
/// <summary>
/// 重置图形
/// </summary>
private void ResetMyPath()
{
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;
}
myPath = new GraphicsPath();
myPath.AddLines(ps);
myPath.CloseAllFigures();
}
/// <summary>
/// 引发Control.Paint 事件。
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
// 设置GDI高质量模式抗锯齿
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality;
g.FillPath(new SolidBrush(arrowColor), myPath);
if (borderColor != null && borderColor != Color.Empty)
g.DrawPath(new Pen(new SolidBrush(borderColor.Value)), myPath);
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>
/// 箭头方向的描述枚举
/// </summary>
public enum ArrowDirection
{
/// <summary>
/// 向左
/// </summary>
Left,
/// <summary>
/// 向右
/// </summary>
Right,
/// <summary>
/// 向上
/// </summary>
Top,
/// <summary>
/// 向下
/// </summary>
Bottom,
/// <summary>
/// 向左向右
/// </summary>
Left_Right,
/// <summary>
/// 向上向下
/// </summary>
Top_Bottom
}
}
C#使用Stateless和箭头控件实现状态机的控制及显示的更多相关文章
- [Android Pro] android控件ListView顶部或者底部也显示分割线
reference to : http://blog.csdn.net/lovexieyuan520/article/details/50846569 在默认的Android控件ListView在 ...
- NTKO控件在阅读PDF时,显示DEMO的问题
NTKO控件在阅读PDF时,显示DEMO的问题, 原因是加载了以前的DEMO版本的控件.解决办法是: 在命令行中执行命令: regsvr32 /u NtkoOleDocAll.DLL 卸载老版本的控件 ...
- C# WPF Image控件下对于Base64的转化显示
原文:C# WPF Image控件下对于Base64的转化显示 算作前言 本文对图片如何转化成base64不做描述,我们可以从很多途径了解到转化办法.却很少有博客提到怎么在WPF的Image控件中显示 ...
- kendo UI 倒如css 和 js 后 窗口控件上的工具栏图标不显示如何解决
examples 文档中找到window的例子打开一个 查看其中文件引入 <head> <title>API</title> <meta char ...
- delphi ,1)控件根据窗口大小,一直居中显示 2)显示最大化最小化按钮控件
一.控件根据窗口大小,一直居中显示 1)onResize:当窗体尺寸改变时发生 例子:如何使控件随窗口的放大和缩小动态改变自己的大小,使控件“保存.返回”在窗口变大变小中随着变. 在Panel调用 p ...
- SilverLight:基础控件使用(4)-日期显示和选择类控件
ylbtech-SilverLight-Basic-Control:基础控件使用(4)-日期显示和选择类控件 Calendar,DatePicker 1.A,返回顶部 Calendar控件(日期控件) ...
- 用spin和edit控件来用spin控制edit里面小数的增减
1.响应SPIN的消息,就是点SPIN的上键头和下键头的消息,在这个消息里改变值是以0.1步进量增减.2.使用UpdateData(FALSE)来更新EDIT的关联的double型的变量. 创建步骤 ...
- 【C++自绘控件】如何用GDI+来显示图片
在我们制作一个应用软件的时候往往需要在窗口或控件中添加背景图.而图片不仅有BMP格式的,还有JPEG.PNG.TIFF.GIF等其它的格式.那么如何用jpg格式的图片来当背景呢? 这里用到了GDI+, ...
- repeater控件 + marquee标签 实现文字滚动显示
各种信息网站.BBS等网站上的公告信息模块的实现 拖出一个repeater控件绑定数据库中要显示的信息 在repeater的 <ItemTemplate> ... </ItemTem ...
- vc 在edit控件中动态插入数据滚动显示
内存从网上论坛摘抄整理 思路:给控件设置多行属性,设置垂直滚动条,Auto Vscroll设置为true,放入文本后把插入点设置到末尾 pEdit->LineScroll(pEdit->G ...
随机推荐
- 【预定义】C语言预定义代码(宏、条件编译等)内容介绍【最全的保姆级别教程】
浅谈C语言预定义中的预定义符号,#define,以及符号#,##的相关运用 求个赞求个赞求个赞求个赞 谢谢 先赞后看好习惯 打字不容易,这都是很用心做的,希望得到支持你 大家的点赞和支持对于我来说是一 ...
- 5个.NET开源且强大的快速开发框架(帮助你提高生产效率)
中台Admin(Admin.Core) 中台Admin(Admin.Core)是前后端分离权限管理系统,前端 UI 基于Vue3开发,后端 Api 基于.NET 8.0开发.支持多租户.接口权限.数据 ...
- 零基础入门学习JAVA课堂笔记 ——DAY07
面向对象(下) 1. Instanceof 我们可以通过Instanceof关键词可以判断当前对象是否为某某类得父类 Object instanceof Student //true 注意:只有是两个 ...
- 2.3 实验:用linxerUnpack进行通用脱壳--《恶意代码分析实战》
Lab01-03.exe 实验内容: 1.将文件上传到http://www.VirusTotal.com 进行分析并查看报告.文件匹配到了已有的反病毒软件特征吗? 2.是否有这个文件被 ...
- Linux zip常用命令
1.将当前目录下的所有文件和文件夹全部压缩成myfile.zip内联代码块文件zip -r myfile.zip ./*-r表示递归压缩子目录下所有文件. 2.unzip把myfile.zip文件解压 ...
- 通过解析库探究函数式抽象代价 ( ini 解析示例补充)
上一篇 用 HexColor 作为示例,可能过于简单 这里再补充一个 ini 解析的示例 由于实在写不动用其他库解析 ini 了, 春节都要过完了,累了,写不动了, 所以随意找了一份解析ini的库, ...
- 吉特日化MES & WMS 与周边系统集成架构
作者:情缘 出处:http://www.cnblogs.com/qingyuan/ 关于作者:从事仓库,生产软件方面的开发,在项目管理以及企业经营方面寻求发展之路 版权声明:本文版权归作者和博客园 ...
- Swoole从入门到入土(3)——TCP服务器[基本配置项]
在这一节的开篇,让我们先解决上一节的"配置"话题.对于server对象,有很多配置项决定了服务端的行为,可以用set的函数进行配置的设置. 1.函数set:用于设置运行时的各项参数 ...
- centos 安装nacos 并以后台服务形式启动
一.下载解压nacos tar -xvf nacos-server-1.2.0.tar.gz 二.持久化配置(mysql) 修改nacos/conf/application.properties文件, ...
- Qt5.15.0 升级至 Qt5.15.9 遇到的一些错误
按照之前我写的文章教程,可以很简单的编译出静态库(仅供学习交流) 编译 windows 上的 qt 静态库 编译出静态库后,替换旧版本的库,见我另一篇文章教程 VS2019 配置 QT 库 之所以没有 ...