如何在 Windows 窗体 C# 中伪造鼠标光标位置?

发布于 2024-09-14 01:14:56 字数 1003 浏览 4 评论 0原文

我有一个带有简单气球工具提示的 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);
}

I have this Windows Forms application with a simple balloon tooltip. Depending on the application's window location on the desktop and the mouse cursor location, the balloon 'tip' (or balloon pointing arrow) may or may not be pointing to the location I want.

For instance, my app snaps to the desktop sides and when it's snapped to the right side, if the mouse cursor is below 100px of the right side, the balloon 'tip' will point to the wrong place. But if the mouse cursor is anywhere else, it will point to the right place.

In this situation I wanted to fake the mouse cursor position (without actually changing the mouse cursor position) to be somewhere else so the the problem wouldn't occur.

Is this possible? How can I achieve this?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

西瓜 2024-09-21 01:14:56

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

例如:

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

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

如果我误解了您的意思,请指定使用鼠标位置的时间点。

I'm not quite sure why do you need to set cursor position, because you can set tool tip to appear where you tell it, and not necessarily where the mouse is.

For example:

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

would display the tip at upper left corner of the controlOnWhichToShow, 15 points away from edges.

If I misunderstood you, than please specify at which point in time is the mouse position being used.

雄赳赳气昂昂 2024-09-21 01:14:56

如果同步 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);
}

希望有帮助。

If you sync the MouseHover event, you can create the Tooltip as veljkoz describes. In this way you can place the tooltip as you like. The code would look smething like this:

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

Hope that helps.

佞臣 2024-09-21 01:14:56

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

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

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

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

In Windows Forms the mouse is captured by the control when the user presses a mouse button on a control, and the mouse is released by the control when the user releases the mouse button.

The Capture property of the Control class specifies whether a control has captured the mouse. To determine when a control loses mouse capture, handle the MouseCaptureChanged event.

Only the foreground window can capture the mouse. When a background window attempts to capture the mouse, the window receives messages only for mouse events that occur when the mouse pointer is within the visible portion of the window. Also, even if the foreground window has captured the mouse, the user can still click another window, bringing it to the foreground. When the mouse is captured, shortcut keys do not work.

More here. Mouse Capture in Windows Forms

荭秂 2024-09-21 01:14:56

您可以通过类执行您所说的操作。您可以用一种非常简单的方式做到这一点。

一个创建类并且

namespace MousLokasyonbulma

{
benimtooltip 类:工具提示
{
[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

You can do what you say with a Class. You can do it in a very simple way.

one create class and

namespace MousLokasyonbulma

{
class benimtooltip : ToolTip
{
[System.Runtime.InteropServices.DllImport("User32.dll")]
static extern bool MoveWindow(IntPtr h, int x, int y, int width, int height, bool redraw);
public 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);
    }
}

}

full Code MyGithup

Example Project image
https://i.hizliresim.com/1pndZG.png
https://i.hizliresim.com/Lvo3Rb.png

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文