ComboBoxTreeView

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
 
 
 
namespace TestMyControls
{   /// <summary>
    ///XQ: ComboBoxTreeView
    /// </summary>
    public class MyComboBoxTreeView : ComboBox
    {
        private ListBox listBox = null;//设置变量,用来保存传进来的ListBox对象的值
        private const int WM_LBUTTONDOWN = 0x201, WM_LBUTTONDBLCLK = 0x203;
        ToolStripControlHost treeViewHost;
 
        ToolStripDropDown dropDown;
        public MyComboBoxTreeView()
        {
            
            InitComboBoxTreeView();
            this.DropDownStyle = ComboBoxStyle.DropDown;
 
        }
        //保留传进来的ListBox对象的值,以便调用实现对原来对象行为的控制
        public void AddList(ListBox list)
        {
            this.listBox = list;
        }
        bool isShow = false;
        public event EventHandler<SelectEventArgs> SelectedIndexChanging = delegate { };
        private void InitComboBoxTreeView()
        {
 
            TreeViewAlwaysGotFocus treeView = new TreeViewAlwaysGotFocus();
            treeView.NodeMouseDoubleClick += new TreeNodeMouseClickEventHandler(treeView_NodeMouseDoubleClick);
            treeView.BorderStyle = BorderStyle.None;
            treeView.Dock = DockStyle.Fill;
            treeView.HideSelection = true;
           
          //  treeView.ImageList = Silkworm.Framework.ImageListService.Instance.ImageList;
            treeViewHost = new ToolStripControlHost(treeView);
            dropDown = new ToolStripDropDown();
            this.DisplayMember = "Text";
            dropDown.AutoSize = false;
           // dropDown.Closing += new ToolStripDropDownClosingEventHandler(dropDown_Closing);
            dropDown.Closed += new ToolStripDropDownClosedEventHandler(dropDown_Closed);
            dropDown.MaximumSize = new Size(430, 300);
            dropDown.Size = new System.Drawing.Size(280, 180);
            dropDown.Items.Add(treeViewHost);
 
        }
 
        void dropDown_Closed(object sender, ToolStripDropDownClosedEventArgs e)
        {
            isShow = false;
        }
 
        //void dropDown_Closing(object sender, ToolStripDropDownClosingEventArgs e)
        //{
        //    isShow = false;
        //}
 
        public new object SelectedItem
        {
            get
            {
                return base.SelectedItem;
            }
            set
            {
                SelectEventArgs args = new SelectEventArgs(base.SelectedItem, value);
                SelectedIndexChanging(this, args);
                if (args.Cancel == true)
                {
                    return;
                }
                Items.Clear();
 
                if (value == null)
                {
                    base.SelectedItem = null;
                    return;
                }
                Items.Add(value);
                base.SelectedItem = value;
                if (value is TreeNode)
                {
                    TreeNode tn = value as TreeNode;
 
                    if (tn.TreeView != null)
                    {
                        this.Tag = tn.FullPath;
                        this.Value = tn;
                    }
                }
            }
        }
 
        void treeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (sender is TreeView)
            {
                TreeView tv = sender as TreeView;
                TreeNode tn = tv.SelectedNode;
                if (tn != null)
                {
                    SelectedItem = tn;
                }
            }
            dropDown.Close();
        //    isShow = false;
        }
 
        void TreeViewNodeSelect()
        {
            if (Tag != null)
            {
                TreeNode CurrentNode = TreeView.GetNodeByFullPath(Tag + "");
                TreeView.Focus();
                if (CurrentNode != null)
                {
                    TreeView.SelectedNode = CurrentNode;
                    while (CurrentNode.Parent != null)
                    {
                        CurrentNode.Parent.Expand();
                        CurrentNode = CurrentNode.Parent;
                    }
                }
            }
        }
 
        public TreeView TreeView
        {
            get { return treeViewHost.Control as TreeView; }
        }
 
        private void ShowDropDown()
        {
            if (dropDown != null)
            {
                if (this.listBox!=null)
                {
                    this.listBox.Visible = false;
                }
                if (isShow)
                {
                    dropDown.Close();
                    isShow = false;
                }
                else
                {
                    treeViewHost.Size = new Size(dropDown.Size.Width - 2, dropDown.Size.Height - 5);
                    dropDown.Show(this, 0, this.Height);                  
                    TreeViewNodeSelect();
                    isShow = true;
                }
 
            }
        }
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_LBUTTONDBLCLK || m.Msg == WM_LBUTTONDOWN)
            {
                ShowDropDown();
                return;
            }
            base.WndProc(ref m);
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (dropDown != null)
                {
                    dropDown.Dispose();
                    dropDown = null;
                }
            }
            base.Dispose(disposing);
        }
 
        public object Value { getset; }
 
    }
 
 
    public static class ComboBoxTreeViewExtern
    {
        //扩展,根据fullpath获取TreeView
        public static TreeNode GetNodeByFullPath(this TreeView tv, string FullPath)
        {
            string[] Paths = FullPath.Split('\\');
            TreeNodeCollection Nodes = tv.Nodes;
            int cnt = 0;
        Label001:
            foreach (TreeNode tn in Nodes)
            {
                if (tn.Text == Paths[cnt])
                {
                    cnt++;
                    if (cnt >= Paths.Count())
                    {
                        return tn;
                    }
                    Nodes = tn.Nodes;
                    goto Label001;
                }
 
            }
 
            return null;
        }
    }
 
 
    class TreeViewAlwaysGotFocus : TreeView
    {
        /// <summary>
        /// 屏蔽失焦消息,以保证选中状态和获焦时相同
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            //0x0008=WM_KILLFOCUS
            if (m.Msg == 0x0008)
            {
                return;
            }
            base.WndProc(ref m);
        }
    }
 
    public class SelectEventArgs : EventArgs
    {
 
        public SelectEventArgs(object oldValue, object newValue)
        {
            this.OldValue = oldValue;
            this.NewValue = newValue;
            this.Cancel = false;
        }
 
 
        /// <summary>
        /// 是否取消
        /// </summary>
        public bool Cancel { getset; }
 
        /// <summary>
        /// 旧值
        /// </summary>
        public object OldValue { getset; }
 
        /// <summary>
        /// 新值
        /// </summary>
        public object NewValue { getset; }
    }
}
posted @ 2013-04-15 19:19  Predator  阅读(324)  评论(0编辑  收藏  举报