【问题标题】:How to fake mouse cursor position in Windows Forms C#?如何在 Windows 窗体 C# 中伪造鼠标光标位置?
【发布时间】:2023-03-15 18:15:01
【问题描述】:

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

【问题讨论】:

    标签: c# winforms position mouse-cursor


    【解决方案1】:

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

    例如:

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

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

    如果我误解了你,请说明鼠标位置是在哪个时间点使用的。

    【讨论】:

      【解决方案2】:

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

      希望对您有所帮助。

      【讨论】:

        【解决方案3】:

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

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

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

        这里有更多。 Mouse Capture in Windows Forms

        【讨论】:

        • 我的场景中没有按钮,所以我真的不明白你的建议。我将用我当前的代码更新问题...
        【解决方案4】:

        你可以用类做你说的。您可以通过一种非常简单的方式做到这一点。

        一个创建类和

        namespace MousLokasyonbulma
        

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-04
          • 1970-01-01
          • 2022-01-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多