0
兩個虛擬鼠標圖像任務是移動水平移動並單擊目標點。在下面的代碼中順序運行(逐個)。 我想編輯此代碼以並行工作(兩個圖像在同一時間工作)。移動2個控件(picturebox)移動並行而不是序列
mouseimg1指的是=>鼠標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);
}
}
[Cross-thread operation not valid]的可能重複(http://stackoverflow.com/questions/5037470/cross-thread-operation-not-valid) –
好的例外解決了。但我有問題中描述的其他問題:(任何幫助? –