让所有工具栏所有按钮的MouseMove使用同一个事件处理方法,再去判断sender就可以了。

解决方案 »

  1.   

    经过努力,终于想到了解决你问题的方法:
    ------------------------------------------private void toolBar1_MouseMove(object sender, MouseEventArgs e)
    {
      int width = 0;
      foreach(ToolBarButton tbtn in toolBar1.Buttons)
      {
        if(tbtn.Style != ToolBarButtonStyle.Separator)
        {
          width += tbtn.Rectangle.Width;
          if(e.X < width)
          {
            statusBar1.Text = tbtn.Text;
            break;
          }
        }
        else
        {
          width += tbtn.Rectangle.Width;
          statusBar1.Text = "";
        }
       }
    }
      

  2.   

    完整代码如下:
    -----------------------------------------------------------
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace WindowsApplication3
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.ToolBar toolBar1;
    private System.Windows.Forms.ToolBarButton toolBarButton1;
    private System.Windows.Forms.ToolBarButton toolBarButton2;
    private System.Windows.Forms.ToolBarButton toolBarButton3;
    private System.Windows.Forms.ToolBarButton toolBarButton4;
    private System.Windows.Forms.ToolBarButton toolBarButton5;
    private System.Windows.Forms.StatusBar statusBar1;
    private System.Windows.Forms.ToolBarButton toolBarButton6;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.toolBar1 = new System.Windows.Forms.ToolBar();
    this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
    this.toolBarButton2 = new System.Windows.Forms.ToolBarButton();
    this.toolBarButton3 = new System.Windows.Forms.ToolBarButton();
    this.toolBarButton4 = new System.Windows.Forms.ToolBarButton();
    this.toolBarButton5 = new System.Windows.Forms.ToolBarButton();
    this.statusBar1 = new System.Windows.Forms.StatusBar();
    this.toolBarButton6 = new System.Windows.Forms.ToolBarButton();
    this.SuspendLayout();
    // 
    // toolBar1
    // 
    this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
    this.toolBarButton1,
    this.toolBarButton2,
    this.toolBarButton3,
    this.toolBarButton6,
    this.toolBarButton4,
    this.toolBarButton5});
    this.toolBar1.DropDownArrows = true;
    this.toolBar1.Name = "toolBar1";
    this.toolBar1.ShowToolTips = true;
    this.toolBar1.Size = new System.Drawing.Size(292, 38);
    this.toolBar1.TabIndex = 0;
    this.toolBar1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.toolBar1_MouseMove);
    // 
    // toolBarButton1
    // 
    this.toolBarButton1.Text = "1";
    // 
    // toolBarButton2
    // 
    this.toolBarButton2.Text = "2";
    // 
    // toolBarButton3
    // 
    this.toolBarButton3.Text = "3";
    // 
    // toolBarButton4
    // 
    this.toolBarButton4.Text = "4";
    // 
    // toolBarButton5
    // 
    this.toolBarButton5.Text = "5";
    // 
    // statusBar1
    // 
    this.statusBar1.Location = new System.Drawing.Point(0, 251);
    this.statusBar1.Name = "statusBar1";
    this.statusBar1.Size = new System.Drawing.Size(292, 22);
    this.statusBar1.TabIndex = 1;
    this.statusBar1.Text = "statusBar1";
    // 
    // toolBarButton6
    // 
    this.toolBarButton6.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.statusBar1,
      this.toolBar1});
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void toolBar1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    int width = 0;
    foreach(ToolBarButton tbtn in toolBar1.Buttons)
    {
    if(tbtn.Style != ToolBarButtonStyle.Separator)
    {
    width += tbtn.Rectangle.Width;
    if(e.X < width)
    {
    statusBar1.Text = tbtn.Text;
    break;
    }
    }
    else
    {
    width += tbtn.Rectangle.Width;
    statusBar1.Text = "";
    }
    }
    }
    }
    }