2013-02-11 51 views

回答

0

DrawMode是要走的路。您必須將其設置爲OwnerDrawText,並訂閱DrawNode事件。 I .: .:

this.treeView1.DrawMode = System.Windows.Forms.TreeViewDrawMode.OwnerDrawText; 
this.treeView1.DrawNode += new System.Windows.Forms.DrawTreeNodeEventHandler(this.treeView1_DrawNode); 

這只是如何繪製方法的樣本。您需要修改它以獲得良好的圖形效果,但它可以讓您瞭解如何去做。

private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e) 
    { 
     Font nodeFont = e.Node.NodeFont; 
     if (nodeFont == null) nodeFont = ((TreeView)sender).Font; 

     string txt = e.Node.Text; 
     int idx = txt.IndexOf(' '); 
     string greenTxt; 
     string redTxt; 
     if (idx >= 0) 
     { 
      greenTxt = txt.Substring(0, idx); 
      redTxt = txt.Substring(idx); 
     } 
     else 
     { 
      greenTxt = txt; 
      redTxt = string.Empty; 
     } 
     Rectangle greenRect = new Rectangle(e.Bounds.Location, new Size((int)Math.Ceiling(e.Graphics.MeasureString(greenTxt, nodeFont).Width), e.Bounds.Height)); 
     Rectangle redRect = new Rectangle(e.Bounds.Location + new Size(greenRect.Width, 0), new Size((int)Math.Ceiling(e.Graphics.MeasureString(redTxt, nodeFont).Width), e.Bounds.Height)); 
     e.Graphics.DrawString(greenTxt, nodeFont, Brushes.Green, greenRect); 
     if (!string.IsNullOrEmpty(redTxt)) 
      e.Graphics.DrawString(redTxt, nodeFont, 
       Brushes.Red, redRect); 
    } 

你可以找到一個更復雜的例子here

+0

嗨!謝謝!!!它對我有很大的幫助 – 2013-02-11 12:15:54

+0

我的代碼有問題 – 2013-02-11 12:29:25

+0

我的代碼有問題,當我展開樹時,我在樹的頂部註冊了所有的擴展對象另一個,然後我滾動它消失 你說什麼? – 2013-02-11 12:32:33