开发VS2005下ComboBoxTreeView(下拉列表框弹出树) 与ToolStripComboBoxTreeView(下拉列表框工具条弹出树)(转)...

最进在使用VS2005开发时,发现有很多新东西,比如,我们常用的ToolBar ,MainMenu,StatusBar,变成了功能强大,样式新颖的,ToolStrip,MenuStrip,StatusStrip,等.不过还是 有些不足,比如,ComboBox 变化不大,下拉框里面只能是文本的,很不方便,我的想法是在下拉ComboBox时会出现TreeView 控件,这也是我今天要做的控件ComboBoxTreeView
开始写了一个,关键点是弹出TreeView 控件,但是把TreeView 做成一个窗体,弹出,还是有什么办法,一查VS2005有一个类窗体弹出类(很强大的对象)ToolStripDropDown, 在使用此类的时候需要传递一个ToolStripControlHost类型的对象,还有个问题就是,TreeView 弹出了,会在它的上方出现了一条小白条,这个问题很棘手,不过如果你懂Win32那就一切OK了,好,我们看看这个类吧.
一:ComboBoxTreeView
None.gif using  System.Data;
None.gif
using  System.Text;
None.gif
using  System.Windows.Forms;
None.gif
namespace  WindowsApplication14
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class ComboBoxTreeView : ComboBox
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private const int WM_LBUTTONDOWN = 0x201, WM_LBUTTONDBLCLK = 0x203;
InBlock.gif        ToolStripControlHost treeViewHost;
InBlock.gif        ToolStripDropDown dropDown;
InBlock.gif        
public ComboBoxTreeView()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            TreeView treeView 
= new TreeView();
InBlock.gif            treeView.AfterSelect
+=new TreeViewEventHandler(treeView_AfterSelect);
InBlock.gif            treeView.BorderStyle 
= BorderStyle.None;
InBlock.gif           
InBlock.gif            treeViewHost 
= new ToolStripControlHost(treeView);
InBlock.gif            dropDown 
= new ToolStripDropDown();
InBlock.gif            dropDown.Width 
= this.Width;
InBlock.gif            dropDown.Items.Add(treeViewHost);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void treeView_AfterSelect(object sender, TreeViewEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.Text=TreeView.SelectedNode.Text;
InBlock.gif            dropDown.Close();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public TreeView TreeView
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn treeViewHost.Control as TreeView; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void ShowDropDown()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (dropDown != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif               treeViewHost.Size 
=new Size(DropDownWidth-2,DropDownHeight);       
InBlock.gif               dropDown.Show(
this0this.Height);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
protected override void WndProc(ref Message m)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (m.Msg == WM_LBUTTONDBLCLK || m.Msg == WM_LBUTTONDOWN)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ShowDropDown();
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }
        
InBlock.gif            
base.WndProc(ref m);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
protected override void Dispose(bool disposing)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (disposing)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (dropDown != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    dropDown.Dispose();
InBlock.gif                    dropDown 
= null;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose(disposing);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif


附图:
ComboBoxTreeView.JPG

二:
ToolStrip
工具条中可以插入文本,下拉框,等,如果要插入下拉树的列表框但不可以直接插入ComboBoxTreeView,必须继承上面提到的ToolStripControlHost类,

None.gif using  System;
None.gif
using  System.Windows;
None.gif
using  System.Windows.Forms;
None.gif
using  System.ComponentModel;
None.gif
using  System.Drawing;
None.gif
using  System.Drawing.Design;
None.gif
using  System.Windows.Forms.Design;
None.gif
None.gif
namespace  WindowsApplication14
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    [DefaultProperty(
"Items")]
InBlock.gif    [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ContextMenuStrip 
| (ToolStripItemDesignerAvailability.MenuStrip | ToolStripItemDesignerAvailability.ToolStrip))]
InBlock.gif    
public class ToolStripComboBoxTreeView : ToolStripControlHost
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
ToolStripComboBoxTreeViewControl#region ToolStripComboBoxTreeViewControl
InBlock.gif        
internal class ToolStripComboBoxTreeViewControl  : ComboBox
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
private const int WM_LBUTTONDOWN = 0x201, WM_LBUTTONDBLCLK = 0x203;
InBlock.gif            ToolStripControlHost treeViewHost;
InBlock.gif            ToolStripDropDown dropDown;
InBlock.gif            
private ToolStripComboBoxTreeView owner;
InBlock.gif
InBlock.gif            
public ToolStripComboBoxTreeView Owner
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
get
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return owner;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
set
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    owner 
= value;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
public ToolStripComboBoxTreeViewControl()
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                TreeView treeView 
= new TreeView();
InBlock.gif                treeView.AfterSelect 
+= new TreeViewEventHandler(treeView_AfterSelect);
InBlock.gif                treeView.BorderStyle 
= BorderStyle.None;
InBlock.gif
InBlock.gif                treeViewHost 
= new ToolStripControlHost(treeView);
InBlock.gif                dropDown 
= new ToolStripDropDown();
InBlock.gif                dropDown.Width 
= this.Width;
InBlock.gif                dropDown.Items.Add(treeViewHost);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
public void treeView_AfterSelect(object sender, TreeViewEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.Text = TreeView.SelectedNode.Text;
InBlock.gif                
if (Owner.DropDown != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Owner.DropDown(
this, e);
ExpandedSubBlockEnd.gif                }

InBlock.gif                dropDown.Close();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
public TreeView TreeView
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
InBlock.gif                    
return treeViewHost.Control as TreeView;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
private void ShowDropDown()
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (dropDown != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    treeViewHost.Size 
= new Size(DropDownWidth - 2, DropDownHeight);
InBlock.gif                    dropDown.Show(
this0this.Height);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
protected override void WndProc(ref Message m)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (m.Msg == WM_LBUTTONDBLCLK || m.Msg == WM_LBUTTONDOWN)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    ShowDropDown();
InBlock.gif                    
return;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
base.WndProc(ref m);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
protected override void Dispose(bool disposing)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (disposing)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (dropDown != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        dropDown.Dispose();
InBlock.gif                        dropDown 
= null;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
base.Dispose(disposing);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif        
public ToolStripComboBoxTreeView()
InBlock.gif            : 
base(ToolStripComboBoxTreeView.CreateControlInstance())
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ToolStripComboBoxTreeView.ToolStripComboBoxTreeViewControl control 
= 
InBlock.gif                
base.Control as ToolStripComboBoxTreeView.ToolStripComboBoxTreeViewControl;
InBlock.gif                control.Owner 
= this;
InBlock.gif                
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private static Control CreateControlInstance()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ComboBox comboBox 
= new ToolStripComboBoxTreeViewControl();
InBlock.gif            
return comboBox;
ExpandedSubBlockEnd.gif        }

ContractedSubBlock.gifExpandedSubBlockStart.gif        
属性#region 属性
InBlock.gif        [Browsable(
false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
InBlock.gif        
public ComboBox ComboBox
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return (base.Control as ComboBox);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        [Browsable(
true)]
InBlock.gif        
public TreeView TreeView
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                    ToolStripComboBoxTreeView.ToolStripComboBoxTreeViewControl control 
= 
InBlock.gif                
base.Control as ToolStripComboBoxTreeView.ToolStripComboBoxTreeViewControl;
InBlock.gif                    
return control.TreeView;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
InBlock.gif        [Editor(
"System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"typeof(UITypeEditor))]
InBlock.gif        [Browsable(
true)]
InBlock.gif        [EditorBrowsable(EditorBrowsableState.Always)] 
InBlock.gif        [Localizable(
true)]
InBlock.gif        
public AutoCompleteStringCollection AutoCompleteCustomSource
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.ComboBox.AutoCompleteCustomSource;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.ComboBox.AutoCompleteCustomSource = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        [Browsable(
false)]
InBlock.gif        [ DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
InBlock.gif        
public int SelectionLength
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.ComboBox.SelectionLength;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.ComboBox.SelectionLength = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        [Browsable(
false)]
InBlock.gif        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
InBlock.gif        
public int SelectionStart
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.ComboBox.SelectionStart;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.ComboBox.SelectionStart = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        [DefaultValue(
0)]
InBlock.gif        [Localizable(
true)]
InBlock.gif        [Description(
"下拉框最大长度")]
InBlock.gif        
public int MaxLength
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.ComboBox.MaxLength;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.ComboBox.MaxLength = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
    
InBlock.gif        [DefaultValue(
1)]
InBlock.gif        [RefreshProperties(RefreshProperties.Repaint)]
InBlock.gif        
public ComboBoxStyle DropDownStyle
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.ComboBox.DropDownStyle;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.ComboBox.DropDownStyle = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        [EditorBrowsable(EditorBrowsableState.Always)]
InBlock.gif        [Browsable(
true)]
InBlock.gif        [DefaultValue(
106)]
InBlock.gif        
public int DropDownHeight
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.ComboBox.DropDownHeight;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.ComboBox.DropDownHeight = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public int DropDownWidth
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.ComboBox.DropDownWidth;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.ComboBox.DropDownWidth = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ContractedSubBlock.gifExpandedSubBlockStart.gif        
方法#region 方法
InBlock.gif        
public void SelectAll()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ComboBox.SelectAll();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void Select(int start, int length)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ComboBox.Select(start, length);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ContractedSubBlock.gifExpandedSubBlockStart.gif        
事件#region 事件
InBlock.gif      
InBlock.gif        
public event EventHandler DropDown;
InBlock.gif 
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif       
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
附图:
ToolStripComboBoxTreeView.JPG
客户端调用方法:
public static void Main(){
   toolStripComboBoxTreeView1.TreeView.ImageList = this.imageList1;
            TreeNode root = new TreeNode("根节点",1,2);
            root.Nodes.Add("节点1");
            root.Nodes.Add("节点2");
            root.Nodes.Add("节点3");
            root.Nodes.Add("节点4");
            root.Nodes.Add("节点5");
            root.Nodes.Add("节点6");
            toolStripComboBoxTreeView1.TreeView.Nodes.Add(root);
}

转载于:https://www.cnblogs.com/xiongeee/archive/2007/03/03/662468.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值