0


两个虚拟鼠标的图像任务是水平移动并单击目标点。下面的代码在 Sequence 中成功运行(一个接一个)。我想将此代码编辑为并行工作(两个图像同时工作)。

mouseimg1 指的是 => mouse 1 和 mouseimg2 指的是 => mouse 2 。

public partial class Form1 : Form
{
    public delegate void delMouse();
    public delMouse delMouse1; enter code here
    public delMouse delMouse2;
    private Thread t1, t2;
    public Form1()
    {
        InitializeComponent();
        delMouse1 = new delMouse(mouse1);
        delMouse2 = new delMouse(mouse2);


    }


    private void button1_Click(object sender, EventArgs e)
    {
        t1 = new Thread(new ThreadStart(delMouse1));
        t1.Start();

    }
    private void button2_Click(object sender, EventArgs e)
    {


        t2 = new Thread(new ThreadStart(delMouse2));
        t2.Start();

    }
    void mouse2()
    {
        if (this.mouseimg2.InvokeRequired)
        {
            this.Invoke(delMouse2);
        }
        else
        {
            int destinval2 = int.Parse(textBox2.Text);
            while (mouseimg2.Location.Y != destinval2)
            {
                if (mouseimg2.Location.Y == 250)
                    mouseimg2.Location = new Point(mouseimg2.Location.X, 15);
                if (mouseimg2.Location.Y < destinval2)
                    mouseimg2.Location = new Point(mouseimg2.Location.X, mouseimg2.Location.Y + 1);
                else
                    mouseimg2.Location = new Point(mouseimg2.Location.X, mouseimg2.Location.Y - 1);
            }
            LeftClick(mouseimg2.Location.X, mouseimg2.Location.Y);
        }
    }

    void mouse1()
    {
        if (this.mouseimg1.InvokeRequired)
        {
            this.Invoke(delMouse1);
        }
        else
        {
            int destinval1 = int.Parse(textBox1.Text);

            while (mouseimg1.Location.Y != destinval1)
            {

                if (mouseimg1.Location.Y < destinval1)
                    mouseimg1.Location = new Point(mouseimg1.Location.X, mouseimg1.Location.Y + 1);
                else
                    mouseimg1.Location = new Point(mouseimg1.Location.X, mouseimg1.Location.Y - 1);
            }
            LeftClick(mouseimg1.Location.X, mouseimg1.Location.Y);
        }
    }



           [DllImport("user32.dll")]
    static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

    [Flags]
    public enum MouseEventFlags
    {
        LEFTDOWN = 0x00000002,
        LEFTUP = 0x00000004,
        MIDDLEDOWN = 0x00000020,
        MIDDLEUP = 0x00000040,
        MOVE = 0x00000001,
        ABSOLUTE = 0x00008000,
        RIGHTDOWN = 0x00000008,
        RIGHTUP = 0x00000010
    }
    public static void LeftClick(int x, int y)
    {
        Cursor.Position = new System.Drawing.Point(x, y);
        mouse_event((int)(MouseEventFlags.LEFTDOWN), 0, 0, 0, 0);
        mouse_event((int)(MouseEventFlags.LEFTUP), 0, 0, 0, 0);
    }


}


截屏

4

1 回答 1

1

通常在 Windows 中,您不应该从创建它的线程以外的线程访问窗口。如您所见,WinForms 强制执行此规则。

我不太清楚你想做什么,但创建两个线程几乎肯定是错误的方法。

看起来有点像多线程但不是的行为通常使用计时器来完成,如果这对您有帮助的话。

于 2012-09-27T12:01:15.193 回答