【问题标题】:C# Moving/Clicking Mouse WinformsC# 移动/单击鼠标 Winforms
【发布时间】:2017-09-30 23:53:11
【问题描述】:

我想制作一个应用程序,它会自动移动鼠标并在后台按一下按钮即可单击它。我来自销售和库存/HTML Shop 网站编程,这是我第一次制作涉及控制的应用程序。请帮助我,因为我想提高我的编程技能。 这就是我想要做的和我的想法。 *我会为动作的重复放置一个循环计数器

1.获取当前光标的x/y值并保存到名为(坐标)(A点)的变量中

2.右击右下(B点)

3.等待 2 秒

4.使用变量(坐标)移动回到第一个位置

5.结束循环重复。

这是我的想法和算法我的问题是我不知道如何移动鼠标并让它停止。

【问题讨论】:

标签: c# winforms mouse


【解决方案1】:

在 Window Form 项目中要将光标移动到屏幕上的特定点,您可以使用此静态方法。

System.Windows.Forms.Cursor.Position = new Point (X,Y);

要执行点击事件,您可以使用此方法。

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

public void DoMouseClick()
{
    //Call the imported function with the cursor's current position
    uint X = (uint)System.Windows.Forms.Cursor.Position.X;
    uint Y = (uint)System.Windows.Forms.Cursor.Position.Y;
    mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}

【讨论】:

    【解决方案2】:

    您可以移动鼠标编写一些函数或代码,如下所示:

    private void MoveCursor()
    {
       // Set the Current cursor, move the cursor's Position,
       // and set its clipping rectangle to the form. 
    
       this.Cursor = new Cursor(Cursor.Current.Handle);
       Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50);
       Cursor.Clip = new Rectangle(this.Location, this.Size);
    }
    

    How To Move mouse in C#

    要查找表单上任何控件的位置,您可以使用以下代码

    Point locationOnForm = control.FindForm().PointToClient(
        control.Parent.PointToScreen(control.Location));
    

    How to get controls location in Win Forms

    【讨论】:

      猜你喜欢
      • 2019-05-15
      • 1970-01-01
      • 1970-01-01
      • 2011-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多