之前开发一个小工具,内部实现一个状态机,并显示状态机当前状态及状态间的转移过程。我使用了Stateless开源类库及一个开源自定义箭头控件。自定义箭头控件是HZHControls其中一个控件,我单独把它从源码中独立出来。主要对代码做了以下改动:

  • 添加、删除、替换了一部分注释。
  • 更改了一些内部事件触发逻辑,时间长了忘了具体改了什么,但不会影响主体功能。
  • protected override void OnPaint(PaintEventArgs e) 中内联了外部依赖的 public static void SetGDIHigh(Graphics g) 函数,我在原作者博客的评论下有说明。

以上涉及到的相关项目的链接如下

自定义箭头控件的代码很有学习价值,可以在其基础上扩展出其它形状的控件。使用时新建一个用户控件然后复制代码即可,代码如下:

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和箭头控件实现状态机的控制及显示的更多相关文章

  1. [Android Pro] android控件ListView顶部或者底部也显示分割线

    reference to  :  http://blog.csdn.net/lovexieyuan520/article/details/50846569 在默认的Android控件ListView在 ...

  2. NTKO控件在阅读PDF时,显示DEMO的问题

    NTKO控件在阅读PDF时,显示DEMO的问题, 原因是加载了以前的DEMO版本的控件.解决办法是: 在命令行中执行命令: regsvr32 /u NtkoOleDocAll.DLL 卸载老版本的控件 ...

  3. C# WPF Image控件下对于Base64的转化显示

    原文:C# WPF Image控件下对于Base64的转化显示 算作前言 本文对图片如何转化成base64不做描述,我们可以从很多途径了解到转化办法.却很少有博客提到怎么在WPF的Image控件中显示 ...

  4. kendo UI 倒如css 和 js 后 窗口控件上的工具栏图标不显示如何解决

    examples 文档中找到window的例子打开一个 查看其中文件引入 <head>    <title>API</title>    <meta char ...

  5. delphi ,1)控件根据窗口大小,一直居中显示 2)显示最大化最小化按钮控件

    一.控件根据窗口大小,一直居中显示 1)onResize:当窗体尺寸改变时发生 例子:如何使控件随窗口的放大和缩小动态改变自己的大小,使控件“保存.返回”在窗口变大变小中随着变. 在Panel调用 p ...

  6. SilverLight:基础控件使用(4)-日期显示和选择类控件

    ylbtech-SilverLight-Basic-Control:基础控件使用(4)-日期显示和选择类控件 Calendar,DatePicker 1.A,返回顶部 Calendar控件(日期控件) ...

  7. 用spin和edit控件来用spin控制edit里面小数的增减

    1.响应SPIN的消息,就是点SPIN的上键头和下键头的消息,在这个消息里改变值是以0.1步进量增减.2.使用UpdateData(FALSE)来更新EDIT的关联的double型的变量. 创建步骤 ...

  8. 【C++自绘控件】如何用GDI+来显示图片

    在我们制作一个应用软件的时候往往需要在窗口或控件中添加背景图.而图片不仅有BMP格式的,还有JPEG.PNG.TIFF.GIF等其它的格式.那么如何用jpg格式的图片来当背景呢? 这里用到了GDI+, ...

  9. repeater控件 + marquee标签 实现文字滚动显示

    各种信息网站.BBS等网站上的公告信息模块的实现 拖出一个repeater控件绑定数据库中要显示的信息 在repeater的 <ItemTemplate> ... </ItemTem ...

  10. vc 在edit控件中动态插入数据滚动显示

    内存从网上论坛摘抄整理 思路:给控件设置多行属性,设置垂直滚动条,Auto Vscroll设置为true,放入文本后把插入点设置到末尾 pEdit->LineScroll(pEdit->G ...

随机推荐

  1. OpenSSH 服务配置与管理

    SSH 是一种以安全的方式提供远程登陆的协议,也是目前远程管理Linux系统的首选方式,SSH由IETF的网络小组所制定,SSH为建立在应用层基础上的安全协议,SSH是目前较可靠,专为远程登录会话和其 ...

  2. 宝塔 websocket连接wss配置

    https要使用wss连接,wss://xxxxx.com/wss 就转发到了 服务器内的127.0.0.1:2346服务. 具体转发可以放在 文件配置或者 伪静态里面 建议放在伪静态里面容易管理 n ...

  3. KVM环境:Active console session exists for this domain

    做测试过程中被迫换电脑,但没有关掉原电脑的连接,所以用其他电脑连接测试环境时,发现之前的kvm测试环境因没有断开,无法连接: error: operation failed: Active conso ...

  4. 超简单实用的4个PPT操作技巧

    作为我们IT岗位的兄弟姐妹们,一定少不了各种PPT的展示,很多IT大佬总是不屑于PPT的美观,认为只要演讲有干货,格式无所谓,甚至都不需要PPT. 话是这样说,但其实无非就是觉得调整美化实在是浪费时间 ...

  5. 小知识:杜绝明文密码,OGG的credentialstore特性

    之前OGG配置文件中都会明文记录密码,而在OGG12c及以上版本中,有一个特性credentialstore,可以用来杜绝明文密码,提升安全性. 这里测试添加credentialstore并配置数据库 ...

  6. Linux中单引号和双引号的区别

    区别:单引号属于强引用,它会忽略所有被引起来的字符的特殊处理,被引用起来的字符会被原封不动的使用:而双引号属于弱引用,它会对一些被引起来的字符进行特殊处理.简单来说,单引号直接输出内部字符串,不解析特 ...

  7. 迟来的HIT2024和realworld2024体验赛WP

    目录 前言碎语 2024.2.14 中午 rwctf2024 体验赛 vision 哈工大青训营2024 结营赛 计算器 小技巧 神奇玩意 gdb! 再也不用苦哈哈往回翻 跟踪fork 赛后复现rw ...

  8. JOISC 2022 记录

    Day1 T1 Jail 操作很类似华容道.由于这题是可以树,同时每一个人走的都是最短路,这也就意味着不会出现通过好多个人一起的挪动来匀出空间. 所以如果合法,必然存在一种方案是每一次直接将一个人挪到 ...

  9. Spring boot集成log4j2

    spring boot默认使用的是logback作为日志框架,那如何使用log4j2呢?下面就给大家介绍一下集成步骤: 此处我使用的是spring boot 2.1.2 1.新建一个spring bo ...

  10. nmap top N端口获取

    使用nmap 扫描时可能会扫描tcp top100 top1000 端口, 有时需要去配置文件提取,配置文件路径/usr/share/nmap/nmap-services, 具体根据实际安装情况调整: ...