模拟对另一进程的窗口按钮的点击

来源:互联网 发布:3d游戏编程入门经典 编辑:程序博客网 时间:2024/06/06 06:39

 

1、

在自动化测试中经常要模拟窗口按钮的点击。

参考文章:http://blog.csdn.net/blackboyofsnp/article/details/3372719

有时我们需要这么做, 手动模拟按钮按下的事件, 让程序做出与按钮真的按下时一样的响应.

   设按钮ID为ID_BTN_OK, 当前Dialog窗口.
   实际上系统并不能区分按钮是真的按下了(如用鼠标点击), 还是一个我们自己用代码模拟出的一种"假象".
它所需要知道的只是哪个窗口(按钮也是一种窗口)发生了哪一种事件, 然后在消息循环中处理它. 系统怎么才
能知道这些呢? 当然靠的是消息(message), 我们只需按照Windows或者MFC的标准格式把相应的信息传给系统,
系统就会"上当"了.
   向系统传递消息可以用SendMessage或PostMessage(可能还有其他很多函数哦), 但SendMessage执行后系统
会一直等待, 直到要发送的消息被处理掉. 而PostMessage可不管那么多, 发送消息后立即返回程序流程. 当
按钮按下的响应函数中有很大一个循环时, 用SendMessage会出现许多问题, 尤其是要在程序初始化阶段模拟时,
会导致窗体无法完成初始化. 所以我们用PostMessage(). 它的原型为:
BOOL PostMessage(          HWND hWnd,    UINT Msg,    WPARAM wParam,     LPARAM lParam);


这样写;  

PostMessage(WM_COMMAND, MAKEWPARAM(ID_BTN_OK, BN_CLICKED), NULL);

这里, WM_COMMAND是要发送的消息, MAKEWPARAM宏是为了组成一个WPARAM,WM_COMMAND消息的WPARAM的低字为控件ID,高字为识别码, 最后一个参数LPARAM可为NULL.相关定义可查看MSDN.

  这样我们就把必需的信息格式化好发送给系统了. 当系统在消息循环中收到该消息时, 就知道哦, 你要引发控件ID_BTN_OK
的事件BN_CLICKED, 好的我帮你处理. 于是我们就惊喜地看到按钮看起来真的按下去了, 并执行了和真正按下去时一样的代码.

 

看评论说不管用,我又试验了一下。新建一个对话框工程,在对话框上添加一个按钮,ID为IDC_BTN_TEST,单击它,为它添加ON_BN_CLICKED消息响应函数:

void CtestDlg::OnBnClickedBtnTest(){    AfxMessageBox(_T("OK"));}

然后在对话框的OnInitDialog()函数的return TRUE前加上:

PostMessage(WM_COMMAND, MAKEWPARAM(IDC_BTN_TEST, BN_CLICKED), NULL);

好了,再运行程序,会弹出个消息框 “OK”,说明模拟正确。

 

 2、 C#调用迅雷的时候 自动模拟点击”下载按钮” 关闭弹出窗口

网上 关于 “不弹出《建立任务》的对话框的方法 在迅雷5.9貌似不适用了” 那么我改了一下网上的“监听方式”来进行模拟点击

#region Dll Import 需要导入的api 声明。         [DllImport("User32.dll", EntryPoint = "FindWindow")]        private static extern IntPtr FindWindow(string lpClassName,      string lpWindowName);         [DllImport("user32.dll", EntryPoint = "FindWindowEx")]        private static extern IntPtr FindWindowEx(IntPtr hwndParent,      IntPtr hwndChildAfter, string lpszClass, string lpszWindow);         [DllImport("User32.dll", EntryPoint = "SendMessage")]        private static extern int SendMessage(IntPtr hWnd,      int Msg, IntPtr wParam, string lParam);        const int WM_GETTEXT = 0x000D;        const int WM_SETTEXT = 0x000C;        const int WM_CLICK = 0x00F5;        #endregion        //关消息的方法        private void ClosePopWindow(object obj)        {            //这些用spy++可以看到            string lpszParentClass = "#32770"; //整个窗口的类名            string lpszParentWindow = "建立新的下载任务"; //窗口标题            string lpszClass_Submit = "Button"; //需要查找的Button的类名            string lpszName_Submit = "立即下载"; //需要查找的Button的标题                       IntPtr ParenthWnd = new IntPtr(0);            IntPtr EdithWnd = new IntPtr(0);            int i = 0;            while (true)            {                //查到窗体,得到整个窗体                ParenthWnd = FindWindow(lpszParentClass, lpszParentWindow);                //判断这个窗体是否有效                if (!ParenthWnd.Equals(IntPtr.Zero))                {                     //得到第一级子窗口                    EdithWnd = FindWindowEx(ParenthWnd,                        new IntPtr(0), "#32770", "");                    //Console.WriteLine("第一级-"+EdithWnd.ToString());                    //得到Button这个子窗体,并触发它的Click事件                    EdithWnd = FindWindowEx(EdithWnd,                        new IntPtr(0), lpszClass_Submit, lpszName_Submit);                    //Console.WriteLine("第二级-" + EdithWnd.ToString());                    if (!EdithWnd.Equals(IntPtr.Zero))                    {                        SendMessage(EdithWnd, WM_CLICK, (IntPtr)0, "0");                    }                    return;                }                Thread.Sleep(1000);                i++;                //  Console.WriteLine("第"+i.ToString()+"次检查"); 5秒都没显示出来就推出循环                if (i > 15)                {                    //break;                }            }        }  //需要导入如下类库using System.Runtime.InteropServices;using System.Threading;using Microsoft.Win32; //在迅雷提交前添加一个方法ThreadPool.QueueUserWorkItem(new WaitCallback(ClosePopWindow));


 3、

#include <iostream>#include <fstream>#include <math.h>#include <cctype>#include <string>#include <windows.h>using namespace std;int main(){    //cout << "Hello world!" << endl;    HWND hwnd = FindWindow( 0, "文件窗口"  );    //HWND hWnd2 =  GetDlgItem( hwnd, 1001);    char* strs = new char[ 255 ];    HWND hWnd2 = ::FindWindowEx(hwnd,NULL,"Button",NULL);    while ( hWnd2 )    {        GetWindowText( hWnd2, strs, 255 );        cout << strs << endl;       /* if ( strcasecmp( strs, "确定" ) == 0 ) {            break;        }*/        hWnd2 = FindWindowEx( hwnd, hWnd2, "Button", NULL );    }    UINT nCtrlID = ::GetDlgCtrlID(hWnd2);    ::PostMessage(hWnd2, WM_COMMAND, (WPARAM)(BN_CLICKED << 16 | nCtrlID), (LPARAM)hWnd2);    ::PostMessage(hWnd2,WM_MOUSEMOVE, MK_LBUTTON, MAKELONG(0,0) );    ::PostMessage(hWnd2,WM_LBUTTONDOWN,MK_LBUTTON,MAKELPARAM(0,0));    ::PostMessage(hWnd2,WM_LBUTTONUP,MK_LBUTTON,MAKELPARAM(0,0));    return 0;}


 

4、往编辑框中写入文件(可实现)

SetWindowText

原创粉丝点击