Skip to main content
Melhorar formatação
Fonte Link
Rafael Tavares
  • 5,2mil
  • 11
  • 31
  • 52

Pessoal Já acheiAchei a solução {na própria https://docs.microsoft.com/pt-br/dotnet/api/system.windows.forms.treeview.drawnode?view=net-5.0documentação } na própria documentação ensina porem n está tão visível .... oSegue o código completo =>

inserir a descrição da imagem aquiabaixo.

Font tagFont = new Font(FontFamily.GenericSansSerif,8, FontStyle.Bold);

treeView1.DrawMode = TreeViewDrawMode.OwnerDrawText;

treeView1.DrawNode += new DrawTreeNodeEventHandler(Ty_DrawNode);

treeView1.MouseDown += new MouseEventHandler(myTreeView_MouseDown);


treeView1.DrawMode = TreeViewDrawMode.OwnerDrawText;

treeView1.DrawNode += new DrawTreeNodeEventHandler(Ty_DrawNode);

treeView1.MouseDown += new MouseEventHandler(myTreeView_MouseDown);


    private void Ty_DrawNode(Object ob,DrawTreeNodeEventArgs e)
    {
        if ((e.State & TreeNodeStates.Selected)!= 0)
        {
           e.Graphics.FillRectangle(Brushes.Red, NodeBounds(e.Node));
            Font nodefont = e.Node.NodeFont;
            if (nodefont == null) nodefont = ((TreeView)ob).Font;

            e.Graphics.DrawString(e.Node.Text, nodefont, Brushes.White,
                Rectangle.Inflate(e.Bounds, 2, 0));
        }
        else
        {
            e.DrawDefault = true;
        }
    } 

    private RectangleF NodeBounds(TreeNode node)
    {
        Rectangle bounds = node.Bounds;
        if (node.Tag != null)
        {
            Graphics g = treeView1.CreateGraphics();
            int tagwhite = (int)g.MeasureString(node.Tag.ToString(),
                tagFont).Width;
            bounds.Offset(tagwhite, 0);
            bounds = Rectangle.Inflate(bounds, tagwhite, 0);
            g.Dispose();
        }
        return bounds;
    }

    private void myTreeView_MouseDown(object sender, MouseEventArgs e)
    {
        TreeNode clickedNode = treeView1.GetNodeAt(e.X, e.Y);
        if (NodeBounds(clickedNode).Contains(e.X, e.Y))
        {
            treeView1.SelectedNode = clickedNode;
        }
    }

// OBS: e.Graphics.FillRectangle(Brushes.Red, NodeBounds(e.Node)); e.Graphics.FillRectangle(Brushes.Red, NodeBounds(e.Node)); é só alterar a parte do Brushes.Brushes (Ee escolher a cor)...

(Desculpa se estiver meio bagunçado a formatação ainda estou aprendendo a mexer aqui :P)inserir a descrição da imagem aqui

Pessoal Já achei a solução { https://docs.microsoft.com/pt-br/dotnet/api/system.windows.forms.treeview.drawnode?view=net-5.0 } na própria documentação ensina porem n está tão visível .... o código completo =>

inserir a descrição da imagem aqui

Font tagFont = new Font(FontFamily.GenericSansSerif,8, FontStyle.Bold);

treeView1.DrawMode = TreeViewDrawMode.OwnerDrawText;

treeView1.DrawNode += new DrawTreeNodeEventHandler(Ty_DrawNode);

treeView1.MouseDown += new MouseEventHandler(myTreeView_MouseDown);

    private void Ty_DrawNode(Object ob,DrawTreeNodeEventArgs e)
    {
        if ((e.State & TreeNodeStates.Selected)!= 0)
        {
           e.Graphics.FillRectangle(Brushes.Red, NodeBounds(e.Node));
            Font nodefont = e.Node.NodeFont;
            if (nodefont == null) nodefont = ((TreeView)ob).Font;

            e.Graphics.DrawString(e.Node.Text, nodefont, Brushes.White,
                Rectangle.Inflate(e.Bounds, 2, 0));
        }
        else
        {
            e.DrawDefault = true;
        }
    }
    private RectangleF NodeBounds(TreeNode node)
    {
        Rectangle bounds = node.Bounds;
        if (node.Tag != null)
        {
            Graphics g = treeView1.CreateGraphics();
            int tagwhite = (int)g.MeasureString(node.Tag.ToString(),
                tagFont).Width;
            bounds.Offset(tagwhite, 0);
            bounds = Rectangle.Inflate(bounds, tagwhite, 0);
            g.Dispose();
        }
        return bounds;
    }private void myTreeView_MouseDown(object sender, MouseEventArgs e)
    {
        TreeNode clickedNode = treeView1.GetNodeAt(e.X, e.Y);
        if (NodeBounds(clickedNode).Contains(e.X, e.Y))
        {
            treeView1.SelectedNode = clickedNode;
        }
    }

// OBS: e.Graphics.FillRectangle(Brushes.Red, NodeBounds(e.Node)); é só alterar a parte do Brushes.(E escolher a cor)...

(Desculpa se estiver meio bagunçado a formatação ainda estou aprendendo a mexer aqui :P)

Achei a solução na própria documentação. Segue o código completo abaixo.

Font tagFont = new Font(FontFamily.GenericSansSerif,8, FontStyle.Bold);

treeView1.DrawMode = TreeViewDrawMode.OwnerDrawText;

treeView1.DrawNode += new DrawTreeNodeEventHandler(Ty_DrawNode);

treeView1.MouseDown += new MouseEventHandler(myTreeView_MouseDown);


    private void Ty_DrawNode(Object ob,DrawTreeNodeEventArgs e)
    {
        if ((e.State & TreeNodeStates.Selected)!= 0)
        {
           e.Graphics.FillRectangle(Brushes.Red, NodeBounds(e.Node));
            Font nodefont = e.Node.NodeFont;
            if (nodefont == null) nodefont = ((TreeView)ob).Font;

            e.Graphics.DrawString(e.Node.Text, nodefont, Brushes.White,
                Rectangle.Inflate(e.Bounds, 2, 0));
        }
        else
        {
            e.DrawDefault = true;
        }
    } 

    private RectangleF NodeBounds(TreeNode node)
    {
        Rectangle bounds = node.Bounds;
        if (node.Tag != null)
        {
            Graphics g = treeView1.CreateGraphics();
            int tagwhite = (int)g.MeasureString(node.Tag.ToString(),
                tagFont).Width;
            bounds.Offset(tagwhite, 0);
            bounds = Rectangle.Inflate(bounds, tagwhite, 0);
            g.Dispose();
        }
        return bounds;
    }

    private void myTreeView_MouseDown(object sender, MouseEventArgs e)
    {
        TreeNode clickedNode = treeView1.GetNodeAt(e.X, e.Y);
        if (NodeBounds(clickedNode).Contains(e.X, e.Y))
        {
            treeView1.SelectedNode = clickedNode;
        }
    }

OBS: e.Graphics.FillRectangle(Brushes.Red, NodeBounds(e.Node)); é só alterar a parte do Brushes (e escolher a cor)

inserir a descrição da imagem aqui

adicionou 378 caracteres ao conteúdo
Fonte Link

inserir a descrição da imagem aqui Font tagFont = new Font(FontFamily.GenericSansSerif,8, FontStyle.Bold);

Font tagFont = new Font(FontFamily.GenericSansSerif,8, FontStyle.Bold);

treeView1.DrawNode += new DrawTreeNodeEventHandler(Ty_DrawNode);

treeView1.MouseDown += new MouseEventHandler(myTreeView_MouseDown);

    }

    private void Ty_DrawNode(Object ob,DrawTreeNodeEventArgs e)
    {
        if ((e.State & TreeNodeStates.Selected)!= 0)
        {
           e.Graphics.FillRectangle(Brushes.Red, NodeBounds(e.Node));
            Font nodefont = e.Node.NodeFont;
            if (nodefont == null) nodefont = ((TreeView)ob).Font;

            e.Graphics.DrawString(e.Node.Text, nodefont, Brushes.White,
                Rectangle.Inflate(e.Bounds, 2, 0));
        }
        else
        {
            e.DrawDefault = true;
        }
    }
    private RectangleF NodeBounds(TreeNode node)
    {
        Rectangle bounds = node.Bounds;
        if (node.Tag != null)
        {
            Graphics g = treeView1.CreateGraphics();
            int tagwhite = (int)g.MeasureString(node.Tag.ToString(),
                tagFont).Width;
            bounds.Offset(tagwhite, 0);
            bounds = Rectangle.Inflate(bounds, tagwhite, 0);
            g.Dispose();
        }
        return bounds;
    }private void myTreeView_MouseDown(object sender, MouseEventArgs e)
    {
        TreeNode clickedNode = treeView1.GetNodeAt(e.X, e.Y);
        if (NodeBounds(clickedNode).Contains(e.X, e.Y))
        {
            treeView1.SelectedNode = clickedNode;
        }
    }

// OBS: e.Graphics.FillRectangle(Brushes.Red, NodeBounds(e.Node)); é só alterar a parte do Brushes.(E escolher a cor)...

(Desculpa se estiver meio bagunçado a formatação ainda estou aprendendo a mexer aqui :P)

inserir a descrição da imagem aqui Font tagFont = new Font(FontFamily.GenericSansSerif,8, FontStyle.Bold);

treeView1.DrawNode += new DrawTreeNodeEventHandler(Ty_DrawNode);

    }

    private void Ty_DrawNode(Object ob,DrawTreeNodeEventArgs e)
    {
        if ((e.State & TreeNodeStates.Selected)!= 0)
        {
           e.Graphics.FillRectangle(Brushes.Red, NodeBounds(e.Node));
            Font nodefont = e.Node.NodeFont;
            if (nodefont == null) nodefont = ((TreeView)ob).Font;

            e.Graphics.DrawString(e.Node.Text, nodefont, Brushes.White,
                Rectangle.Inflate(e.Bounds, 2, 0));
        }
        else
        {
            e.DrawDefault = true;
        }
    }
    private RectangleF NodeBounds(TreeNode node)
    {
        Rectangle bounds = node.Bounds;
        if (node.Tag != null)
        {
            Graphics g = treeView1.CreateGraphics();
            int tagwhite = (int)g.MeasureString(node.Tag.ToString(),
                tagFont).Width;
            bounds.Offset(tagwhite, 0);
            bounds = Rectangle.Inflate(bounds, tagwhite, 0);
            g.Dispose();
        }
        return bounds;
    }

// OBS: e.Graphics.FillRectangle(Brushes.Red, NodeBounds(e.Node)); é só alterar a parte do Brushes.(E escolher a cor)...

inserir a descrição da imagem aqui

Font tagFont = new Font(FontFamily.GenericSansSerif,8, FontStyle.Bold);

treeView1.DrawNode += new DrawTreeNodeEventHandler(Ty_DrawNode);

treeView1.MouseDown += new MouseEventHandler(myTreeView_MouseDown);

    private void Ty_DrawNode(Object ob,DrawTreeNodeEventArgs e)
    {
        if ((e.State & TreeNodeStates.Selected)!= 0)
        {
           e.Graphics.FillRectangle(Brushes.Red, NodeBounds(e.Node));
            Font nodefont = e.Node.NodeFont;
            if (nodefont == null) nodefont = ((TreeView)ob).Font;

            e.Graphics.DrawString(e.Node.Text, nodefont, Brushes.White,
                Rectangle.Inflate(e.Bounds, 2, 0));
        }
        else
        {
            e.DrawDefault = true;
        }
    }
    private RectangleF NodeBounds(TreeNode node)
    {
        Rectangle bounds = node.Bounds;
        if (node.Tag != null)
        {
            Graphics g = treeView1.CreateGraphics();
            int tagwhite = (int)g.MeasureString(node.Tag.ToString(),
                tagFont).Width;
            bounds.Offset(tagwhite, 0);
            bounds = Rectangle.Inflate(bounds, tagwhite, 0);
            g.Dispose();
        }
        return bounds;
    }private void myTreeView_MouseDown(object sender, MouseEventArgs e)
    {
        TreeNode clickedNode = treeView1.GetNodeAt(e.X, e.Y);
        if (NodeBounds(clickedNode).Contains(e.X, e.Y))
        {
            treeView1.SelectedNode = clickedNode;
        }
    }

// OBS: e.Graphics.FillRectangle(Brushes.Red, NodeBounds(e.Node)); é só alterar a parte do Brushes.(E escolher a cor)...

(Desculpa se estiver meio bagunçado a formatação ainda estou aprendendo a mexer aqui :P)

Fonte Link

Pessoal Já achei a solução { https://docs.microsoft.com/pt-br/dotnet/api/system.windows.forms.treeview.drawnode?view=net-5.0 } na própria documentação ensina porem n está tão visível .... o código completo =>

inserir a descrição da imagem aqui Font tagFont = new Font(FontFamily.GenericSansSerif,8, FontStyle.Bold);

treeView1.DrawMode = TreeViewDrawMode.OwnerDrawText;

treeView1.DrawNode += new DrawTreeNodeEventHandler(Ty_DrawNode);

    }

    private void Ty_DrawNode(Object ob,DrawTreeNodeEventArgs e)
    {
        if ((e.State & TreeNodeStates.Selected)!= 0)
        {
           e.Graphics.FillRectangle(Brushes.Red, NodeBounds(e.Node));
            Font nodefont = e.Node.NodeFont;
            if (nodefont == null) nodefont = ((TreeView)ob).Font;

            e.Graphics.DrawString(e.Node.Text, nodefont, Brushes.White,
                Rectangle.Inflate(e.Bounds, 2, 0));
        }
        else
        {
            e.DrawDefault = true;
        }
    }
    private RectangleF NodeBounds(TreeNode node)
    {
        Rectangle bounds = node.Bounds;
        if (node.Tag != null)
        {
            Graphics g = treeView1.CreateGraphics();
            int tagwhite = (int)g.MeasureString(node.Tag.ToString(),
                tagFont).Width;
            bounds.Offset(tagwhite, 0);
            bounds = Rectangle.Inflate(bounds, tagwhite, 0);
            g.Dispose();
        }
        return bounds;
    }

// OBS: e.Graphics.FillRectangle(Brushes.Red, NodeBounds(e.Node)); é só alterar a parte do Brushes.(E escolher a cor)...