经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
windform 重绘Treeview +- 号图标
来源:cnblogs  作者:永恒921  时间:2018/12/20 9:45:01  对本文有异议

模仿wind系统界面,重绘Treeview + - 号图标

一,首先需要图片 ,用于替换原有的 +-号

 

二、新建Tree扩展类 TreeViewEx继承TreeView

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Windows.Forms;
  7.  
  8. /******************************************************************* 
  9. * Copyright (C)  版权所有
  10. * 文件名称:TreeViewEx
  11. * 命名空间:TestRecentMenu
  12. * 创建时间:2018/12/18 16:49:08
  13. * 作    者: wangyonglai
  14. * 描    述:
  15. * 修改记录:
  16. * 修改人:
  17. * 版 本 号:v1.0.0
  18. **********************************************************************/
  19. namespace TestRecentMenu
  20. {
  21.     public class TreeViewEx : TreeView
  22.     {
  23.         private bool ArrowKeyUp = false;
  24.         private bool ArrowKeyDown = false;
  25.         private System.Windows.Forms.ImageList arrowImageList1;
  26.  
  27.         /*1节点被选中 ,TreeView有焦点*/
  28.         private SolidBrush brush1 = new SolidBrush(Color.FromArgb(209, 232, 255));//填充颜色
  29.         private Pen pen1 = new Pen(Color.FromArgb(102, 167, 232), 1);//边框颜色
  30.  
  31.         /*2节点被选中 ,TreeView没有焦点*/
  32.         private SolidBrush brush2 = new SolidBrush(Color.FromArgb(247, 247, 247));
  33.         private Pen pen2 = new Pen(Color.FromArgb(222, 222, 222), 1);
  34.  
  35.         /*3 MouseMove的时候 画光标所在的节点的背景*/
  36.         private SolidBrush brush3 = new SolidBrush(Color.FromArgb(229, 243, 251));
  37.         private Pen pen3 = new Pen(Color.FromArgb(112, 192, 231), 1);
  38.  
  39.         public const int WM_PRINTCLIENT = 0x0318;
  40.         public const int PRF_CLIENT = 0x00000004;
  41.  
  42.  
  43.         //替换+-号图标的imagelist
  44.         public ImageList arrowImageList
  45.         {
  46.             get
  47.             {
  48.                 return arrowImageList1;
  49.             }
  50.             set
  51.             {
  52.                 arrowImageList1 = value;
  53.             }
  54.         }
  55.  
  56.         public TreeViewEx()
  57.         {
  58.             //双缓存防止屏幕抖动
  59.             //this.SetStyle(ControlStyles.UserPaint, true);
  60.             this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);
  61.             this.UpdateStyles();
  62.             this.DrawMode = TreeViewDrawMode.OwnerDrawAll;
  63.             this.FullRowSelect = true;
  64.             this.HotTracking = true;
  65.             this.HideSelection = false;
  66.             //this.ShowLines = true;
  67.             this.ItemHeight = 20;
  68.  
  69.         }
  70.  
  71.  
  72.         protected override void OnDrawNode(DrawTreeNodeEventArgs e)
  73.         {
  74.             base.OnDrawNode(e);
  75.  
  76.             #region 1     选中的节点背景=========================================
  77.             Rectangle nodeRect = new Rectangle(1, e.Bounds.Top, e.Bounds.Width - 3, e.Bounds.Height - 1);
  78.  
  79.             if (e.Node.IsSelected)
  80.             {
  81.                 if (this.Focused)
  82.                 {
  83.                     e.Graphics.FillRectangle(brush1, nodeRect);
  84.                     e.Graphics.DrawRectangle(pen1, nodeRect);
  85.                 }
  86.                 else
  87.                 {
  88.                     e.Graphics.FillRectangle(brush2, nodeRect);
  89.                     e.Graphics.DrawRectangle(pen2, nodeRect);
  90.                 }
  91.  
  92.             }
  93.             else if ((e.State & TreeNodeStates.Hot) != 0 && e.Node.Text != "")//|| currentMouseMoveNode == e.Node)
  94.             {
  95.                 e.Graphics.FillRectangle(brush3, nodeRect);
  96.                 e.Graphics.DrawRectangle(pen3, nodeRect);
  97.             }
  98.             else
  99.             {
  100.                 e.Graphics.FillRectangle(Brushes.White, e.Bounds);
  101.             }
  102.  
  103.             #endregion
  104.  
  105.             #region 2     +-号绘制=========================================
  106.             Rectangle plusRect = new Rectangle(e.Node.Bounds.Left - 32, nodeRect.Top + 6, 9, 9); // +-号的大小 是9 * 9
  107.  
  108.             if (e.Node.IsExpanded)
  109.                 e.Graphics.DrawImage(arrowImageList.Images[1], plusRect);
  110.             else if (e.Node.IsExpanded == false && e.Node.Nodes.Count > 0)
  111.                 e.Graphics.DrawImage(arrowImageList.Images[0], plusRect);
  112.  
  113.  
  114.             /*测试用 画出+-号出现的矩形*/
  115.             //if (e.Node.Nodes.Count > 0)
  116.             //    e.Graphics.DrawRectangle(new Pen(Color.Red), plusRect);
  117.             #endregion
  118.  
  119.             #region 3     画节点文本=========================================
  120.             Rectangle nodeTextRect = new Rectangle(
  121.                                                     e.Node.Bounds.Left,
  122.                                                     e.Node.Bounds.Top + 4,
  123.                                                     e.Node.Bounds.Width + 2,
  124.                                                     e.Node.Bounds.Height
  125.                                                     );
  126.             nodeTextRect.Width += 4;
  127.             nodeTextRect.Height -= 4;
  128.  
  129.             e.Graphics.DrawString(e.Node.Text,
  130.                                   e.Node.TreeView.Font,
  131.                                   new SolidBrush(Color.Black),
  132.                                   nodeTextRect);
  133.  
  134.  
  135.             //画子节点个数 (111)
  136.             if (e.Node.GetNodeCount(true) > 0)
  137.             {
  138.                 e.Graphics.DrawString(string.Format("({0})", e.Node.GetNodeCount(true)),
  139.                                         new Font("Arial", 8),
  140.                                         Brushes.Gray,
  141.                                         nodeTextRect.Right - 4,
  142.                                         nodeTextRect.Top -2);
  143.             }
  144.  
  145.             ///*测试用,画文字出现的矩形*/
  146.             //if (e.Node.Text != "")
  147.             //    e.Graphics.DrawRectangle(new Pen(Color.Blue), nodeTextRect);
  148.             #endregion
  149.  
  150.             #region 4   画IImageList 中的图标===================================================================
  151.  
  152.             int currt_X = e.Node.Bounds.X;
  153.             if (this.ImageList != null && this.ImageList.Images.Count > 0)
  154.             {
  155.                 //图标大小16*16
  156.                 Rectangle imagebox = new Rectangle(
  157.                     e.Node.Bounds.- 3 - 16,
  158.                     e.Node.Bounds.+ 2,
  159.                     16,//IMAGELIST IMAGE WIDTH
  160.                     16);//HEIGHT
  161.  
  162.  
  163.                 int index = e.Node.ImageIndex;
  164.                 string imagekey = e.Node.ImageKey;
  165.                 if (imagekey != "" && this.ImageList.Images.ContainsKey(imagekey))
  166.                     e.Graphics.DrawImage(this.ImageList.Images[imagekey], imagebox);
  167.                 else
  168.                 {
  169.                     if (e.Node.ImageIndex < 0)
  170.                         index = 0;
  171.                     else if (index > this.ImageList.Images.Count - 1)
  172.                         index = 0;
  173.                     e.Graphics.DrawImage(this.ImageList.Images[index], imagebox);
  174.                 }
  175.                 currt_X -= 19;
  176.  
  177.                 /*测试 画IMAGELIST的矩形*/
  178.                 //if (e.Node.ImageIndex > 0)
  179.                 //    e.Graphics.DrawRectangle(new Pen(Color.Black, 1), imagebox);
  180.             }
  181.             #endregion
  182.         }
  183.  
  184.  
  185.         protected override void OnBeforeSelect(TreeViewCancelEventArgs e)
  186.         {
  187.             base.OnBeforeSelect(e);
  188.             if (e.Node != null)
  189.             {
  190.                 //禁止选中空白项
  191.                 if (e.Node.Text == "")
  192.                 {
  193.                     //响应上下键
  194.                     if (ArrowKeyUp)
  195.                     {
  196.                         if (e.Node.PrevNode != null && e.Node.PrevNode.Text != "")
  197.                             this.SelectedNode = e.Node.PrevNode;
  198.                     }
  199.  
  200.                     if (ArrowKeyDown)
  201.                     {
  202.                         if (e.Node.NextNode != null && e.Node.NextNode.Text != "")
  203.                             this.SelectedNode = e.Node.NextNode;
  204.                     }
  205.  
  206.                     e.Cancel = true;
  207.                 }
  208.             }
  209.         }
  210.  
  211.  
  212.       
  213.  
  214.         /// <summary>
  215.         /// 防止在选择设,treeNode闪屏
  216.         /// </summary>
  217.         protected override CreateParams CreateParams
  218.         {
  219.             get
  220.             {
  221.                 CreateParams cp = base.CreateParams;
  222.                 if (!DesignMode)
  223.                 {
  224.                     cp.ExStyle |= 0x02000000;// Turn on WS_EX_COMPOSITED  
  225.                 }
  226.                 return cp;
  227.  
  228.             }
  229.         }
  230.     }
  231. }

  

 生成后拖动控件到界面中,实际效果如下

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号