2012-01-31 97 views
2

我重寫了TreeView,以便我可以使用更好的顏色突出顯示節點。作爲應用程序選項的一部分,我想讓用戶在選擇和取消選擇時更改TreeView的字體和字體顏色。代碼如下:動態更改覆蓋的TreeView類的特徵

class MyTreeView : TreeView 
{ 
    // Create a Font object for the node tags and HotTracking. 
    private Font hotFont; 
    private Font tagFont = new Font("Helvetica", Convert.ToSingle(8.0), FontStyle.Bold); 

    #region Accessors. 
    public Font hotTrackFont 
    { 
     get { return this.hotFont; } 
     set { this.hotFont = value; } 
    } 

    //public string unFocusedColor 
    //{ 
    // get { return this.strDeselectedColor; } 
    // set { this.strDeselectedColor = value; } 
    //} 

    //public string focusedColor 
    //{ 
    // get { return this.strSelectedColor; } 
    // set { this.strSelectedColor = value; } 
    //} 
    #endregion 

    public MyTreeView() 
    { 
     this.HotTracking = true; 
     this.DrawMode = TreeViewDrawMode.OwnerDrawText; 
     hotFont = new Font(this.Font.FontFamily, this.Font.Size, FontStyle.Underline); 
    } 

    // Override the drawMode of TreeView. 
    protected override void OnDrawNode(DrawTreeNodeEventArgs e) 
    { 
     TreeNodeStates treeState = e.State; 
     Font treeFont = e.Node.NodeFont ?? e.Node.TreeView.Font; 

     // Colors. 
     Color foreColor = e.Node.ForeColor; 

     // Like with the hotFont I want to be able to change these dynamically... 
     string strDeselectedColor = @"#6B6E77", strSelectedColor = @"#94C7FC"; 
     Color selectedColor = System.Drawing.ColorTranslator.FromHtml(strSelectedColor); 
     Color deselectedColor = System.Drawing.ColorTranslator.FromHtml(strDeselectedColor); 

     // New brush. 
     SolidBrush selectedTreeBrush = new SolidBrush(selectedColor); 
     SolidBrush deselectedTreeBrush = new SolidBrush(deselectedColor); 

     // Set default font color. 
     if (foreColor == Color.Empty) 
      foreColor = e.Node.TreeView.ForeColor; 

     // Draw bounding box and fill. 
     if (e.Node == e.Node.TreeView.SelectedNode) 
     { 
      // Use appropriate brush depending on if the tree has focus. 
      if (this.Focused) 
      { 
       foreColor = SystemColors.HighlightText; 
       e.Graphics.FillRectangle(selectedTreeBrush, e.Bounds); 
       ControlPaint.DrawFocusRectangle(e.Graphics, e.Bounds, foreColor, SystemColors.Highlight); 
       TextRenderer.DrawText(e.Graphics, e.Node.Text, treeFont, e.Bounds, 
              foreColor, TextFormatFlags.GlyphOverhangPadding); 
      } 
      else 
      { 
       foreColor = SystemColors.HighlightText; 
       e.Graphics.FillRectangle(deselectedTreeBrush, e.Bounds); 
       ControlPaint.DrawFocusRectangle(e.Graphics, e.Bounds, foreColor, SystemColors.Highlight); 
       TextRenderer.DrawText(e.Graphics, e.Node.Text, treeFont, e.Bounds, 
              foreColor, TextFormatFlags.GlyphOverhangPadding); 
      } 
     } 
     else 
     { 
      if ((e.State & TreeNodeStates.Hot) == TreeNodeStates.Hot) 
      { 
       e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds); 
       TextRenderer.DrawText(e.Graphics, e.Node.Text, hotFont, e.Bounds, 
              System.Drawing.Color.Black, TextFormatFlags.GlyphOverhangPadding); 
      } 
      else 
      { 
       e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds); 
       TextRenderer.DrawText(e.Graphics, e.Node.Text, treeFont, e.Bounds, 
              foreColor, TextFormatFlags.GlyphOverhangPadding); 
      } 
     } 
    } 
} 

正如你可以看到我使用的是訪問到似乎工作類目前改變FonthotFont。但是,當我嘗試編輯聚焦/未聚焦節點的顏色時,VS2010崩潰!究竟是什麼導致了這種行爲,我該如何實現我想要的?

+0

focusedColor和unFocusedColor假設是一個字符串?是VS註釋掉的屬性VS崩潰? – LarsTech 2012-01-31 17:31:34

+0

是的,你說得對。對不起,延遲響應... – MoonKnight 2012-02-20 10:11:54

+0

我無法複製錯誤。我在tagFont聲明下添加了strDeselectedColor和strSelectedColor變量,並在DrawItem中使用了默認值,取消了註釋屬性並在DrawItem事件中註釋了變量聲明,並且一切正常。什麼是你收到的實際錯誤信息? – LarsTech 2012-02-20 14:03:15

回答

1

當我啓用那些註釋掉的屬性並將變量添加到控件的作用域時,發佈的代碼不會重新創建錯誤。

有兩點要注意,雖然:

而是字符串屬性的,我會用實際的顏色:

private Color _UnfocusedColor = ColorTranslator.FromHtml(@"#94C7FC"); 
private Color _FocusedColor = ColorTranslator.FromHtml(@"#6B6E77"); 

public Color UnfocusedColor 
{ 
    get { return _UnfocusedColor; } 
    set { _UnfocusedColor = value; } 
} 

public Color FocusedColor 
{ 
    get { return _FocusedColor; } 
    set { _FocusedColor = value; } 
} 

此外,確保處置繪圖對象,如SolidBrush對象,或者將它們包裝在Using(...){}區塊中。