Winform ToolTip 位置设置 [英] Winform ToolTip location setting

查看:30
本文介绍了Winform ToolTip 位置设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能通过 MouseHover 事件在空桌面上的固定点中以某种方式在应用程序窗体之外找到 ToolTip 的弹出窗口,当然,该事件是否对 ToolTip 有用,不确定.或者任何其他可能的方式

I'm wondering if it is possible somehow locate popup of ToolTip outside of application form in the fixed point over the empty desktop with MouseHover event, of course if event is useful for ToolTip, not sure. Or any other way if it is possible

我不是在问如何显示另一个表单作为实现此目标的选项.

I'm not asking for how to display another form as an option for this goal.

推荐答案

您可以使用以下任一选项:

You can use either of these options:

  1. 自行处理显示和隐藏 ToolTip.您可以使用 MouseHover 在所需位置显示 ToolTip 并使用 MouseLeave 隐藏它.

  1. Handle showing and hiding the ToolTip yourself. You can use MouseHover show the ToolTip in desired location and using MouseLeave hide it.

使用 MoveWindow Windows API 方法,强制工具提示显示在特定位置而不是默认位置.

Using MoveWindow Windows API method, force the tooltip to show in a specific location instead of default location.

选项 1

您可以通过这种方式处理控件的 MouseHoverMouseLeave 事件并在桌面窗口的特定位置显示 ToolTip:

You can handle MouseHover and MouseLeave event of your control(s) and show ToolTip in specific location of desktop window this way:

private void control_MouseHover(object sender, EventArgs e) 
{
    var control = (Control)sender;
    var text = toolTip1.GetToolTip(control);
    if (!string.IsNullOrEmpty(text))
        toolTip1.Show(text, control, control.PointToClient(new Point(100, 100)));
}
private void control_MouseLeave(object sender, EventArgs e)
{
    var control = (Control)sender;
    toolTip1.Hide(control);
}

选项 2

作为我之前为对齐控件和工具提示的右边缘提供的另一个选项,您可以设置OwnerDrawToolTip 的属性设为true 并处理控件的Draw 事件并使用MoveWindowToolTip 移动到所需位置的 Windows API 方法:

As another option which I previously offered for align right edges of a control and ToolTip, you can set OwnerDraw property of ToolTip to true and handle Draw event of the control and use MoveWindow Windows API method to move ToolTip to desired location:

[System.Runtime.InteropServices.DllImport("User32.dll")]
static extern bool MoveWindow(IntPtr h, int x, int y, int width, int height, bool redraw);
private void toolTip1_Draw(object sender, DrawToolTipEventArgs e) {
    e.DrawBackground();
    e.DrawBorder();
    e.DrawText();
    var t = (ToolTip)sender;
    var h = t.GetType().GetProperty("Handle",
      System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
    var handle = (IntPtr)h.GetValue(t);
    var location = new Point(100,100);
    MoveWindow(handle, location.X, location.Y, e.Bounds.Width, e.Bounds.Height, false);
}

这篇关于Winform ToolTip 位置设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆