因項目需求,需要畫出樹狀圖,在網上搜了一下,發現這方面的資料是少之又少。沒辦法,只能自己摸索著做。下面給出一個簡單的三層實例實現,權當拋磚引玉,以下是最終效果圖:
首先需要說明的是樹狀圖是在pictureBox控件下畫的,代碼寫在其Paint事件下。
第一步,定義一下各個結點的內容以及結點數量等初始信息。這裡就簡單地直接賦值了,大多數情況下結點的內容應該從數據庫中讀取,這時只要對該部分的代碼作相關修改即可。
[csharp]
string parentTree = "中國";
ArrayList midTree = new ArrayList();
midTree.Add("江蘇省");
midTree.Add("山東省");
int midTreeCount = midTree.Count;
ArrayList subTree = new ArrayList();
subTree.Add("南京市");
subTree.Add("揚州市");
subTree.Add("蘇州市");
subTree.Add("青島市");
subTree.Add("日照市");
int subTreeCount = subTree.Count;
ArrayList eachSubTreeCount = new ArrayList();
eachSubTreeCount.Add(3);
eachSubTreeCount.Add(2);
定義一些畫圖需要的初始變量
[csharp]
int midCountFlag = 0; //畫中間結點時用到的偏移量
int subCountFlag = 0; //畫頂層結點時用到的偏移量
int x = 0; //結點矩形圖左上角X坐標
int y = 0; //結點矩形圖左上角Y坐標
int picX = pictureBox1.Width; //繪圖區域水平長度
int picY = pictureBox1.Height; //繪圖區域豎直長度
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
Rectangle rect; //結點矩形圖
Point loc; //結點矩形圖左上角坐標
Point startP; //連接線起始坐標
Point endP; //連接線終止坐標
Point tempP = new Point(); //坐標緩存量
SizeF sizeF; //結點內容尺寸大小
Size s;
Font font = new Font("宋體", 18); //結點內容的字體
Pen redPen = new Pen(Color.Red, 2); //連線需要的畫筆
Graphics g = e.Graphics;
g.Clear(Color.White); //每次重繪先把繪圖區域清空
OK,這時我們就可以開始畫了,先把根部畫出來。
[csharp]
#region 畫樹根
sizeF = g.MeasureString(parentTree, font);
sizeF.Width += 10;
s = sizeF.ToSize();
x = Convert.ToInt32((picX - sizeF.Width) / 2);
y = 30;
startP = new Point(picX / 2, y + s.Height);
loc = new Point(x, y);
rect = new Rectangle(loc, s);
g.DrawRectangle(Pens.Black, rect);
g.DrawString(parentTree, font, Brushes.Black, rect, sf);
#endregion
再把樹根的子樹畫出來。
[csharp]
#region 畫子樹
foreach (object o in midTree)
{
int picXMid = picX / midTreeCount;
string strMidTree = o.ToString();
sizeF = g.MeasureString(strMidTree, font);
sizeF.Width += 10;
s = sizeF.ToSize();
x = Convert.ToInt32((picXMid - sizeF.Width) / 2 + picXMid * midCountFlag);
y = 230;
endP = new Point(picXMid / 2 + picXMid * midCountFlag, y);
loc = new Point(x, y);
rect = new Rectangle(loc, s);
g.DrawRectangle(Pens.Black, rect);
g.DrawString(strMidTree, font, Brushes.Black, rect, sf);
g.DrawLine(redPen, startP, endP);
midCountFlag++;
if (midCountFlag == 1)
tempP = new Point(endP.X, endP.Y + s.Height);
}
#endregion
畫出子樹的樹枝。
[csharp]
#region 畫子樹的樹枝
startP = tempP;
for (int i = 0; i != midTree.Count; ++i)
{
int picXMid = picX / midTreeCount;
startP.X += picXMid * i;
if (i >= 1)
subCountFlag += (int)eachSubTreeCount[i - 1];
for (int j = 0; j != (int)eachSubTreeCount[i]; ++j)
{
int picXSub = picX / (midTreeCount * (int)eachSubTreeCount[i]);
string strSubTree = subTree[j + subCountFlag].ToString();
sizeF = g.MeasureString(strSubTree, font);
sizeF.Width += 10;
s = sizeF.ToSize();
x = Convert.ToInt32(((picXSub - sizeF.Width) / 2 + picXSub * j) + picXMid * i);
y = 430;
endP = new Point(picXSub / 2 + picXSub * j + picXMid * i, y);
loc = new Point(x, y);
rect = new Rectangle(loc, s);
g.DrawRectangle(Pens.Black, rect);
g.DrawString(strSubTree, font, Brushes.Black, rect, sf);
g.DrawLine(redPen, startP, endP);
}
}
#endregion