发布网友 发布时间:2022-04-21 11:52
共1个回答
热心网友 时间:2023-11-10 10:15
先用FindWindow函数找到程序句柄,再用findWindowEx扭句柄,然后调用sendmessage函数发送消息即可\x0d\x0a\x0d\x0aDeclareFunctionSendMessageLib“user32"Alias“SendMessageA"(ByValhwnd\x0d\x0aAsLong,ByValwMsgAsLong,ByValwParamAsLong,lParamAsAny)AsLong\x0d\x0aSendMessage函数的hwnd参数为接收消息的窗口或控件的hwnd句柄。wMsg参数指定具体的消息值。要发送鼠标左键按下和释放的消息,wMsg的取值分别为WM_LBUTTONDOWN和WM_LBUTTONUP,这两个常量定义如下:\x0d\x0a\x0d\x0aConstWM_LBUTTONDOWN=&H201\x0d\x0aConstWM_LBUTTONUP=&H202\x0d\x0a把这两个消息发送给一个按钮,按钮就会像真的被鼠标左键单击了一样。SendMessage函数中的后两个参数在本例中可以不理,简单置0即可。\x0d\x0a还有一点细节需注意。如果连续给按钮发送一对WM_LBUTTONDOWN和WM_LBUTTONUP消息,Windows会来不及进行一些必要的系统操作,这样在视觉上就看不出按钮被按下后又放开的效果,好像按钮没有按动。但按钮的Click事件处理过程被激活执行表明按钮确实被按过。为了达到视觉上的完美效果,我们不妨在WM_LBUTTONDOWN和WM_LBUTTONUP两个消息之间插入一段短短的延时,比如说200毫秒,在这段延时期间,把处理权交给Windows,这样Windows就有时间显示按钮被按下的效果了。插入延时的办法有很多,可以加入一段空循环或利用计时器控件,这里再介绍一个API函数GetTickCount,该函数获取自Windows启动至被调用时所经过的毫秒数。利用这个函数控制延时,不仅精确,而且节省资源。\x0d\x0aGetTickCount函数的接口声明如下:\x0d\x0aDeclareFunctionGetTickCountLib“kernel32"()AsLong\x0d\x0a\x0d\x0a====================CODE:\x0d\x0aPrivateeclareFunctionGetTickCountLib“kernel32"()AsLong\x0d\x0a\x0d\x0aPrivateDeclareFunctionSendMessageLib"user32"Alias"SendMessageA"(ByValhwndAsLong,ByValwMsgAsLong,ByValwParamAsInteger,ByVallParamAsAny)AsLong\x0d\x0aPrivateDeclareFunctionFindWindowExLib"user32"Alias"FindWindowExA"(ByValhWnd1AsLong,ByValhWnd2AsLong,ByVallpsz1AsString,ByVallpsz2AsString)AsLong\x0d\x0aPrivateDeclareFunctionFindWindowLib"user32"Alias"FindWindowA"(ByVallpClassNameAsString,ByVallpWindowNameAsString)AsLong\x0d\x0aDimtWndAsLong,bWndAsLong,ncWndAsLong\x0d\x0a\x0d\x0aConstWM_LBUTTONDOWN=&H201\x0d\x0aConstWM_LBUTTONUP=&H202\x0d\x0a\x0d\x0aPrivateSubCommand1_Click()\x0d\x0atWnd=FindWindow("Shell_TrayWnd",vbNullString)\x0d\x0abWnd=FindWindowEx(tWnd,ByVal0&,"BUTTON",vbNullString)\x0d\x0aSendMessagebWnd,WM_LBUTTONDOWN,0,ByValO&\x0d\x0aGetTickCount\x0d\x0aSendMessagebWnd,WM_LBUTTONUP,0,ByValO&\x0d\x0aEndSub这个例子展示点“开始”按扭