简体   繁体   English

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

[英]How to fake mouse cursor position in Windows Forms C#?

I have this Windows Forms application with a simple balloon tooltip. 我有一个带有简单气球工具提示的Windows窗体应用程序。 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. 根据应用程序在桌面上的窗口位置和鼠标光标位置,气球“tip”(或气球指向箭头)可能指向或不指向我想要的位置。

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. 例如,我的应用程序捕捉到桌面两侧,当它被捕捉到右侧时,如果鼠标光标低于右侧的100px,则气球“提示”将指向错误的位置。 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);
}

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. 会在controlOnWhichToShow的左上角显示尖端,距离边缘15个点。

If I misunderstood you, than please specify at which point in time is the mouse position being used. 如果我误解了你,请指明在哪个时间点使用鼠标位置。

If you sync the MouseHover event, you can create the Tooltip as veljkoz describes. 如果同步MouseHover事件,则可以像veljkoz描述的那样创建工具提示。 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. 希望有所帮助。

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. 在Windows窗体中,当用户按下控件上的鼠标按钮时,控件捕获鼠标,当用户释放鼠标按钮时,控件释放鼠标。

The Capture property of the Control class specifies whether a control has captured the mouse. Control类的Capture属性指定控件是否捕获了鼠标。 To determine when a control loses mouse capture, handle the MouseCaptureChanged event. 要确定控件何时失去鼠标捕获,请处理MouseCaptureChanged事件。

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 Windows窗体中的鼠标捕获

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); {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; public benimtooltip(){this.OwnerDraw = true; this.Draw += Benimtooltip_Draw; 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 完整的代码MyGithup

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

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

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