简体   繁体   English

Winform ToolTip位置设置

[英]Winform ToolTip location setting

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. 我想知道是否有可能通过MouseHover事件以某种方式将ToolTip的弹出窗口定位在空桌面上固定位置的应用程序窗体外部的应用程序窗体中,当然,如果事件对ToolTip有用(不确定)。 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. Handle showing and hiding the ToolTip yourself. 自己处理显示和隐藏ToolTip You can use MouseHover show the ToolTip in desired location and using MouseLeave hide it. 您可以使用MouseHover在所需位置显示ToolTip ,并使用MouseLeave隐藏它。

  2. Using MoveWindow Windows API method, force the tooltip to show in a specific location instead of default location. 使用MoveWindow Windows API方法,强制工具提示显示在特定位置而不是默认位置。

Option 1 选项1

You can handle MouseHover and MouseLeave event of your control(s) and show ToolTip in specific location of desktop window this way: 您可以通过以下方式处理MouseHoverMouseLeave事件,并在桌面窗口的特定位置显示ToolTip

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

Option 2 选项2

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: 作为我先前提供的用于对齐控件和ToolTip的右边缘的另一个选项,可以将ToolTip OwnerDraw属性设置为true并处理控件的Draw事件,并使用MoveWindow Windows API方法将ToolTip移至所需位置:

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

It sounds like ultimately what you want is a box to display some information whenever you hover over some particular items on your GUI. 听起来,最终您想要的是一个当您将鼠标悬停在GUI上的某些特定项目上时显示一些信息的框。 You also say that you want the information to display at a fixed point. 您还说您希望信息固定显示。

As opposed to achieving this with the tool-tip, I would do the following: 与通过工具提示实现此目的相反,我将执行以下操作:

  1. Create some fixed label or text-box to display information and put it somewhere on your Windows form. 创建一些固定的标签或文本框以显示信息,并将其放在Windows窗体上的某个位置。
  2. Create a subscriber to the mouse hover event. 创建一个鼠标悬停事件的订阅服务器。
  3. Based on the sender (which control you're hovering) from the mouse hover event, choose what information to display in the fixed location. 根据鼠标悬停事件中的sender (由您控制​​悬停),选择要在固定位置显示的信息。

I've seen people doing this in some other programs... Take, RealTerm for example. 我见过其他人在其他程序中执行此操作...以RealTerm为例。 Try it out if you want and see how it feels before you try this solution. 如果需要,请尝试一下,然后在尝试此解决方案之前先看一下感受。

On the other hand, if you must do this with a tool-tip. 另一方面,如果必须使用工具提示来执行此操作。 You can choose the position using overloads of ToolTip.Show . 您可以使用ToolTip.Show重载来选择位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM