ToolStripItems在鼠标悬停时显示活动高亮,即使它们所在的窗体没有焦点。但是,除非窗体获得焦点,否则它们不会显示工具提示。我看过ToolStrip 'click-though' hack。有人知道如何使ToolStripButton在其父窗体没有焦点时显示其工具提示吗?
谢谢!
谢谢!
public class ToolStripDropDownEx : ToolStripDropDownButton
{
public ToolStripDropDownEx(string text)
{
}
protected override void OnMouseHover(EventArgs e)
{
if (this.Parent != null)
Parent.Focus();
base.OnMouseHover(e);
}
}
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();
}
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
我曾试图做同样的事情,并确定这将是相当具有挑战性且不值得的。原因是在内部,.NET代码专门设计为仅在窗口处于活动状态时显示工具提示 - 它们在Win32级别进行检查,因此很难欺骗代码。
这是ToolTip.cs中检查“GetActiveWindow()”并返回false的代码片段。您可以在代码中看到注释“工具提示应仅在活动窗口上显示。”
顺便说一下,您可以使用Visual Studio 2008查看.NET BCL的所有源代码,以下是我使用的说明:
// 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;
}