代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
组织结构图
作者:
Dezai.CN
/ 发布于
2012/12/12
/
432
1. 从下往上计算位置2. 模仿Word中组织结构图的特点
调用代码: //实现二维数组读取 Tree<string> tree = new Tree<string>(null, "根"); string[,] str = { { "a", "b", "c", "d", "e", "f", "g" }, { "1", "2", "3", "4", "5", "6", "7" }, { "10", "20", "30", "40", "50", "60", "70" } }; for (int i = 0; i <= str.GetUpperBound(0); i++) { tree.Add(i.ToString()); for (int j = 0; j <= str.GetUpperBound(1); j++) { tree.Childs[i].Add(str[i, j]); //string a = str[i, j]; } } //tree.Add("北京公司").Add("a").Parent.Add("b").Add("c").Add("D"); //tree.Add("董事秘书室特殊机构"); //tree.Add("上海公司"); //tree.Childs[0].Add("总经理办公室"); //tree.Childs[0].Add("财务部"); //tree.Childs[0].Add("销售部"); //tree.Childs[1].Add("秘书长"); //tree.Childs[1].Add("秘书总理"); //tree.Childs[2].Add("上海销售部").Parent.Add("aaaaa"); //tree.Childs[2].Add("上海分公司销售部").Add("无无针无针针"); Response.ContentType = "images/jpg"; Bitmap bmp = tree.DrawAsImage(); bmp.Save(Response.OutputStream, ImageFormat.Jpeg); //Response.ClearContent(); //this.Response.Output.WriteLine(bmp); Response.End(); 实现代码: using System; using System.Collections.Generic; using System.Text; using System.Drawing; namespace Test { /// <summary> /// 用来输出组织结构图的类 /// </summary> /// <typeparam name="T"></typeparam> public class Tree<T> { Tree<T> _Parent = null; T _Content; List<Tree<T>> _Childs = new List<Tree<T>>(); SizeF _Size; Rectangle _Rec; public Tree(Tree<T> parent, T content) { _Parent = parent; _Content = content; } public Tree<T> Add(T content) { Tree<T> tree = new Tree<T>(this, content); _Childs.Add(tree); return tree; } public Tree<T> Parent { get { return _Parent; } } public T Content { get { return _Content; } } public List<Tree<T>> Childs { get { return _Childs; } } public SizeF Size { get { return _Size; } set { _Size = value; } } public Rectangle Rec { get { return _Rec; } set { _Rec = value; } } void MeatureAllSize(Graphics g, Font font, int addWidth) { _Size = g.MeasureString(_Content.ToString(), font); _Size.Width += addWidth; foreach (Tree<T> tree in Childs) tree.MeatureAllSize(g, font, addWidth); } List<List<Tree<T>>> GetTreeLayers() { List<List<Tree<T>>> layers = new List<List<Tree<T>>>(); GetTreeLayers(layers, new List<Tree<T>>(new Tree<T>[] { this }), 0); return layers; } void GetTreeLayers(List<List<Tree<T>>> layers, List<Tree<T>> childs, int level) { if (childs.Count == 0) return; if (layers.Count <= level) layers.Add(new List<Tree<T>>()); for (int i = 0; i < childs.Count; i++) { layers[level].Add(childs[i]); GetTreeLayers(layers, childs[i].Childs, level + 1); } } /// <summary> /// 设置显示区域(从最后一层最左开始) /// </summary> /// <param name="level"></param> /// <param name="height"></param> /// <param name="interval"></param> /// <param name="left"></param> void SetRectangle(int level, int height, int hInterval, int vInterval, int left) { int index = 0; if (Parent != null) index = Parent.Childs.IndexOf(this); if (Childs.Count == 0) { // 没有儿子,就向前靠 if (left > 0) left += hInterval; } else { // 有儿子,就在儿子中间 int centerX = (Childs[0].Rec.Left + Childs[Childs.Count - 1].Rec.Right) / 2; left = centerX - (int)_Size.Width / 2; // 并且不能和前面的重复,如果重复,联同子孙和子孙的右边节点右移 if (Parent != null && index > 0) { int ex = (Parent.Childs[index - 1].Rec.Right + hInterval) - left; if (index > 0 && ex > 0) { for (int i = index; i < Parent.Childs.Count; i++) Parent.Childs[i].RightChilds(ex); left += ex; } } } _Rec = new Rectangle(left, (height + vInterval) * level, (int)_Size.Width, height); } /// <summary> /// 所有子孙向右平移 /// </summary> /// <param name="ex"></param> void RightChilds(int ex) { Rectangle rec; for (int i = 0; i < _Childs.Count; i++) { rec = _Childs[i].Rec; rec.Offset(ex, 0); _Childs[i].Rec = rec; _Childs[i].RightChilds(ex); } } void Offset(int x, int y) { _Rec.Offset(x, y); for (int i = 0; i < _Childs.Count; i++) _Childs[i].Offset(x, y); } public Bitmap DrawAsImage() { return DrawAsImage(Pens.Black, new Font("宋体", 10.5f), 26, 20, 5, 20, 26); } public Bitmap DrawAsImage(Pen pen, Font font, int h, int horPadding, int horInterval, int verInterval, int borderWidth) { Bitmap bmp = new Bitmap(1, 1); Graphics g = Graphics.FromImage(bmp); // 把树扁平化 List<List<Tree<T>>> layers = GetTreeLayers(); // 算出每个单元的大小 MeatureAllSize(g, font, horPadding); g.Dispose(); bmp.Dispose(); // 从最后一层开始排列 int left = 0; for (int i = layers.Count - 1; i >= 0; i--) { for (int j = 0; j < layers[i].Count; j++) { layers[i][j].SetRectangle(i, h, horInterval, verInterval, left); left = layers[i][j].Rec.Right; } } Offset(borderWidth, borderWidth); // 获取画布需要的大小 int maxHeight = (h + verInterval) * layers.Count - verInterval + borderWidth * 2; int maxWidth = 0; for (int i = layers.Count - 1; i >= 0; i--) { for (int j = 0; j < layers[i].Count; j++) { if (layers[i][j].Rec.Right > maxWidth) maxWidth = layers[i][j].Rec.Right; } } maxWidth += borderWidth; // 边宽 // 画 bmp = new Bitmap(maxWidth, maxHeight); g = Graphics.FromImage(bmp); g.Clear(Color.White); StringFormat format = (StringFormat)StringFormat.GenericDefault.Clone(); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; Rectangle rec, recParent; for (int i = 0; i < layers.Count; i++) { for (int j = 0; j < layers[i].Count; j++) { // 画字 rec = (Rectangle)layers[i][j].Rec; g.DrawRectangle(pen, rec); g.DrawString(layers[i][j].Content.ToString(), font, new SolidBrush(pen.Color), rec, format); // 画到父亲的线 if (layers[i][j].Parent != null) { recParent = layers[i][j].Parent.Rec; g.DrawLine(pen, rec.Left + rec.Width / 2, rec.Top, rec.Left + rec.Width / 2, rec.Top - verInterval / 2); g.DrawLine(pen, recParent.Left + recParent.Width / 2, recParent.Bottom, recParent.Left + recParent.Width / 2, rec.Top - verInterval / 2); g.DrawLine(pen, rec.Left + rec.Width / 2, rec.Top - verInterval / 2, recParent.Left + recParent.Width / 2, rec.Top - verInterval / 2); } } } g.Flush(); g.Dispose(); return bmp; } } }
试试其它关键字
组织结构图
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3