C# 动态更改重写的TreeView类的功能

C# 动态更改重写的TreeView类的功能,c#,winforms,treeview,overriding,C#,Winforms,Treeview,Overriding,我正在覆盖TreeView,以便使用更好的颜色高亮显示节点。作为应用程序选项的一部分,我希望用户能够在选中和取消选中时更改TreeView的字体和字体颜色。代码如下: class MyTreeView : TreeView { // Create a Font object for the node tags and HotTracking. private Font hotFont; private Font tagFont = new Font("Helvetica"

我正在覆盖
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);
            }
        }
    }
}

如您所见,我目前正在使用类的访问器更改
Font
hotFont
,该类似乎可以工作。然而,当我试图编辑聚焦/非聚焦节点的颜色时,VS2010崩溃了!到底是什么导致了这种行为?我如何实现我想要的?

当我启用那些注释掉的属性并将变量添加到控件的作用域时,发布的代码不会重新创建错误

不过有几个注意事项:

我将使用实际颜色,而不是字符串属性:

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对象,或使用(…){}块将它们包装在
中。

focusedColor和unFocusedColor是否假定为字符串?是否注释掉了VS崩溃的属性?是的,您的注释是正确的。很抱歉延迟响应…我无法复制错误。我在tagFont声明下添加了strSelectedColor和strSelectedColor变量,使用了DrawItem中使用的默认值,取消了属性注释,并在DrawItem事件中注释掉了变量声明,一切正常。您收到的实际错误消息是什么?VS2010中的一个致命错误导致它停止工作,给出“VS2010已停止工作,需要关闭!”错误消息。这可能是因为我调用类的方式。我以不同的形式同时创建多个
TreeView
s。话虽如此,我认为这是设计时的失败。这是一个奇怪的错误,为了阻止它发生,我只是注释掉上面的代码!?谢谢你花时间回答这个问题-如果你用上面的答案回答问题,我会接受;这是没有关闭,但我不会花更多的时间在这一点上,因为它可能非常深奥,并与我的设置只相关。