当用户焦点离开TreeView时,TreeView选中节点仍然高亮,但是颜色符合主题。

设置TreeView.HideSelection = False;可让选中节点保持高亮。

添加重绘事件

TreeView.DrawMode = TreeViewDrawMode.OwnerDrawText;

TreeView.DrawNode += new DrawTreeNodeEventHandler(TreeView_DrawNode);

  1. // Draws a node.
  2. private void TreeView_DrawNode(object sender, DrawTreeNodeEventArgs e)
  3. {
  4. // Draw the background and node text for a selected node.
  5. if ((e.State & TreeNodeStates.Selected) != )
  6. {
  7. // Draw the background of the selected node. The NodeBounds
  8. // method makes the highlight rectangle large enough to
  9. // include the text of a node tag, if one is present.
  10. e.Graphics.FillRectangle(Brushes.Green, NodeBounds(e.Node));
  11. // Retrieve the node font. If the node font has not been set,
  12. // use the TreeView font.
  13. Font nodeFont = e.Node.NodeFont;
  14. if (nodeFont == null)
  15. nodeFont = ((TreeView)sender).Font;
  16. // Draw the node text.
  17. e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.White,
  18. Rectangle.Inflate(e.Bounds, , ));
  19. }
  20. // Use the default background and node text.
  21. else
  22. {
  23. e.DrawDefault = true;
  24. }
  25. // If a node tag is present, draw its string representation
  26. // to the right of the label text.
  27. if (e.Node.Tag != null)
  28. {
  29. e.Graphics.DrawString(e.Node.Tag.ToString(), tagFont, Brushes.Yellow, e.Bounds.Right + , e.Bounds.Top);
  30. }
  31. // If the node has focus, draw the focus rectangle large, making
  32. // it large enough to include the text of the node tag, if present.
  33. if ((e.State & TreeNodeStates.Focused) != )
  34. {
  35. using (Pen focusPen = new Pen(Color.Black))
  36. {
  37. focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
  38. Rectangle focusBounds = NodeBounds(e.Node);
  39. focusBounds.Size = new Size(focusBounds.Width - , focusBounds.Height - );
  40. e.Graphics.DrawRectangle(focusPen, focusBounds);
  41. }
  42. }
  43. }

WinForm TreeView节点重绘,失去焦点的高亮显示的更多相关文章

  1. C#TreeView节点选中后失去焦点时改变节点背景色

    C#TreeView节点选中后失去焦点时改变节点背景色 在使用TreeView控件时候,单击一个节点,当鼠标聚焦到别的地方的时候,之前点击的这个节点就看不清楚了 举例截图 单击后           ...

  2. WPF如何实现TreeView节点重命名

    我们经常看到一些软件比如酷狗音乐,在对列表右键进行重命名的时候,当前列表会泛白并且进入可编辑状态,当我们更改完成后就会并进入非编辑状态,这些具体是怎么实现的呢?下面的方法也许会提供一些思路,下面的Tr ...

  3. WPF 之 TreeView节点重命名

    下面的TreeView节点是通过数据双向绑定的方式,绑定到TextBlock控件和TextBox控件的Text属性上,并且让两者绑定相同的属性,同时使TextBox控件刚好完全覆盖TextBlock控 ...

  4. WinForm中的重绘 - 按钮等控件的背景渐变色重绘

    注:brush通过起止坐标来控制重绘范围及方向.比如从上到下渐变时,brush第二个Point参数是左下角坐标. private void PaintGradientBackground(Button ...

  5. Winform TreeView 节点拖动

    private void treeView_ItemDrag(object sender, ItemDragEventArgs e) { TreeNode dragNode = e.Item as T ...

  6. winform TreeView 节点选择

    public partial class Form1 : Form { public Form1() { InitializeComponent(); } string tag = "Tru ...

  7. Winform解决界面重绘闪烁的问题

    在窗体或用户控件中重写CreateParams protected override CreateParams CreateParams { get { CreateParams cp = base. ...

  8. WinForm中的重绘 - 文本的重绘

    两种方式 TextRenderer.DrawText 注意:默认在每次绘制的文本左右有padding,即使参数中设置了TextFormatFlags.NoPadding也是一样,因此在分段绘制文本时( ...

  9. 重绘和回流(reflow和repaint)

    由于DOM操作会导致浏览器的回流,回流需要花费大量的时间进行样式计算和节点重绘与渲染,所以应当尽量减少回流次数. 以下是几种常见的减少重绘和回流的方法: 一.不要一项一项的更改页面的样式,尽量一口气写 ...

随机推荐

  1. org.apache.hadoop.conf-Configured

    org.apache.hadoop.conf中的最后一个类,也是这个包中以后用的最频繁的一个,Configurable算是肉体,Configuration算是灵魂吧 package org.apach ...

  2. Asp.NET 之 路径浅析

    比如你的工程是Web(url是:http://localhost/web/default.aspx) Request.ApplicationPath 就是/Web 如果是站点就直接返回"/& ...

  3. Wcf 之 配置文件解析

    在WCF Service Configuration Editor的使用中,我们通过配置工具自动生成了WCF服务端的config文件.现在我们来看下这个配置文件各个标签的意义(解释在下面xml文件中的 ...

  4. Find the Clones

    Find the Clones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6365   Accepted: 2375 D ...

  5. org.apache.solr.common.util.ContentStream.java及其实现类

    org.apache.solr.common.util.ContentStream.java 主要是获取文件,URL,字节数组,字符串等的数据流.主要方法又InputStream getStream( ...

  6. 跨平台高效率Lua网络库 ( 同步形式的API ,底层是异步非阻塞)

    Joynet 项目地址:https://github.com/IronsDu/Joynet 介绍 high performance network library for lua, based on  ...

  7. uiview 的setAnimationTransition : forView 方法实现翻页效果

    [UIView beginAnimations:nil context:nil]; [UIView setAnimationTransition:UIViewAnimationTransitionCu ...

  8. PHP实现遍历、复制、删除目录

    一.遍历 opendir 具体函数我就不解释了,直接看代码理解: <?php header("Content-Type:Text/html;charset=utf8"); $ ...

  9. 关于URLEncoder的编码和URLDecode解码

    在我们开发中,很多时候会遇见各种编码乱码的问题,其实这都是编码不一致的问题,如果你懂得了关于URLEncoder的编码和URLDecode解码,那么问题就会随之消失! 下面我们看看关于编码的代码: [ ...

  10. mysql mysql_error mysqli_connect_error 乱码

    <html> <head> <meta charset="utf-8"> <title></title> </he ...