陈程的技术博客

  • 关于作者
全栈软件工程师
一个专注于技术研究创新的程序员
  1. 首页
  2. .NET
  3. 正文

DreamSkin自定义美化控件-TreeView控件

2016年8月17日 768点热度 0人点赞 0条评论

TreeViewList控件。

属性有2个:

treeViewData:树的内容,类型是自定义的实体结构如下:

public int Id { get; set; }

/// <summary>
/// 父节点,空的时候为根节点
/// </summary>
public int? FatherId { get; set; }

public string Text { get; set; }

SelectId:选中的节点。

事件有1个:

SelectIdChanged:选择Id变化触发事件

目前该控件使用了DEV控件里面的控件,这边先暂时不考虑,之后会做去DEV控件化。

源码开放如下:

private List<TreeViewEntity> treeViewData;

      private Dictionary<string, string> controlPosition;

      public List<TreeViewEntity> TreeViewData
      {
          get { return treeViewData; }
          set { treeViewData = value; }
      }

      private int selectId;

      public int SelectId
      {
          get { return selectId; }
          set { selectId = value; }
      }

      public int ExpandId = 0;

      public TreeViewList()
      {
          InitializeComponent();

          InitData();
      }

      //定义事件  
      public delegate void SelectIdChangedDelegate(object sender, EventArgs e);
      /// <summary>
      /// 选择Id变化触发事件
      /// </summary>
      public event SelectIdChangedDelegate SelectIdChanged;

      /// <summary>
      /// 初始化
      /// </summary>
      public void InitData()
      {
          if (treeViewData == null)
              return;

          controlPosition = new Dictionary<string, string>();

          Font rootFont = new Font("微软雅黑", 10.5f, FontStyle.Regular);
          Color rootColor = Color.FromArgb(119, 119, 119);

          int topPosition = 10;
          int rootInterval = 18;
          int rootLeft = 0;

          int index = 0;
          ExpandButton clickExpandBtn = new ExpandButton();
          foreach (var item in treeViewData)
          {
              if (item.FatherId == null)
              {
                  LabelControl rootLab = new LabelControl();
                  rootLab.Name = "rootLab" + index;
                  rootLab.Tag = item.Id.ToString();
                  rootLab.Font = rootFont;
                  rootLab.ForeColor = rootColor;
                  rootLab.Location = new Point(rootLeft, topPosition + rootInterval * index + rootLab.Height * index);
                  rootLab.Text = item.Text;

                  ExpandButton expandBtn = new ExpandButton();
                  expandBtn.Name = "expandBtn" + index;
                  expandBtn.Tag = item.Id.ToString();
                  expandBtn.IsExpand = false;
                  expandBtn.BorderStyle = BorderStyle.None;
                  expandBtn.Size = new Size(10, 10);
                  expandBtn.Location = new Point(panelMain.Width - 15, rootLab.Location.Y + 5);
                  expandBtn.BackColor = this.panelMain.BackColor;
                  expandBtn.expandClickEvent += expandBtn_ExpandClickEvent;
                  panelMain.Controls.Add(rootLab);
                  panelMain.Controls.Add(expandBtn);

                  controlPosition.Add(item.Id.ToString(), rootLab.Location.X + "," + rootLab.Location.Y + "|" + expandBtn.Location.X + "," + expandBtn.Location.Y);
              
              if(index==0)
              {
                  clickExpandBtn=expandBtn;
              }
              }
              index++;
          }
          clickExpandBtn.IsExpand = true;
          ExpandFirst(clickExpandBtn);
      }

      private void ExpandFirst(ExpandButton expandBtn)
      {
          Font childFont = new Font("微软雅黑", 9, FontStyle.Regular);
          Font childSelectFont = new Font("微软雅黑", 9, FontStyle.Bold);
          Color childSelectColor = Color.FromArgb(184, 39, 18);
          Color childColor = Color.FromArgb(119, 119, 119);

          //展开子节点个数
          int childTreeCount = treeViewData.Where(m => m.FatherId != null).Where(s => s.FatherId.Value == Convert.ToInt32(expandBtn.Tag)).Count();
          if (childTreeCount <= 0)
              return;

          if (ExpandId == Convert.ToInt32(expandBtn.Tag))
          {
              ExpandId = 0;
              return;
          }

          int childInterval = 5;
          int childTextHeight = 15;
          int addHeight = childTextHeight * childTreeCount + childInterval * (childTreeCount - 1);
          //下移其他节点
          foreach (Control childLab in expandBtn.Parent.Controls)
          {
              if (Convert.ToInt32(childLab.Tag.ToString()) > Convert.ToInt32(expandBtn.Tag))
              {
                  childLab.Location = new Point(childLab.Location.X, childLab.Location.Y + addHeight);
              }
          }

          int childLeft = 20;
          int topPosition = expandBtn.Location.Y + expandBtn.Height + 10;
          int index = 0;
          //展开树
          foreach (var item in treeViewData)
          {
              if (item.FatherId == Convert.ToInt32(expandBtn.Tag))
              {
                  LabelControl childLab = new LabelControl();
                  childLab.Name = "childLab" + index;
                  childLab.Tag = item.Id.ToString();
                  childLab.Location = new Point(childLeft, topPosition + childInterval * index + childLab.Height * index);
                  childLab.Text = item.Text;
                  childLab.Click += childLab_Click;
                  if (index == 0)
                  {
                      childLab.ForeColor = childSelectColor;
                      childLab.Font = childSelectFont;

                      Circle circle = new Circle();
                      circle.Name = "childLabCircle" + childLab.Tag.ToString();
                      circle.Location = new Point(childLab.Location.X - 13, childLab.Location.Y + 4);
                      panelMain.Controls.Add(circle);
                      selectId = item.Id;
                  }
                  else
                  {
                      childLab.Font = childFont;
                      childLab.ForeColor = childColor;
                  }
                  panelMain.Controls.Add(childLab);
                  index++;
              }
          }
          //展开的节点
          ExpandId = Convert.ToInt32(expandBtn.Tag);
      }

      void expandBtn_ExpandClickEvent(object sender, EventArgs e)
      {
          ExpandButton expandBtn = (ExpandButton)sender as ExpandButton;

          Font childFont = new Font("微软雅黑", 9, FontStyle.Regular);
          Font childSelectFont = new Font("微软雅黑", 9, FontStyle.Bold);
          Color childSelectColor = Color.FromArgb(184, 39, 18);
          Color childColor = Color.FromArgb(119, 119, 119);

          if (ExpandId != 0)
          {
              //有子节点的收起来
              string names = string.Empty;
              List<Control> removeControls = new List<Control>();
              foreach (Control childLab in expandBtn.Parent.Controls)
              {
                  names += childLab.Name + ",";
                  if (childLab.Name.Contains("childLab"))
                  {
                      removeControls.Add(childLab);
                  }
                  if (childLab.Name.Contains("expandBtn"))
                  {
                      ExpandButton exBtn = (ExpandButton)childLab as ExpandButton;
                      if (Convert.ToInt32(expandBtn.Tag) != Convert.ToInt32(exBtn.Tag))
                          exBtn.IsExpand = false;
                  }
              }
              foreach (var item in removeControls)
              {
                  expandBtn.Parent.Controls.Remove(item);
                  item.Dispose();
              }

              //还原位置
              foreach (Control childLab in expandBtn.Parent.Controls)
              {
                  if (controlPosition.ContainsKey(childLab.Tag.ToString()))
                  {
                      string position = controlPosition[childLab.Tag.ToString()];
                      if (!String.IsNullOrEmpty(position))
                      {
                          string[] positionList = position.Split('|');
                          string labPosition = positionList[0];
                          string btnPosition = positionList[1];
                          string[] labP = labPosition.Split(',');
                          Point labPoint = new Point(Convert.ToInt32(labP[0]), Convert.ToInt32(labP[1]));
                          string[] btnP = btnPosition.Split(',');
                          Point btnPoint = new Point(Convert.ToInt32(btnP[0]), Convert.ToInt32(btnP[1]));

                          if (childLab.Name.Contains("rootLab"))
                              childLab.Location = labPoint;

                          if (childLab.Name.Contains("expandBtn"))
                              childLab.Location = btnPoint;
                      }
                  }
              }
          }
          ExpandFirst(expandBtn);

          if (SelectIdChanged != null)
          {
              SelectIdChanged(this, e);
          }
      }

      void childLab_Click(object sender, EventArgs e)
      {
          Font childFont = new Font("微软雅黑", 9, FontStyle.Regular);
          Font childSelectFont = new Font("微软雅黑", 9, FontStyle.Bold);
          Color childSelectColor = Color.FromArgb(184, 39, 18);
          Color childColor = Color.FromArgb(119, 119, 119);
          LabelControl childLab = (LabelControl)sender as LabelControl;

          foreach (Control item in childLab.Parent.Controls)
          {
              if (item.Name.Contains("childLabCircle"))
              {
                  panelMain.Controls.Remove(item);
              }
          }

          foreach (Control item in childLab.Parent.Controls)
          {
              if (item.Name.Contains("childLab"))
              {
                  if (childLab.Tag.ToString() == item.Tag.ToString())
                  {
                      item.ForeColor = childSelectColor;
                      item.Font = childSelectFont;

                      Circle circle = new Circle();
                      circle.Name = "childLabCircle" + childLab.Tag.ToString();
                      circle.Location = new Point(item.Location.X - 13, item.Location.Y + 4);
                      panelMain.Controls.Add(circle);
                  }
                  else
                  {
                      item.ForeColor = childColor;
                      item.Font = childFont;
                  }
              }
          }
      }

 

这个控件并非优化的太好,需要优化的有几点

1.切换的时候更加平滑。

2.还无法随意配置菜单内容。

3.控件去DEV化

标签: C# winform
最后更新:2021年4月1日

博主

全栈工程师,侧重项目技术解决方案规划和开发

打赏 点赞
< 上一篇
下一篇 >

文章评论

取消回复

分类
  • .NET (65)
  • docker (3)
  • linux (12)
  • python (20)
  • web (14)
  • 小程序 (4)
  • 数据库 (2)
  • 未分类 (4)
  • 杂七杂八 (10)
标签聚合
nginx centos js C# winform DevExpress python linux
最新 热点 随机
最新 热点 随机
.NET开发手册标准参考 招募兼职前端开发 Centos安装dotnet6环境 VS上切换分支,vs编译运行出现bug,A fatal error was encountered彻底解决方案 用C#封装一个线程安全的缓存器,达到目标定时定量更新入库 C#通过特性的方式去校验指定数据是否为空
一个异常重启winform程序 自己写了个C# 生成简单验证码和加减算法验证码 dynamic的操作 NAudio监听系统声音,屏蔽麦克风其他声音 python守护进程--supervisor 使用教程 小程序-上传图片功能

COPYRIGHT © 2021 陈程的技术博客. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS