5

我有这个带有简单气球工具提示的 Windows 窗体应用程序。根据应用程序在桌面上的窗口位置和鼠标光标位置,气球“提示”(或气球指向箭头)可能指向也可能不指向我想要的位置。

例如,我的应用程序捕捉到桌面侧,当它捕捉到右侧时,如果鼠标光标低于右侧 100 像素,气球“提示”将指向错误的位置。但如果鼠标光标在其他任何地方,它将指向正确的位置。

在这种情况下,我想将鼠标光标位置(不实际更改鼠标光标位置)伪造到其他地方,这样问题就不会发生。

这可能吗?我怎样才能做到这一点?

private void noteTitleInput_KeyPress(object sender, KeyPressEventArgs e) {
    if(e.KeyChar == Convert.ToChar(Keys.Return, CultureInfo.InvariantCulture) && noteTitleInput.Text.Length > 0) {
        e.Handled = true;

        noteInputButton_Click(null, null);
    } else if(!Char.IsControl(e.KeyChar)) {
        if(Array.IndexOf(Path.GetInvalidFileNameChars(), e.KeyChar) > -1) {
            e.Handled = true;

            System.Media.SystemSounds.Beep.Play();

            noteTitleToolTip.Show("The following characters are not valid:\n\\ / : * ? < > |",
                groupNoteInput, 25, -75, 2500);

            return;
        }
    }

    noteTitleToolTip.Hide(groupNoteInput);
}
4

4 回答 4

4

我不太清楚你为什么需要设置光标位置,因为你可以设置工具提示出现在你告诉它的位置,而不一定是鼠标所在的位置。

例如:

tooltip1.Show("My tip", controlOnWhichToShow, 15, 15);

将在 controlOnWhichToShow 的左上角显示提示,距离边缘 15 个点。

如果我误解了您,请指定在哪个时间点使用鼠标位置。

于 2010-09-08T15:54:56.357 回答
3

如果您同步 MouseHover 事件,您可以按照 veljkoz 的描述创建工具提示。通过这种方式,您可以随意放置工具提示。代码看起来像这样:

protected override void OnMouseHover(EventArgs e)
{
  ToolTip myToolTip = new ToolTip();
  myToolTip.IsBalloon = true;
  // TODO The x and y coordinates should be what ever you wish.
  myToolTip.Show("Helpful Text Also", this, 50, 50);
  base.OnMouseHover(e);
}

希望有帮助。

于 2010-09-08T16:13:51.697 回答
0

在 Windows 窗体中,当用户在控件上按下鼠标按钮时,鼠标被控件捕获,当用户释放鼠标按钮时,鼠标被控件释放。

Control 类的 Capture 属性指定控件是否已捕获鼠标。若要确定控件何时失去鼠标捕获,请处理 MouseCaptureChanged 事件。

只有前台窗口可以捕获鼠标。当后台窗口试图捕捉鼠标时,窗口只接收鼠标指针在窗口可见部分内时发生的鼠标事件的消息。此外,即使前台窗口已经捕获了鼠标,用户仍然可以单击另一个窗口,将其带到前台。捕获鼠标时,快捷键不起作用。

更多在这里。Windows 窗体中的鼠标捕获

于 2010-08-14T19:52:35.087 回答
0

你可以用 Class 做你所说的。你可以用一种非常简单的方式来做到这一点。

一个创建类和

namespace MousLokasyonbulma

{ 类 beimtooltip : ToolTip { [System.Runtime.InteropServices.DllImport("User32.dll")] static extern bool MoveWindow(IntPtr h, int x, int y, int width, int height, bool redraw); 公共 benimtooltip() { this.OwnerDraw = true; this.Draw += Benimtooltip_Draw; }

    private void Benimtooltip_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(650, 650);
        var ss= MoveWindow(handle, location.X, location.Y, e.Bounds.Width, e.Bounds.Height, false);
    }
}

}

完整代码MyGithup

示例项目图片 https://i.hizliresim.com/1pndZG.png https://i.hizliresim.com/Lvo3Rb.png

于 2019-06-05T05:13:10.577 回答