TreeView所有者在选择时绘制毛刺

时间:2010-03-09 21:38:51

标签: c# .net winforms treeview ownerdrawn

我正在尝试向标准System.Windows.Forms.TreeView控件的元素添加更多图标。

我的计划是仅更改树视图控件的标签区域,但它显示了一种奇怪的行为。如果单击某个节点将其选中,则在按下鼠标按钮时,将使用突出显示颜色正确绘制背景。但是,在我释放鼠标按钮之前,文本是错误的未选择颜色。就像e.State在按下和释放鼠标按钮之间包含错误状态一样。

以下是我正在做的事情:我使用this.DrawMode = TreeViewDrawMode.OwnerDrawText初始化,然后使用this.DrawNode += LayoutTreeView_DrawNode注册我的事件处理程序。这是处理程序:

void LayoutTreeView_DrawNode(object sender, DrawTreeNodeEventArgs e)
{

    Color color = (e.State & TreeNodeStates.Selected) != 0 ?
        SystemColors.HighlightText : SystemColors.WindowText;

    TextFormatFlags flags = TextFormatFlags.Left | TextFormatFlags.SingleLine |
       TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis;

    TextRenderer.DrawText(e.Graphics, e.Node.Text, Font, e.Bounds, color, flags);
}

如果我将处理程序设置为默认情况......

void LayoutTreeView_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
    e.DefaultDraw = true;
}

......同样的事情发生了,这很奇怪,因为Windows现在正在绘制它。此行为在Windows XP中使用.Net 3.5。

有没有办法解决这种奇怪的行为?

1 个答案:

答案 0 :(得分:3)

更改

Color color = (e.State & TreeNodeStates.Selected) != 0 ?
    SystemColors.HighlightText : SystemColors.WindowText;

Color color = (e.State & TreeNodeStates.Focused) != 0 ?
    SystemColors.HighlightText : SystemColors.WindowText;

这适用于Vista x64和VS 2008以及.Net 3.5。让我知道它是否适合你。

我在观看默认窗口行为时观察到的是,在选择节点并具有焦点之前,不会绘制文本和突出显示。所以我检查了聚焦条件,以便更改文本颜色。然而,这并不能精确地模仿Widows的行为,即在释放鼠标之前不使用新颜色。当它在所有者绘制模式中选择绘制蓝色突出显示状态时,与在窗口中绘制它相比,这似乎是一个点......这无疑是令人困惑的。

EDIT 但是,当您创建自己的派生树视图时,您可以完全控制何时绘制所有内容。

public class MyTreeView : TreeView
{
    bool isLeftMouseDown = false;
    bool isRightMouseDown = false;
    public MyTreeView()
    {
        DrawMode = TreeViewDrawMode.OwnerDrawText;
    }

    protected override void OnMouseDown(MouseEventArgs e)
    {
        TrackMouseButtons(e);
        base.OnMouseDown(e);
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
        TrackMouseButtons(e);
        base.OnMouseUp(e);
    }
    protected override void OnMouseMove(MouseEventArgs e)
    {
        TrackMouseButtons(e);
        base.OnMouseMove(e);
    }

    private void TrackMouseButtons(MouseEventArgs e)
    {
        isLeftMouseDown = e.Button == MouseButtons.Left;
        isRightMouseDown = e.Button == MouseButtons.Right;
    }

    protected override void OnDrawNode(DrawTreeNodeEventArgs e)
    {
        // don't call the base or it will goof up your display!
        // capture the selected/focused states
        bool isFocused = (e.State & TreeNodeStates.Focused) != 0;
        bool isSelected = (e.State & TreeNodeStates.Selected) != 0;
        // set up default colors.
        Color color = SystemColors.WindowText;
        Color backColor = BackColor;

        if (isFocused && isRightMouseDown)
        {
            // right clicking on a 
            color = SystemColors.HighlightText;
            backColor = SystemColors.Highlight;
        }
        else if (isSelected && !isRightMouseDown)
        {
            // if the node is selected and we're not right clicking on another node.
            color = SystemColors.HighlightText;
            backColor = SystemColors.Highlight;
        }

        using (Brush sb = new SolidBrush(backColor))
            e.Graphics.FillRectangle(sb,e.Bounds);

        TextFormatFlags flags = TextFormatFlags.Left | TextFormatFlags.SingleLine |
           TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis;

        TextRenderer.DrawText(e.Graphics, e.Node.Text, Font, e.Bounds, color, backColor, flags);
    }
}
相关问题