我利用mouse_event在 timer1_Tick事件里来模拟鼠标移动和单击操作。主要代码如下:private void btnStart_Click(object sender, EventArgs e)
{
NativeRECT rect;
ptrTaskbar = FindWindow(null, "使用状态栏");
if (ptrTaskbar == IntPtr.Zero)
{
MessageBox.Show("No taskbar found.");
return;
}
GetWindowRect(new HandleRef(this, ptrTaskbar), out rect);
endPosition.X = (rect.left + rect.right) / 2;
endPosition.Y = (rect.top + rect.bottom) / 2;
//将鼠标移动到窗体中间
SetCursorPos(endPosition.X, endPosition.Y);
p = new Point();
p.X = endPosition.X;
p.Y = endPosition.Y;
//激活当前窗体
SetForegroundWindow(ptrTaskbar);
timer1.Start();
label1.Text = "正在工作……";
btnStart.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
//将鼠标移动到窗体中间
SetCursorPos(endPosition.X, endPosition.Y);
mouse_event(MouseEventFlag.Move, -90, -101, 0, UIntPtr.Zero);
System.Threading.Thread.Sleep(1000);
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.Move, -74, 35, 0, UIntPtr.Zero);
System.Threading.Thread.Sleep(2000);
mouse_event(MouseEventFlag.LeftDown, 0,0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.Move, 0, -20, 0, UIntPtr.Zero);
System.Threading.Thread.Sleep(1000);
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
//System.Threading.Thread.Sleep(2000);
//将鼠标移动到窗体中间
//SetCursorPos(endPosition.X, endPosition.Y);
//mouse_event(MouseEventFlag.Move, -240, -204, 0, UIntPtr.Zero);
}
Point p;
IntPtr ptrTaskbar;
NativeRECT rect;被模拟的图形界面如下:奇怪的是当程序第一次进入timer_tick时,一切模拟正常。但是当今后进入timer_tick后鼠标就被移动到窗体之外了。请问是什么原因?PS:"使用状态栏"窗体模拟程序可以 点此下载
{
NativeRECT rect;
ptrTaskbar = FindWindow(null, "使用状态栏");
if (ptrTaskbar == IntPtr.Zero)
{
MessageBox.Show("No taskbar found.");
return;
}
GetWindowRect(new HandleRef(this, ptrTaskbar), out rect);
endPosition.X = (rect.left + rect.right) / 2;
endPosition.Y = (rect.top + rect.bottom) / 2;
//将鼠标移动到窗体中间
SetCursorPos(endPosition.X, endPosition.Y);
p = new Point();
p.X = endPosition.X;
p.Y = endPosition.Y;
//激活当前窗体
SetForegroundWindow(ptrTaskbar);
timer1.Start();
label1.Text = "正在工作……";
btnStart.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
//将鼠标移动到窗体中间
SetCursorPos(endPosition.X, endPosition.Y);
mouse_event(MouseEventFlag.Move, -90, -101, 0, UIntPtr.Zero);
System.Threading.Thread.Sleep(1000);
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.Move, -74, 35, 0, UIntPtr.Zero);
System.Threading.Thread.Sleep(2000);
mouse_event(MouseEventFlag.LeftDown, 0,0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.Move, 0, -20, 0, UIntPtr.Zero);
System.Threading.Thread.Sleep(1000);
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
//System.Threading.Thread.Sleep(2000);
//将鼠标移动到窗体中间
//SetCursorPos(endPosition.X, endPosition.Y);
//mouse_event(MouseEventFlag.Move, -240, -204, 0, UIntPtr.Zero);
}
Point p;
IntPtr ptrTaskbar;
NativeRECT rect;被模拟的图形界面如下:奇怪的是当程序第一次进入timer_tick时,一切模拟正常。但是当今后进入timer_tick后鼠标就被移动到窗体之外了。请问是什么原因?PS:"使用状态栏"窗体模拟程序可以 点此下载
没人回 都还想接分啊!
好在 部分问题已经解决了。
-----------
还有一个新问题:
鼠标移动问题是解决了。
可是在模拟鼠标点击游戏里角色的地方卡住了,本人先后使用了mouse_event, SendInput,SendMessage函数,奇怪的是在普通的WinForm里都可以正常模拟单击,但是在游戏窗体里却不行。Google了N圈也没有找到解决方法。还请DX指点。
还可能游戏占用的内存比WINFORM大,在线程处就卡住了。
我以前写过一个和你这个基本类似,但点的不是游戏,而是一套软件。没有出现卡住的问题
------
这个是什么意思?
和占用内存有什么联系?