选择时,TreeView的所有者绘制故障 [英] TreeView owner draw glitch when selecting

查看:132
本文介绍了选择时,TreeView的所有者绘制故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想多几个图标添加到标准System.Windows.Forms.TreeView控制的元素。

I'm trying to add a few more icons to elements of a standard System.Windows.Forms.TreeView control.

我的计划是只更改标签TreeView控件的地区,但它显示了一个奇怪的现象。如果我点击一个节点,将其选中,当鼠标按钮按下时,背景是突出显示颜色正确绘制。然而,该文本是错误的未选定的颜色,直到我松开鼠标按钮。这是因为如果 e.State 包含当鼠标按钮被按下并释放的错误状态。

My plan was to only change the label area of the treeview control, but it shows a strange behaviour. If I click a node to select it, when the mouse button is depressed the background is draw correctly with the highlight color. However, the text is the wrong unselected color until I release the mouse button. It's as if e.State contains the wrong state between when the mouse button is pressed and released.

下面是我在做什么:我用的init this.DrawMode = TreeViewDrawMode.OwnerDrawText ,然后注册我的与 this.DrawNode + = LayoutTreeView_DrawNode 事件处理程序。这里是处理程序:

Here is what I'm doing: I init with this.DrawMode = TreeViewDrawMode.OwnerDrawText and then register my event handler with this.DrawNode += LayoutTreeView_DrawNode. Here is the handler:

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

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

If I set the handler to its default case...

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



...同样的事情发生,这是奇怪的,因为Windows实际上是借鉴现在。此行为是在Windows XP与.net 3.5。

...the same thing happens, which is weird since windows is actually drawing it now. This behaviour is in Windows XP with .Net 3.5.

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

Is there any way to work around this strange behaviour?

推荐答案

修改

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



to

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

这工作在Vista x64和VS 2008与.net 3.5。让我知道它是否适合你。

This worked on Vista x64 and VS 2008 with .Net 3.5. Let me know if it works for you.

看什么默认的Windows行为时,我观察是不绘制的文本和亮点,直到选择的节点,不得不焦点。所以,我为了改变文字颜色检查聚焦状态。然而这并没有精确地模仿寡妇行为,其中,不使用新的颜色直到鼠标被释放。当它选择绘制时ownerdrawn模式与Windows中的蓝色突出地位的变化绘制这...这诚然是混乱它出现的点。

What I observed when watching the default windows behavior was that the text and highlight weren't drawn until the node was selected and had focus. So I checked for the focused condition in order to change the text color. However this doesn't precisely mimic the Widows behavior where the new colors aren't used until the mouse is released. It appears the point when it chooses to draw the blue highlight status changes when in ownerdrawn mode versus windows drawing it... Which admittedly is confusing.

编辑
然而,当你创建自己的派生的TreeView你当一切都绘制在完全控制

EDIT However, when you create your own derived treeview you have full control over when everything is drawn.

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

这篇关于选择时,TreeView的所有者绘制故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆