代码之家  ›  专栏  ›  技术社区  ›  foson

显示非焦点ToolStripItem上的工具提示

  •  8
  • foson  · 技术社区  · 18 年前

    当您将鼠标移到ToolStripItems上时,即使它们所在的窗体不在焦点中,也会显示活动的高亮显示。但是,除非窗体被聚焦,否则它们不会显示工具提示。我见过 ToolStrip 'click-though' hack . 有人知道如何使ToolStripButton在其父窗体不在焦点时显示其工具提示吗?

    谢谢!

    4 回复  |  直到 17 年前
        1
  •  6
  •   foson    18 年前

    问题是ToolStripButton或ToolStripDropDownButton之类的ToolStrip“控件”不能从控件继承。现在,每当用户将鼠标悬停在按钮上时,我都会关注ToolStrip来解决这个问题。按钮的MouseHover事件触发得太晚了——在“show tooltip”代码运行之后,所以我扩展了ToolStripDropDownButton类并使用了我的新按钮。此方法应适用于从ToolStripItem继承的任何其他类似按钮的类

    public class ToolStripDropDownEx : ToolStripDropDownButton
    {
        public ToolStripDropDownEx(string text)
        {
        }
    
        protected override void OnMouseHover(EventArgs e)
        {
            if (this.Parent != null)
                Parent.Focus();
            base.OnMouseHover(e);
        } 
    }
    
        2
  •  3
  •   Matthew Scharley    18 年前

    也许这段代码中的两种方法中的一种会让你朝着正确的方向前进。。。

    public Form1()
    {
        InitializeComponent();
    
        tooltip = new ToolTip();
        tooltip.ShowAlways = true;
    }
    
    private ToolTip tooltip;
    private void toolStripButton_MouseHover(object sender, EventArgs e)
    {
        if (!this.Focused)
        {
            ToolStripItem tsi = (ToolStripItem)sender;
            tooltip.SetToolTip(toolStrip1, tsi.AutoToolTip ? tsi.ToolTipText : tsi.Text);
            /*tooltip.Show(tsi.AutoToolTip ? tsi.ToolTipText : tsi.Text, this, 
                new Point(toolStrip1.Left, toolStrip1.Bottom));*/
        }
    }
    
    private void toolStripButton_MouseLeave(object sender, EventArgs e)
    {
        tooltip.RemoveAll();
    }
    

    第一种方法的问题是不能直接将其设置为按钮,它不会从控件继承,并且工具提示不会显示,除非您在条带上而不是按钮上。

    第二个(注释掉的方式)的问题是它根本不显示。不太清楚为什么,但也许你可以调试一下。

        3
  •  2
  •   KevinKode    14 年前

    我试了一些东西,发现这是最简单的

    创建toolstripbutton项时,我向其悬停事件添加了一个事件处理程序:

    private sub SomeCodeSnippet()
    
        Me.tooltipMain.ShowAlways = True
    
        Dim tsi As New ToolStripButton(String.Empty, myImage)
        tsi.ToolTipText = "my tool tip text"
        toolstripMain.Add(tsi)
    
        AddHandler tsi.MouseHover, AddressOf ToolStripItem_MouseHover
    
    end sub
    

    Private Sub ToolStripItem_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs)
    
        If TypeOf sender Is ToolStripButton Then
            Me.tooltipMain.SetToolTip(Me.toolstripMain, CType(sender, ToolStripButton).ToolTipText)
        End If
    
    End Sub
    

    这工作得非常好,尽管我注意到当你第一次在toolstrip上停留时有一个很小的初始延迟

        4
  •  1
  •   Maurice Flanagan    17 年前

    顺便说一下,您可以在Visual Studio 2008中看到.NET BCL的所有源代码,以下是我使用的说明:

    http://blogs.msdn.com/sburke/archive/2008/01/16/configuring-visual-studio-to-debug-net-framework-source-code.aspx

    // refer VsWhidbey 498263: ToolTips should be shown only on active Windows.
    private bool IsWindowActive(IWin32Window window)
    { 
        Control windowControl = window as Control;
        // We want to enter in the IF block only if ShowParams does not return SW_SHOWNOACTIVATE. 
        // for ToolStripDropDown ShowParams returns SW_SHOWNOACTIVATE, in which case we DONT want to check IsWindowActive and hence return true. 
        if ((windowControl.ShowParams & 0xF) != NativeMethods.SW_SHOWNOACTIVATE)
        { 
            IntPtr hWnd = UnsafeNativeMethods.GetActiveWindow();
            IntPtr rootHwnd =UnsafeNativeMethods.GetAncestor(new HandleRef(window, window.Handle), NativeMethods.GA_ROOT);
            if (hWnd != rootHwnd)
            { 
                TipInfo tt = (TipInfo)tools[windowControl];
                if (tt != null && (tt.TipType & TipInfo.Type.SemiAbsolute) != 0) 
                { 
                    tools.Remove(windowControl);
                    DestroyRegion(windowControl); 
                }
                return false;
            }
        } 
        return true;
    } 
    
    推荐文章