ToolTip.Show为非活动窗口不起作用

时间:2013-10-14 15:47:52

标签: c# winforms tooltip

当包含控件的窗口处于非活动状态时,为什么不显示使用ToolTip.Show手动显示的工具提示?

public class MyControl : Button
{

    private _tip;
    public string ToolTip
    {
        get { return _tip; }
        set { _tip = value; }
    }

    private ToolTip _toolTip = new ToolTip();

    public MyControl()
    {
        _toolTip.UseAnimation = false;
        _toolTip.UseFading = false;
        _toolTip.ShowAlways = true;
    }

    protected override void OnMouseHover(EventArgs e)
    {
        _toolTip.Show(_tip, this, 0, Height);
        base.OnMouseHover(e);
    }

    protected override void OnMouseLeave(EventArgs e)
    {
        _toolTip.Hide(this);
        base.OnMouseLeave(e);
    }
}

我去了ToolTip.Show,因为我必须在屏幕上显示工具提示无限时间,这对于普通ToolTip是不可能的。我也喜欢将工具提示文本作为控件本身的一部分。但不幸的是,当这种方式显示非活动窗口的工具提示时(尽管ShowAlways = true),它根本不起作用。

OnMouseHower事件有所增加,但_toolTip.Show什么也没做。除非激活窗口,否则一切正常。

恩惠

为解决方案添加赏金以显示非活动表单的工具提示(当工具提示文本是控件属性时,最好使用解决方案,而不是IContainer)。

1 个答案:

答案 0 :(得分:5)

有一个私有方法可以执行您想要的操作,因此要访问它,您必须使用反射来调用它:

using System.Reflection;

public class MyControl : Button {
  private ToolTip toolTip = new ToolTip() {
    UseAnimation = false,
    UseFading = false
  };

  public string ToolTip { get; set; }

  protected override void OnMouseHover(EventArgs e) {
    base.OnMouseHover(e);
    Point mouse = MousePosition;
    mouse.Offset(10, 10);
    MethodInfo m = toolTip.GetType().GetMethod("SetTool",
                           BindingFlags.Instance | BindingFlags.NonPublic);
    m.Invoke(toolTip, new object[] { this, this.ToolTip, 2, mouse });
  }

  protected override void OnMouseLeave(EventArgs e) {
    base.OnMouseLeave(e);
    toolTip.Hide(this);
  }
}

提示将显示在非活动窗口上,它将无限期地保留在屏幕上,直到鼠标离开控件。