Roman Ratskey
Roman Ratskey

Reputation: 5269

Disable TreeView Node Focus Cue

I wonder how to disable node focus rectangle on WinForms TreeView, i have tried a to handle the AfterSelect but the selection as well as the FocusCues still appear for a second and then disappears

Code

public class CustomTreeView : TreeView
{
    protected override void OnDrawNode(DrawTreeNodeEventArgs e)
    {
        e.DrawDefault = true;
        base.OnDrawNode(e);
    }

    protected override void OnAfterSelect(TreeViewEventArgs e)
    {
        SelectedNode = null;
        base.OnAfterSelect(e);
    }
}

Upvotes: 4

Views: 1566

Answers (1)

Dmitry
Dmitry

Reputation: 14059

I've created a sample class for you. It inherits from the TreeView class and introduces new property DrawElements. By default, both focus rect & selection are enabled. To disable certain element, adjust the DrawElements property's value. To disable both, set it's value to 0.

[Flags]
public enum TreeViewDrawElements
{
    FocusRect = 1,
    Selection = 2
}

public sealed class MyTreeView : TreeView
{
    public MyTreeView()
    {
        DrawMode = TreeViewDrawMode.OwnerDrawText;
        DrawElements = TreeViewDrawElements.FocusRect | TreeViewDrawElements.Selection;
    }

    [DefaultValue(TreeViewDrawElements.FocusRect | TreeViewDrawElements.Selection)]
    public TreeViewDrawElements DrawElements { get; set; }

    protected override void OnDrawNode(DrawTreeNodeEventArgs e)
    {
        base.OnDrawNode(e);
        if (DrawElements == (TreeViewDrawElements.FocusRect | TreeViewDrawElements.Selection))
        {
            e.DrawDefault = true;
            return;
        }
        TreeNode node = e.Node;
        Rectangle bounds = node.Bounds;
        Graphics g = e.Graphics;
        Size textSize = TextRenderer.MeasureText(node.Text, node.TreeView.Font);
        Point textLoc = new Point(bounds.X - 1, bounds.Y); // required to center the text 
        bounds = new Rectangle(textLoc, new Size(textSize.Width, bounds.Height));
        Font font = node.NodeFont ?? node.TreeView.Font;
        bool selected = (DrawElements & TreeViewDrawElements.Selection) != 0 && (e.State & TreeNodeStates.Selected) == TreeNodeStates.Selected;
        Color color = (selected && node.TreeView.Focused) ? SystemColors.HighlightText : (node.ForeColor != Color.Empty) ? node.ForeColor : node.TreeView.ForeColor;

        g.FillRectangle(selected ? SystemBrushes.Highlight : SystemBrushes.Window, bounds);
        if ((DrawElements & TreeViewDrawElements.FocusRect) != 0 && (e.State & TreeNodeStates.Selected) != 0)
            ControlPaint.DrawFocusRectangle(g, bounds, color, SystemColors.Highlight);
        TextRenderer.DrawText(g, e.Node.Text, font, bounds, color, TextFormatFlags.Default);
    }
}

Usage example:

MyTreeView tree = new MyTreeView { DrawElements = TreeViewDrawElements.Selection };
tree.Nodes.Add("aaa");
tree.Nodes.Add("bbb");
tree.Nodes.Add("ccc");

Upvotes: 1

Related Questions