65.9K
CodeProject 正在变化。 阅读更多。
Home

Visual Studio 2005 工具箱克隆

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.77/5 (10投票s)

2008 年 1 月 27 日

CPOL

2分钟阅读

viewsIcon

92221

downloadIcon

3416

使用标准树形视图克隆的 Visual Studio 2005 工具箱。

引言

此控件是一个非常简单的 Visual Studio 工具箱控件克隆。 控件本身只是一个自定义绘制的 TreeView。 因为它基于标准的 treeview,所以它只支持一种 Visual Studio 工具箱的样式 - 列表样式。

背景

几周前,我需要一个看起来像 Visual Studio 2005 中 ToolBox 的简单控件。 CodeProject 上有一些控件可以做到这一点。 但所有这些控件对我来说都太大了。 所以我写了一个自己的...

为了创建工具箱的外观和感觉,我只是重写了 OnDrawNode 方法。 根据节点级别,OnDrawNode 方法将调用根项目或子项目的方法。 如您所见,此控件中只有两个级别是可能的。 第 0 级的项目将作为根项目并绘制为较暗的标题项目。 所有子项(级别 > 0)都将在第 1 级作为子项绘制。

将高级工具提示添加到工具箱有点棘手,因为标准 treeview 不支持此类工具提示。 首先,我必须像这样禁用 treeview 的标准工具提示功能

private const int TVS_NOTOOLTIPS = 0x80;

/// <summary>
/// Disables the tooltip activity for the treenodes.
/// </summary>
protected override CreateParams CreateParams
{
  get
  {
    CreateParams p = base.CreateParams;
    p.Style = p.Style | TVS_NOTOOLTIPS;
    return p;
  }
}

之后,我实现了一个工具提示控件,它支持现代工具提示的高级外观和感觉(也用于原始的 Visual Studio ToolBox)。

Using the Code

此控件的处理非常简单。 如果您曾经使用过 .NET TreeView 控件,那么您应该不会遇到任何问题。 但您必须在此处设置一些属性才能使控件看起来像 Visual Studio ToolBox

  • DrawMode 设置为 OwnerDrawAll
  • FullRowSelect 设置为 true
  • HideSelection 设置为 false
  • HotTracking 设置为 true
  • ItemHeight 设置为 20
  • 添加一个 ImageList

添加新组

// To support the custom tool tips, we
// have to add an enhanced node type to the toolbox.
ToolBox.VSTreeNode newGroup = new ToolBox.VSTreeNode();

newGroup.Text = String.Format("Sample Node {0}", toolBox1.Nodes.Count + 1);

toolBox1.Nodes.Add(newGroup);

向组添加新项目

ToolBox.VSTreeNode newSubItem = new ToolBox.VSTreeNode();

newSubItem.Text =  String.Format("Sample SubItem {0}",
        toolBox1.SelectedNode.Nodes.Count + 1);

// Assuming, that a image list is set.
newSubItem.ImageIndex     = 0;
newSubItem.ToolTipCaption = "Look atg this!";
newSubItem.ToolTipText    = "This is an example ToolTip.";

// It's also possible to add a context menu to a node (root or subitem)
newSubItem.ContextMenuStrip = cmsExample;

// Add the new subitem to the toolbox.
toolBox1.SelectedNode.Nodes.Add(newSubItem);  

更新

2008/04/03

  • 修复了在编辑模式下在左侧蓝色边框上显示的 textbox 的错误
  • 修复了点击组项目的 +/- 时的错误行为
  • 添加了将项目显示为禁用的功能
© . All rights reserved.