Winform绘制TreeView树形结构

本文介绍了如何创建一个自定义的WindowsFormsTreeView组件ZHTestTreeView,包括设置样式、节点背景绘制、双缓冲优化以及事件处理,同时展示了如何使用该组件生成树型菜单并捕获节点点击事件。
摘要由CSDN通过智能技术生成


创建组件:ZHTestTreeView

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ZHERP.UI.ZHControl
{
    public partial class ZHTestTreeView : TreeView
    {

        private bool ArrowKeyUp = false;
        private bool ArrowKeyDown = false;
        private Font foreFont = new Font("微软雅黑", 9F, FontStyle.Regular);
        /*1节点被选中 ,TreeView有焦点*/
        private SolidBrush brush1 = new SolidBrush(Color.FromArgb(229, 243, 251));//填充颜色
        private Pen pen1 = new Pen(Color.FromArgb(102, 167, 232), 1);//边框颜色

        /*2节点被选中 ,TreeView没有焦点*/
        private SolidBrush brush2 = new SolidBrush(Color.FromArgb(229, 243, 251));
        private Pen pen2 = new Pen(Color.FromArgb(222, 222, 222), 1);

        /*3 MouseMove的时候 画光标所在的节点的背景*/
        private SolidBrush brush3 = new SolidBrush(Color.FromArgb(229, 243, 251));
        private Pen pen3 = new Pen(Color.FromArgb(112, 192, 231), 1);

        //public const int WM_PRINTCLIENT = 0x0318;
        //public const int PRF_CLIENT = 0x00000004;
        
        public ZHTestTreeView()
        {
            //双缓存防止屏幕抖动
            //this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);
            this.UpdateStyles();
            this.DrawMode = TreeViewDrawMode.OwnerDrawAll;
            this.FullRowSelect = true;
            this.HotTracking = true;
            this.HideSelection = false;
            this.ShowLines = false;
  
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值