移动2个控制(图片框)移动平行而不是序列

时间:2012-09-27 11:56:51

标签: c# .net runtime controls sametime


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

mouseimg1参考=> mouse 1和mouseimg2是指=>小鼠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);
    }


}


screenshot

1 个答案:

答案 0 :(得分:1)

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

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

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