2012-01-31 5 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을 변경하고 볼 수 있듯이. 그러나 초점을 맞춘/unfocused 노드의 색상을 편집하려고하면 VS2010이 충돌합니다! 이 행동을 정확히 일으키는 것은 무엇이며 어떻게하면 좋을까요?

+0

focusedColor 및 unFocusedColor가 문자열로 간주됩니까? VS가 충돌하는 곳은 주석 처리 된 속성입니까? – LarsTech

+0

네, 맞습니다. 지연된 응답을 드려 죄송합니다 ... – MoonKnight

+0

오류를 복제 할 수 없습니다. tagItem에서 strDeselectedColor와 strSelectedColor 변수를 DrawItem에서 사용했던 기본값으로 추가하고 속성의 주석을 제거하고 DrawItem 이벤트의 변수 선언을 주석 처리했습니다. 실제 오류 메시지는 무엇입니까? – LarsTech

답변

1

주석 처리 된 속성을 활성화하고 변수를 컨트롤의 범위에 추가했을 때 게시 된 코드가 오류를 다시 작성하지 않습니다.

하지만 노트의 몇 :

대신 문자열 속성, 나는 실제 색상 사용합니다 : 예 SolidBrush 개체로, 그리기 개체를 삭제해야합니다, 또한

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; } 
} 

을, 또는 Using(...){} 블록으로 마무리하십시오.

관련 문제