-->

显示在非集中的ToolStripItem工具提示(Showing a tooltip on a no

2019-08-06 17:08发布

ToolStripItems show Active highlighting when you mouse over them, even if the form they are in is not in focus. They do not, however, show their tooltips, unless the form is focused. I have seen the ToolStrip 'click-though' hack. Anyone know how to make a ToolStripButton show its tooltip when its parent form is not in focus?

Thanks!

Answer 1:

问题是,ToolStrip的“控制”之类ToolStripButton或ToolStripDropDownButton不从控制继承。 现在我通过集中每当用户将鼠标悬停在按钮的ToolStrip解决的问题。 按钮的MouseHover事件被触发太晚了 - “显示工具提示”代码将被运行后,所以我延长了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);
    } 
}


Answer 2:

也许在这个代码的两种方法之一将踢你关在正确的方向...

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();
}

与第一个问题是,您可以在不直接设置按钮,它不会从控制继承和工具提示不会出现,除非你是在钢带但不超过一个按钮。

与第二个(注释掉方式)的问题是它不会显示在所有。 不明白为什么,但也许你可以调试出来。



Answer 3:

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

当我创建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

这个作品真的很好,虽然我做的,当你将鼠标悬停在工具条的第1次看到一个很小的初始延迟



Answer 4:

我试图做同样的事情,并确定它是将是非常具有挑战性和不值得。 其原因是在内部,.NET代码是专为只显示提示,如果窗口是活动的 - 他们是在一个Win32级别选中此所以它的将是难以伪造的代码了。

这里是在ToolTip.cs代码段,它检查“GetActiveWindow()”并返回false。 你可以看到在代码中的注释“工具提示应该只能显示在活动的Windows。”

顺便说一句,你可以看到所有的源代码与Visual Studio 2008的.NET基础类库,这里是我使用的指令:

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;
} 


文章来源: Showing a tooltip on a non-focused ToolStripItem