c#ktv项目思维导图_(六十五)c#Winform自定义控件-思维导图/组织架构图(工业)-HZHControls...

本文介绍了如何使用C# Winform和GDI+创建自定义的思维导图/组织架构图控件。文章详细讲解了数据实体类的设计,包括属性、方法,以及控件的重绘过程,包括节点、子节点的绘制和连接线的绘制。此外,还提到了事件处理,如节点点击和展开折叠操作。
摘要由CSDN通过智能技术生成

用处及效果

准备工作

依然是用GDI+画的,不懂的可以先百度一下

开始

添加一个实体类,用以记录数据源节点信息

public class MindMappingItemEntity

{

///

/// Gets or sets the identifier.

///

/// The identifier.

public string ID { get; set; }

private string _text;

///

/// Gets or sets the text.

///

/// The text.

public string Text

{

get { return _text; }

set

{

_text = value;

ResetSize();

}

}

///

/// Gets or sets the data source.

///

/// The data source.

public object DataSource { get; set; }

///

/// The childrens

///

private MindMappingItemEntity[] _Childrens;

///

/// Gets or sets the childrens.

///

/// The childrens.

public MindMappingItemEntity[] Childrens

{

get { return _Childrens; }

set

{

_Childrens = value;

if (value != null && value.Length > 0)

{

value.ToList().ForEach(p => { if (p != null) { p.ParentItem = this; } });

}

}

}

///

/// The back color

///

private Color backColor = Color.Transparent;

///

/// Gets or sets the color of the back.

///

/// The color of the back.

public Color BackColor

{

get { return backColor; }

set { backColor = value; }

}

private Font font = new Font("微软雅黑", 10);

public Font Font

{

get { return font; }

set

{

font = value;

ResetSize();

}

}

///

/// The fore color

///

private Color foreColor = Color.Black;

///

/// Gets or sets the color of the fore.

///

/// The color of the fore.

public Color ForeColor

{

get { return foreColor; }

set { foreColor = value; }

}

private bool _IsExpansion = false;

///

/// Gets or sets a value indicating whether the instance is expanded.

///

/// true if this instance is expansion; otherwise, false.

public bool IsExpansion

{

get

{

return _IsExpansion;

}

set

{

if (value == _IsExpansion)

return;

_IsExpansion = value;

if (!value)

{

_Childrens.ToList().ForEach(p => { if (p != null) { p.IsExpansion = false; } });

}

}

}

///

/// Gets the parent item.

///

/// The parent item.

public MindMappingItemEntity ParentItem { get; private set; }

///

/// Gets all childrens maximum show count.

///

/// All childrens maximum show count.

public int AllChildrensMaxShowHeight { get { return GetAllChildrensMaxShowHeight(); } }

///

/// Gets the maximum level.

///

/// The maximum level.

public int AllChildrensMaxShowWidth { get { return GetAllChildrensMaxShowWidth(); } }

///

/// Gets all childrens maximum show count.

///

/// System.Int32.

private int GetAllChildrensMaxShowHeight()

{

if (!_IsExpansion || _Childrens == null || _Childrens.Length <= 0)

return ItemHeight + 10;

else

{

return _Childrens.Sum(p => p == null ? 0 : p.AllChildrensMaxShowHeight);

}

}

///

/// Gets the maximum level.

///

/// System.Int32.

private int GetAllChildrensMaxShowWidth()

{

if (!_IsExpansion || _Childrens == null || _Childrens.Length <= 0)

return ItemWidth + 50;

else

{

return 1 + _Childrens.Max(p => p == null ? 0 : p.AllChildrensMaxShowWidth);

}

}

///

/// Gets or sets the working rectangle.

///

/// The working rectangle.

internal RectangleF WorkingRectangle { get; set; }

///

/// Gets or sets the draw rectangle.

///

/// The draw rectangle.

internal RectangleF DrawRectangle { get; set; }

///

/// Gets or sets the expansion rectangle.

///

/// The expansion rectangle.

internal RectangleF ExpansionRectangle { get; set; }

///

/// Gets the height of the item.

///

/// The height of the item.

private int ItemHeight { private get; private set; }

///

/// Gets the width of the item.

///

/// The width of the item.

private int ItemWidth { private get; private set; }

///

/// Resets the size.

///

private void ResetSize()

{

string _t = _text;

if (string.IsNullOrEmpty(_t))

{

_t = "aaaa";

}

Bitmap bit = new Bitmap(1, 1);

var g = Graphics.FromImage(bit);

var size = g.MeasureString(_t, font);

g.Dispose();

bit.Dispose();

ItemHeight = (int)size.Height;

ItemWidth = (int)size.Width;

}

}

主要属性说明:

Text:显示文字

Childrens:子节点信息

BackColor:节点颜色

IsExpansion:是否展开子节点

ParentItem:父级节点

AllChildrensMaxShowHeight:该节点包含所有子节点需要显示的高度,通过私有函数GetAllChildrensMaxShowHeight()返回结果

AllChildrensMaxShowWidth:该节点包含所有子节点需要显示的宽度,通过私有函数GetAllChildrensMaxShowWidth()返回结果

WorkingRectangle:该节点以及所有子节点的工作区域

DrawRectangle:该节点的绘制区域

ExpansionRectangle:展开折叠按钮的绘制区域

ItemHeight:该节点的高度

ItemWidth:该节点的宽度

主要函数说明:

GetAllChildrensMaxShowHeight:获取当前节点及所有子节点需要的最大高度

GetAllChildrensMaxShowWidth:获取当前节点及所有子节点需要的最大宽度

ResetSize:当文本和字体改变时重新计算宽高

添加一个类UCMindMapping,继承UserCo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值