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

工具栏的扩展接口

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.88/5 (24投票s)

2002年4月11日

3分钟阅读

viewsIcon

195288

downloadIcon

403

此扩展允许工具栏按钮自动调用函数

问题

.NET 中的 ToolbarButtons 缺乏独立执行任务的基本能力。 直接在工具栏本身设置所有工具栏事件会非常方便。

当前被接受的做法是重写 ButtonClick 事件,并使用 case 语句来决定按下了哪个按钮以及应该调用哪个函数。  下面显示了 Microsoft 提供的涂鸦示例。

private void toolBar1_ButtonClick(object sender,
                       System.Windows.Forms.ToolBarButtonClickEventArgs e) 
{
   if(      e.Button == newButton     ) { New( ); } 
   else if( e.Button == openButton    ) { Open( ); } 
   else if( e.Button == saveButton    ) { Save( ); } 
   else if( e.Button == previewButton ) { PrintPreview( ); } 
   else if( e.Button == printButton   ) { Print( ); } 
   else if( e.Button == helpButton    ) { ShowHelpTopics(); }
 } 

或者您可以设计某种方法来查看字符串文本。 我见过一个类似的例子。

void ToolBarOnClick( object obj, ToolBarButtonClickEventArgs e )
{
   switch( e.Button.Text )
   {
      case "&Open":
         ...
         break;
      case "&Close":

         ...

         break;

   }

}

Petzolt 描述的另一种方法会将 ToolBarButtonClickEventArgs 参数转换为 MenuItem

void ToolBarOnClick( object obj, ToolBarButtonClickEventArgs e )
{
ToolBarButton btn = e.Button;
   MenuItem      mnu = (MenuItem) btn.Tag;
  
   mnu.PerformClick( );
}

希望我们能够通过一个快速简单的扩展属性来克服这些缺点。  此外,为您提供一个创建您自己的扩展接口的示例。

背景

在与接口结合使用时,IExtenderProvider 是一个非常强大的工具,但语法却非常简单。  基本上,您需要创建一个 Get/Set 配对,类似于标准的 C# 属性,但显示了全名。

ProvideProperty 标签告诉接口您正在将一个名为 "ToolbarFunction" 的属性添加到您当前开发中的所有组件。  CanExtend 函数将接收此属性的组件范围缩小到仅限 ToolBarButtons。  每个 ToolBarButton 将接收一个新属性,该属性将允许将每个按钮分配给特定的 MenuItem。

[ProvideProperty( "ToolbarFunction", typeof(Component)) ] 
public class ToolbarFunction : Component, IExtenderProvider 
{ 
   public void SetToolbarFunction( Component pComponent, 
                                   MenuItem pMenuItem ) 

       ...
   public MenuItem GetToolbarFunction( Component pComponent ) 

       ...


   public bool CanExtend( object pComponent )
   { 
      return( pComponent is ToolBarButton ); 
   } 
}

接口的源代码非常简单,但我将避免讨论实际的编码过程,以便将本文保持在基本级别。

1. 将 ToolBarFunction 组件添加到 .NET

将扩展接口 DLL (ExtendedInterface.DLL) 复制到您希望的目录后,您需要将其永久添加到您的工具箱。 

  1. 打开工具箱后,右键单击列表以选择“自定义工具箱”。
  2. 选择选项卡 .NET Framework 组件
  3. 点击 B浏览...
  4. 单击 ExtendedInterface.DLL
  5. 点击**确定**。

工具箱应该有两个新组件:StatusMessageToolBarFunction

另一个组件 StatusMessage 在之前的文章中讨论过。

2. 可选:为您的菜单提供有用的名称

Menu Item Properties

一个可选步骤是为每个 MenuItem 的 (Name) 参数使用有意义的名称。 像 MenuItem23 这样的名称当然是可用的,但是当您必须根据这些名称管理功能时,它可能会变得非常复杂。

3. 将 ToolBarFunction 组件添加到您的表单中。

现在是时候将 ToolBarFunction 添加到您的应用程序了。 从您的工具箱中拖动一个,它应该与其他组件一起出现在表单下方。

4. 将 MenuItem 功能分配给您的工具栏。

当然,您需要在表单上有一个 ToolBar。 如果您尚未这样做,请从您的工具箱中拖动一个。

  1. 显示您的 ToolBar 的属性。
  2. 单击“按钮”集合,确保您至少有一个按钮。 随便叫什么,我用了 ToolOpen
  3. 您将看到组件为您添加的“ToolbarFunction”属性。 使用下拉列表查看您所有的菜单项。
  4. 选择您希望按钮在单击时使用的 MenuItem
  5. 单击“确定”以关闭集合。

Adding ToolBar Buttons

5. 测试您的应用程序

一旦您将 ToolBarButtonMenuItem 关联起来,单击按钮将产生与单击菜单相同的效果! 真是太简单了。

源代码

[ProvideProperty( "ToolbarFunction", typeof(Component)) ] 
public class ToolbarFunction : Component, IExtenderProvider 
{ 
   ToolBar m_ToolBar = null; 
   Hashtable m_Dictionary = new Hashtable( ); 
 
   public void SetToolbarFunction( Component pComponent, MenuItem pMenuItem ) 
   { 
      if( ! m_Dictionary.Contains( pComponent )) 
      {  
         m_Dictionary.Add( pComponent, pMenuItem ); 
 
         if( m_ToolBar == null ) 
         {
            ToolBarButton pToolBarButton = pComponent as ToolBarButton; 
 
            if( pToolBarButton != null ) 
            { 
               m_ToolBar = pToolBarButton.Parent as ToolBar; 
 
               if( m_ToolBar != null ) 
                  m_ToolBar.ButtonClick += 
                     new ToolBarButtonClickEventHandler( Handle_ToolbarButtonClick );
            }
         }
      }
      else 
      { 
         m_Dictionary[ pComponent ] = pMenuItem;
      }
   } 
 
   public MenuItem GetToolbarFunction( Component pComponent ) 
   { 
      if( m_Dictionary.Contains( pComponent )) 
         return (MenuItem) m_Dictionary[ pComponent ]; 
 
      return null;
   }
 
   public bool CanExtend( object pComponent )
   { 
      return( pComponent is ToolBarButton ); 
   } 
 
   private void Handle_ToolbarButtonClick( object pComponent, 
                                           ToolBarButtonClickEventArgs e ) 
   { 
      if( m_Dictionary.Contains( e.Button )) 
      { 
         MenuItem pMenuItem = (MenuItem) m_Dictionary[ e.Button ]; 
 
         if( pMenuItem != null ) 
            pMenuItem.PerformClick( ); 
      }
   } 
}
© . All rights reserved.