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

5
我有一个Windows Forms应用程序,里面有一个简单的气球提示。根据应用程序窗口在桌面上的位置和鼠标光标的位置,气球提示(或指向箭头)可能会指向我想要的位置,也可能不会。
例如,我的应用程序贴到桌面的边缘时,如果它贴到右侧,如果鼠标光标在右侧下方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
我不太确定为什么需要设置光标位置,因为您可以将工具提示设置为出现在您指定的位置,而不一定是鼠标所在的位置。
例如:
tooltip1.Show("My tip", controlOnWhichToShow, 15, 15);

在控件的左上角显示提示,距边缘 15 点。

如果我误解了您,请具体说明鼠标位置何时被使用。


3
如果您同步MouseHover事件,可以按照veljkoz的描述创建Tooltip。这样,您可以根据需要放置提示框。代码可能如下所示:

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

希望这能帮到您。

0

你可以使用类来实现你所说的功能。而且,这可以非常简单地完成。

只需要创建一个类即可。

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

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

}

完整代码 我的Github

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


0
在Windows Forms中,当用户在控件上按下鼠标按钮时,鼠标会被该控件捕获,并且当用户释放鼠标按钮时,鼠标会被该控件释放。
Control类的Capture属性指定了一个控件是否已经捕获了鼠标。要确定控件何时失去鼠标捕获,请处理MouseCaptureChanged事件。
只有前景窗口可以捕获鼠标。当后台窗口尝试捕获鼠标时,该窗口仅接收鼠标事件的消息,这些事件发生在鼠标指针位于窗口可见部分内时。此外,即使前景窗口已经捕获了鼠标,用户仍然可以单击另一个窗口,将其置于前景。当鼠标被捕获时,快捷键无法使用。
更多信息请参见Windows Forms中的鼠标捕获

我的情况中没有涉及到按钮,所以我真的不明白你在建议什么。我将会更新问题并附上我的当前代码... - rfgamaral

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接