| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- /*******************************************************************************
- * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
- * 类的信息:
- * 1.程序名称:DKButtonTreeView.cs
- * 2.功能描述:系统导航栏中Button和TreeView形式用户控件
- * 编辑履历:
- * 作者 日期 版本 修改内容
- * 张国印 2014/08/27 1.00 新建
- *******************************************************************************/
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace Dongke.IBOSS.PRD.Basics.BaseControls
- {
- /// <summary>
- /// 系统导航栏中Button和TreeView形式用户控件
- /// </summary>
- public partial class DKButtonTreeView : UserControl, IDKControl
- {
- #region 自定义控件公用属性和对外事件定义
- public delegate void TreeView_DoubleClick(object sender, EventArgs e);
- public event TreeView_DoubleClick TreeViewDoubleClick;
- public delegate void TreeView_AfterCollapse(object sender, TreeViewEventArgs e);
- public event TreeView_AfterCollapse TreeViewAfterCollapse;
- public delegate void TreeView_AfterExpand(object sender, TreeViewEventArgs e);
- public event TreeView_AfterExpand TreeViewAfterExpand;
- public delegate TreeNode TreeView_GetTreeViewNodes(string appID);
- public event TreeView_GetTreeViewNodes GetTreeViewNodes;
- public Dictionary<string, string> dicButton;
- private int buttonHight = 28;
- private int buttonCount = 0;
- private bool _beginAsyncStatus; // 异步处理开始时,控件状态
- #endregion
- #region 构造函数
- public DKButtonTreeView()
- {
- InitializeComponent();
- this.dicButton = new Dictionary<string, string>();
- }
- public DKButtonTreeView(Dictionary<string, string> pDicButtons)
- {
- InitializeComponent();
- this.dicButton = pDicButtons;
- }
- #endregion
- #region 对应事件处理
- private void DKButtonTreeView_Load(object sender, EventArgs e)
- {
- ButtonAndTreeIni();
- }
- private void panelMemu_Layout(object sender, LayoutEventArgs e)
- {
- this.tvMemu.Width = this.panelMemu.Width - 2;
- this.tvMemu.Height = this.panelMemu.Height - (buttonCount * buttonHight);
- }
- private void tvMemu_DoubleClick(object sender, EventArgs e)
- {
- if (TreeViewDoubleClick != null)
- {
- TreeViewDoubleClick(sender, e);
- }
- }
- private void tvMemu_AfterCollapse(object sender, TreeViewEventArgs e)
- {
- if (TreeViewAfterCollapse != null)
- {
- TreeViewAfterCollapse(sender, e);
- e.Node.SelectedImageIndex = 0;
- e.Node.ImageIndex = 0;
- }
- }
- private void tvMemu_AfterExpand(object sender, TreeViewEventArgs e)
- {
- if (TreeViewAfterExpand != null)
- {
- e.Node.SelectedImageIndex = 2;
- e.Node.ImageIndex = 2;
- TreeViewAfterExpand(sender, e);
- }
- }
- #endregion
- #region 自定义方法
- /// <summary>
- /// 设置要显示的一级菜单项目
- /// </summary>
- /// <param name="pDicButtons"></param>
- public void SetMenuDictionart(Dictionary<string, string> pDicButtons)
- {
- this.dicButton = pDicButtons;
- ButtonAndTreeIni();
- }
- /// <summary>
- /// 设置空间的停靠属性,默认第一个面板展开
- /// </summary>
- /// <param name="index"></param>
- private void SetSplitContainerDock(int index)
- {
- foreach (Control control in this.panelMemu.Controls)
- {
- if (control is Button)
- {
- Button btn = (Button)control;
- if (btn.TabIndex > index)
- {
- if (btn.Dock != DockStyle.Bottom)
- {
- btn.Dock = DockStyle.Bottom;
- btn.BringToFront(); //把控件置于顶层
- }
- }
- else
- {
- if (btn.Dock != DockStyle.Top)
- {
- btn.Dock = DockStyle.Top;
- btn.BringToFront(); //把控件置于顶层
- }
- }
- }
- this.tvMemu.Left = 0;
- this.tvMemu.Top = index * buttonHight;
- }
- }
- /// <summary>
- /// 根据模块编号获取该模块下的数据
- /// </summary>
- /// <param name="appId"></param>
- private void GetModulesOfApplication(string appId)
- {
- if (GetTreeViewNodes != null)
- {
- this.tvMemu.Nodes.Clear();
- TreeNode tempNode = GetTreeViewNodes(appId);
- if (tempNode != null)
- {
- this.tvMemu.Nodes.Add(tempNode);
- this.tvMemu.ExpandAll();
- if (this.tvMemu.Nodes != null && this.tvMemu.Nodes.Count > 0)
- {
- this.tvMemu.Nodes[0].EnsureVisible();
- }
- }
- }
- }
- /// <summary>
- /// 初始化用户控件方法
- /// </summary>
- private void ButtonAndTreeIni()
- {
- int width = this.panelMemu.Width;
- int index = 0;
- int tabIndex = 0;
- string applicationId = "01";
- if (this.dicButton.Count > 0)
- {
- buttonCount = this.dicButton.Count;
- Button[] btnCollections = new Button[buttonCount];
- foreach (var newKey in dicButton)
- {
- #region 为Button控件赋值
- Button btn = new Button
- {
- Tag = newKey.Key,
- Text = newKey.Value,
- Width = width - 2,
- Height = buttonHight,
- Dock = DockStyle.Top,
- BackColor = SystemColors.Control,
- UseVisualStyleBackColor = false, //不使用XP主题样式
- TextAlign = ContentAlignment.MiddleCenter,
- TabIndex = index + 1,
- };
- #endregion
- #region 设置Button控件事件
- btn.Click += (s, ex) =>
- {
- Button clickedButton = (Button)s;
- int clickedButtonTabIndex = clickedButton.TabIndex;
- //设置按钮的停靠顺序
- SetSplitContainerDock(clickedButtonTabIndex);
- //加载系统菜单
- string strTag = clickedButton.Tag.ToString();
- GetModulesOfApplication(strTag);
- this.tvMemu.BringToFront();
- this.tvMemu.Focus(); //2014-9-30 张国印 增加 用于获取焦点 使得滚动条可用
- };
- #endregion
- if (index == 0)
- {
- tabIndex = 1;
- applicationId = btn.Tag.ToString();
- }
- btnCollections[index++] = btn;
- }
- Array.Reverse(btnCollections); //反转数组,改变控件的Z顺序,此句将直接影响下面添加的顺序
- this.panelMemu.Controls.AddRange(btnCollections);
- SetSplitContainerDock(tabIndex); //设置按钮的停靠顺序
- GetModulesOfApplication(applicationId); //加载系统菜单
- this.panelMemu.Padding = new Padding { Left = 0, Right = 0 }; //设置折叠面板和外边缘的间隔像素
- this.tvMemu.Width = width - 2;
- this.tvMemu.Height = this.panelMemu.Height - (buttonCount * buttonHight);
- this.tvMemu.Focus(); //2014-9-30 张国印 增加 用于获取焦点 使得滚动条可用
- }
- }
- #endregion
- #region 公有方法
- /// <summary>
- /// 异步处理开始
- /// </summary>
- public void BeginAsync()
- {
- this._beginAsyncStatus = this.Enabled;
- this.Enabled = true;
- }
- /// <summary>
- /// 异步处理结束
- /// </summary>
- public void EndAsync()
- {
- this.Enabled = this._beginAsyncStatus;
- }
- #endregion
- }
- }
|