none
TreeView1失去焦点后,我想原选中的节点仍高亮显示,求教该如何做,谢谢!!! RRS feed

  • 问题

  • 我查看了网络,得到方法:

      
    TreeView1.HideSelection = False
    TreeView1.DrawMode = TreeViewDrawMode.OwnerDrawText
    
     Private Sub TreeView1_DrawNode(sender As Object, e As DrawTreeNodeEventArgs) Handles TreeView1.DrawNode
            e.Graphics.FillRectangle(Brushes.White, e.Node.Bounds)
            If e.State = TreeNodeStates.Selected Then
                e.Graphics.FillRectangle(Brushes.Silver, New Rectangle(TreeView1.Left, e.Node.Bounds.Top, TreeView1.Width, e.Node.Bounds.Height)) '//背景色为蓝色
                e.Graphics.DrawString(e.Node.Text, TreeView1.Font, Brushes.White, Rectangle.Inflate(e.Bounds, 2, 0)) ' //字体为白色Rectangle.Inflate(e.Bounds, 2, 2)
            Else
                e.DrawDefault = True
            End If
        End Sub
    

    得到的文字位置不一样(错位),还请帮助,谢谢!!!

    2015年3月12日 8:41

答案

  • 你好,

    我改了一下代码,现在应该好了。你可以试试看。

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            TreeView1.HideSelection = False
            TreeView1.DrawMode = TreeViewDrawMode.OwnerDrawText
        End Sub
        Private Sub TreeView1_DrawNode(sender As Object, e As DrawTreeNodeEventArgs) Handles TreeView1.DrawNode
            e.Graphics.FillRectangle(Brushes.White, e.Node.Bounds)
            If e.State = TreeNodeStates.Selected Then
                e.Graphics.FillRectangle(Brushes.Silver, New Rectangle(e.Node.Bounds.Left, e.Node.Bounds.Top, TreeView1.Width, e.Node.Bounds.Height)) '//背景色为蓝色
                e.Graphics.DrawString(e.Node.Text, TreeView1.Font, Brushes.White, Rectangle.Inflate(e.Bounds, 0, 0)) ' //字体为白色Rectangle.Inflate(e.Bounds, 2, 2)
            Else
                e.DrawDefault = True
            End If
        End Sub


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 YQY888 2015年3月15日 3:08
    2015年3月13日 7:41
    版主

全部回复

  • 你好,

    我改了一下代码,现在应该好了。你可以试试看。

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            TreeView1.HideSelection = False
            TreeView1.DrawMode = TreeViewDrawMode.OwnerDrawText
        End Sub
        Private Sub TreeView1_DrawNode(sender As Object, e As DrawTreeNodeEventArgs) Handles TreeView1.DrawNode
            e.Graphics.FillRectangle(Brushes.White, e.Node.Bounds)
            If e.State = TreeNodeStates.Selected Then
                e.Graphics.FillRectangle(Brushes.Silver, New Rectangle(e.Node.Bounds.Left, e.Node.Bounds.Top, TreeView1.Width, e.Node.Bounds.Height)) '//背景色为蓝色
                e.Graphics.DrawString(e.Node.Text, TreeView1.Font, Brushes.White, Rectangle.Inflate(e.Bounds, 0, 0)) ' //字体为白色Rectangle.Inflate(e.Bounds, 2, 2)
            Else
                e.DrawDefault = True
            End If
        End Sub


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 YQY888 2015年3月15日 3:08
    2015年3月13日 7:41
    版主
  • 谢谢!!!还是不行,e.Graphics.DrawString里的文字位置就是比原文字位置要偏左一点,想求教是否能得到原文字的坐标值,谢谢!!!
    2015年3月15日 1:37
  • 谢谢,已解决。
    2015年3月15日 3:08