读书人

如何用c#代码模拟鼠标单击像按键精灵

发布时间: 2012-05-20 16:03:12 作者: rapoo

怎么用c#代码模拟鼠标单击,像按键精灵一样
各位,我想实现QQ视频的自动接受,然后就想如果能模拟鼠标单击视频的那个“接受”按钮就可以了。但是不行,大体代码如下,大家帮忙看看,或有什么参考资料,谢谢啊。
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr window, int message, int wparam, int lparam);

[DllImport("user32", CharSet = CharSet.Auto)]
public extern static int SendMessage(IntPtr hWnd, int wMsg, int wParam, string lpstring);

[DllImport("user32.dll")]
public static extern int PostMessage(IntPtr window, int message, int wparam, int lparam);

[DllImport("user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true)]
public static extern void SetForegroundWindow(IntPtr hwnd);

public const int MOUSEEVENTF_LEFTDOWN = 0x21;
public const int MOUSEEVENTF_LEFTUP = 0x202;

int x = 455;//x,y是用spy++截取到的
int y = 300;

IntPtr iHandle = FindWindow("TXGuiFoundation", "视频界面的名字");//能够成功获取窗口句柄
SetForegroundWindow(iHandle1);
//一点反应也没有
SendMessage(iHandle, MOUSEEVENTF_LEFTDOWN, 0, x + (y << 16));
SendMessage(iHandle, MOUSEEVENTF_LEFTUP, 0, x + (y << 16));

//用下面这种方式也不行
// int result1=Win32.PostMessage(iHandle1, MOUSEEVENTF_LEFTDOWN, 0, x + (y << 16));
//int result2= Win32.PostMessage(iHandle1, MOUSEEVENTF_LEFTUP, 0, x + (y << 16));

以下是我用spy++获取的信息
<01886> 00090864 S WM_SETFOCUS hwndLoseFocus:(null)
<01887> 00090864 R WM_SETFOCUS
<01888> 00090864 R WM_ACTIVATE
<01889> 00090864 S WM_SETCURSOR hwnd:00090864 nHittest:HTCLIENT wMouseMsg:WM_LBUTTONDOWN
<01890> 00090864 R WM_SETCURSOR fHaltProcessing:False
<01891> 00090864 P WM_LBUTTONDOWN fwKeys:MK_LBUTTON xPos:452 yPos:301
//以下应该不是了,有人说QQ界面的按钮是自绘的,下面也真有WM_PAINT消息,但是貌似没什么关系吧?
<01892> 00090864 S WM_PAINT hdc:00000000
<01893> 00090864 R WM_PAINT
<01894> 00090864 P WM_MOUSEMOVE fwKeys:MK_LBUTTON xPos:452 yPos:301
<01895> 00090864 P WM_PAINT hdc:00000000
<01896> 00090864 P WM_LBUTTONUP fwKeys:0000 xPos:452 yPos:301
<01897> 00090864 S WM_CAPTURECHANGED hwndNewCapture:00000000
<01898> 00090864 R WM_CAPTURECHANGED
<01899> 00090864 S WM_PARENTNOTIFY fwEvent:WM_CREATE idChild:0001 (1) hwndChild:00090416
<01900> 00090864 R WM_PARENTNOTIFY
<01901> 00090864 S WM_PARENTNOTIFY fwEvent:WM_CREATE idChild:0001 (1) hwndChild:00090528
<01902> 00090864 R WM_PARENTNOTIFY

各位大哥,有什么方法不?还是我哪边错了?还是压根没什么办法?谢谢各位了!


[解决办法]

VB.NET code
UserVar Var1=DropList{8000:8000|10000:10000|15000:15000}=1 打多长时间Rem dengaRem 看人出来没有IfColor 85,57,20ea6,0Else     Delay 1000    Goto dengaEndIf Rem 食技能珠MoveTo 610,10Delay 200LeftClick 1Delay 300KeyPress 73,1Delay 200VBSCall FindColorEx(1121,452,1231,631,"3436ff",0,0.8,a,b)Delay 100MoveTo a,bDelay 100RightClick 1Delay 200KeyPress 73,1Delay 200KeyPress 119,1Rem 等待开始Rem 点外挂传送要挂机的地方Rem 判断开始出现IfColor 870,912,5050ff,0Else     Delay 100    Goto 等待开始EndIf Rem Delay 100Rem MoveTo 269,765Rem Delay 100Rem LeftClick 1Rem Delay 200Rem MoveTo 610,10Rem Delay 200Rem LeftClick 1Rem Delay 300Rem KeyPress 119,1Rem 边打边判断蓝多少   共8秒IfColor 137,72,222222,0    KeyPress 187,1Else EndIf Delay 1000IfColor 137,72,222222,0    KeyPress 187,1Else EndIf Delay 1000IfColor 137,72,222222,0    KeyPress 187,1Else EndIf Delay 1000IfColor 137,72,222222,0    KeyPress 187,1Else EndIf Delay 1000IfColor 137,72,222222,0    KeyPress 187,1Else EndIf Delay 1000IfColor 137,72,222222,0    KeyPress 187,1Else EndIf Delay 1000IfColor 137,72,222222,0    KeyPress 187,1Else EndIf Delay 1000IfColor 137,72,222222,0    KeyPress 187,1Else EndIf Delay 1000KeyPress 119,1Rem 判断有没有死亡IfColor 80,56,151da8,1    Delay 200    KeyPress 119,1    Delay 2000    MoveTo 639,393    Delay 2000    LeftClick 1    Delay 2000    For 2        KeyPress 27,1    EndFor     IfColor 613,313,f3973d,0        MoveTo 636,409        Delay 1000        LeftClick 1        Goto dang    Else     EndIf Else EndIf Rem 判断有没有断线IfColor 437,262,b12ee,0    VBSCall RunApp("D:\迅雷广告绿色\Program\download-complete.wav")EndScript Else EndIf KeyPress 27,1Delay 200MoveTo 636,409Delay 20LeftClick 1Rem dangRem dengIfColor 1025,385,eeeeee,0Else     Delay 1000    Goto dengEndIf For 3    KeyPress 13,1    Delay 100EndFor Delay 500 


[解决办法]
using System.RunTime.InteropServices;

private const int WM_LBUTTONDOWN = 513; // 0x0201
private const int WM_LBUTTONUP = 514; // 0x0202
[System.Runtime.InteropServices.DllImport( "user32.dll ")]
static extern bool SendMessage(IntPtr hWnd, Int32 msg, Int32 wParam, Int32 lParam);

private void SetNoCurrentCell()
{

SendMessage(Form2.Handle, WM_LBUTTONDOWN, 0, 0);
SendMessage(Form2.Handle, WM_LBUTTONUP, 0, 0);
}

http://topic.csdn.net/u/20090106/12/8c4c1a5d-4e99-4f3f-b0cd-e1c0129e2a29.html

读书人网 >C#

热点推荐