鼠标单击桌面上的模拟 [英] Mouse Click Simulation on Desktop

查看:78
本文介绍了鼠标单击桌面上的模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为鼠标点击模拟编写代码,但有一个问题。它不是每隔一秒点击,但光标正在移动。谁能帮我 ?这是一个代码:



I write a code for mouse Click Simulation but there is one problem. It isn't making every second Click but cursor is moving. Can any one help me ? here is a code :

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

            [DllImport("user32.dll", EntryPoint = "SetCursorPos")]
            [return: MarshalAs(UnmanagedType.Bool)]
            private static extern bool SetCursorPos(int X, int Y);

    private const int MOUSEEVENTF_ABSOLUTE = 0x8000;
            private const int MOUSEEVENTF_MOVE = 0x0001;
            private const int MOUSEEVENT_LEFTDOWN = 0x0002;
            private const int MOUSEEVENTF_LEFTUP = 0x0004;

    private void button2_Click(object sender, EventArgs e)
            {

                SetCursorPos(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text));
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENT_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text), 0, 0);
                Thread.Sleep(1000);
                SetCursorPos(Convert.ToInt32(textBox3.Text), Convert.ToInt32(textBox4.Text));
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENT_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToInt32(textBox3.Text), Convert.ToInt32(textBox4.Text), 0, 0);
                Thread.Sleep(1000);
                SetCursorPos(Convert.ToInt32(textBox5.Text), Convert.ToInt32(textBox6.Text));
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENT_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToInt32(textBox5.Text), Convert.ToInt32(textBox6.Text), 0, 0);
                Thread.Sleep(1000);
                SetCursorPos(Convert.ToInt32(textBox7.Text), Convert.ToInt32(textBox8.Text));
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENT_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToInt32(textBox7.Text), Convert.ToInt32(textBox8.Text), 0, 0);
                Thread.Sleep(1000);
                SetCursorPos(Convert.ToInt32(textBox9.Text), Convert.ToInt32(textBox10.Text));
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENT_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToInt32(textBox9.Text), Convert.ToInt32(textBox10.Text), 0, 0);
                Thread.Sleep(1000);
                SetCursorPos(Convert.ToInt32(textBox11.Text), Convert.ToInt32(textBox12.Text));
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENT_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToInt32(textBox11.Text), Convert.ToInt32(textBox12.Text), 0, 0);
                Thread.Sleep(1000);
                SetCursorPos(Convert.ToInt32(textBox13.Text), Convert.ToInt32(textBox14.Text));
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENT_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToInt32(textBox13.Text), Convert.ToInt32(textBox14.Text), 0, 0);
                Thread.Sleep(1000);
                SetCursorPos(Convert.ToInt32(textBox15.Text), Convert.ToInt32(textBox16.Text));
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENT_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToInt32(textBox15.Text), Convert.ToInt32(textBox16.Text), 0, 0);
                Thread.Sleep(1000);
                SetCursorPos(Convert.ToInt32(textBox17.Text), Convert.ToInt32(textBox18.Text));
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENT_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToInt32(textBox17.Text), Convert.ToInt32(textBox18.Text), 0, 0);
            }





点击仅在1,3,5,7和9上进行;



Clicks are made just on 1,3,5,7 and 9;

推荐答案

你试图做很多坏事,很难在所有细节中解释这一切。也许你应该尝试接受不要做坏事的公式。



整个想法听起来很大,但它的实施可能更糟。首先,您正在尝试在UI线程上休眠。太糟糕了。您应该只在一个可以使用UI线程调用机制与UI线程通信的单独线程上睡眠,这实际上意味着将任何与UI相关的操作委派给它们所属的UI线程。请查看我过去的答案:

Control.Invoke( )与Control.BeginInvoke() [ ^ ],

Treeview扫描仪和MD5的问题 [ ^ ],

主线程上的.NET事件 [ ^ ]。



现在, mouse_event 已被 SendInput 取代,因此您可以使用:

https:// msdn .microsoft.com / zh-CN / library / windows / desktop / ms646310%28v = vs.85%29.aspx [ ^ ],

http://www.pinvoke.net/default.aspx/user32.sendinput [ ^ ]。



< dd> -SA
You are trying to do so many bad things that it will be hard to explain it all in all the detail. Perhaps you should try with embracing the "Don't do evil" formula.

The whole idea sounds line a big abuse, but its implementation might be even worse. First, you are trying to sleep on a UI thread. Too bad. You should only sleep on a separate thread which could communicate with the UI thread using the UI thread invocation mechanism, which really means delegating any UI-related actions to UI thread where they belong. Please see my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^],
.NET event on main thread[^].

Now, mouse_event has been superseded by SendInput, so this is what you can use:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx[^],
http://www.pinvoke.net/default.aspx/user32.sendinput[^].

—SA


您好,



尝试远离SetCursor API:



Hi,

Try moving away from the SetCursor API:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

public class Form1 : Form
{
   [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
   public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);

   private const int MOUSEEVENTF_LEFTDOWN = 0x02;
   private const int MOUSEEVENTF_LEFTUP = 0x04;
   private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
   private const int MOUSEEVENTF_RIGHTUP = 0x10;

   public Form1()
   {
   }

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

   //...other code needed for the application
}


这篇关于鼠标单击桌面上的模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆