用处及效果
准备工作
依然是用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