MindFusion.Diagramming流程图控件如何创建自定义节点

作者:控件中国网   出处:控件中国网   2015-11-12 16:06:55   阅读:11

MindFusion.Diagramming是一款用于.NET下的流程图控件,可以帮助开发人员快速创建工作流程处理、关系类图、图形网络图、实体关系图、IVR系统、工业自动化处理、家谱宗族树型图、逻辑流程图、组织图、XML文档等多种流程图,这篇文章咱们主要介绍怎么使用MindFusion.Diagramming控件对流程图中的节点进行自定义绘制,开发人员可以根据自己不同的需求对节点进行相关的自定义,下面是实现的详细步奏:
<a href=http://www.componentcn.com/kongjianchanpin/tubiao/liuchengtukongjian/2014-11-17/546.html target=_blank class=infotextkey>MindFusion.Diagramming</a>.small_0.png
1.自定义节点类
public class IconNode : DiagramNode
{
}
2.节点类定义一个初始化静态函数
static IconNode()
{
    defaultIcon = new Bitmap(48, 48);
 
    Graphics graphics = Graphics.FromImage(defaultIcon);
    Font font = new Font("Arial", 48, FontStyle.Bold, GraphicsUnit.Pixel);
 
    graphics.FillRectangle(Brushes.Transparent, 0, 0, 48, 48);
    graphics.DrawString("?", font, Brushes.Black, 0, 0);
 
    font.Dispose();
    graphics.Dispose();
}
3.定义一个公共的构造函数
public IconNode(Diagram diagram) : base(diagram)
{
    icon = defaultIcon;
    label = "Label";
    format = new StringFormat();
    format.Alignment = StringAlignment.Center;
    format.LineAlignment = StringAlignment.Center;
    Bounds = new RectangleF(Bounds.Location, CalculateSize());
}
4.重载DrawLocal方法
public override void DrawLocal(IGraphics graphics, RenderOptions options)
{
    Rectangle iconSizePixels = new Rectangle(
        0, 0, icon.Width, icon.Height);
    RectangleF imageSize = MindFusion.Utilities.DeviceToDoc(
        graphics, iconSizePixels);
 
    // Draw the icon at the top-middle
    graphics.DrawImage(icon,
        Bounds.X + Bounds.Width / 2 - imageSize.Width / 2, Bounds.Y);
 
    // Draw the label at the bottom
    RectangleF labelBounds = RectangleF.FromLTRB(
        Bounds.X, Bounds.Y + imageSize.Height,
        Bounds.Right, Bounds.Bottom);
 
    graphics.DrawString(label,
        Font, Brushes.Black, labelBounds, format);
}
5.重载DrawShadowLocal方法
public override void DrawShadowLocal(IGraphics graphics, RenderOptions options)
{
}
6.定义自定义属性
public Image Icon
{
    get { return icon; }
    set
    {
        icon = value;
        Bounds = new RectangleF(Bounds.Location, CalculateSize());
    }
}
 
public string Label
{
    get { return label; }
    set
    {
        label = value;
        Bounds = new RectangleF(Bounds.Location, CalculateSize());
    }
}
7.设置可序列化节点实例
// Enable serialization of IconNode instances
Diagram.RegisterItemClass(typeof(IconNode), "IconNode", 1);
8.执行序列化
protected override void SaveTo(
    System.IO.BinaryWriter writer, PersistContext ctx)
{
    base.SaveTo(writer, ctx);
 
    // Save the label using the standard .NET BinaryWriter
    writer.Write(label);
 
    // Save the image using the MindFusion.Diagramming built-in image saving code,
    // which stores the contents of shared images only once.
    ctx.SaveImage(icon);
}
 
protected override void LoadFrom(
    System.IO.BinaryReader reader, PersistContext ctx)
{
    base.LoadFrom(reader, ctx);
 
    label = reader.ReadString();
    icon = ctx.LoadImage();
}
9.设置可交互式地绘制节点对象
// Let users draw IconNode objects
diagramView.CustomNodeType = typeof(IconNode);
diagramView.Behavior = Behavior.Custom;
10.添加自定义节点绘制到click事件里
private void diagram_Clicked(object sender, DiagramEventArgs e)
{
    IconNode iconNode = new IconNode(diagram);
    diagram.Nodes.Add(iconNode);
    iconNode.Move(e.MousePosition.X, e.MousePosition.Y);
}
Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat