x7code 发表于 2013-1-3 22:04:01

api一日一练 第16天 FindWindowEx PostMessage 模拟点击


今天我们要实现的是对一个窗口上的按钮进行模拟点击

找子窗口
HWND FindWindowEx
(
HWND hwndParent,      // 窗口句柄
HWND hwndChildAfter,// 子窗口句柄
LPCTSTR lpszClass,    // 类名
LPCTSTR lpszWindow    // 窗口名
);

发送消息

PostMessageBOOL PostMessage
(
HWND hWnd,      // 窗口句柄
UINT Msg,       //消息类型
WPARAM wParam,// 指定附加的消息指定信息
LPARAM lParam   // 指定附加的消息指定信息
);

**** Hidden Message *****
成功的对一个窗口上的按钮进行模拟点击
提供测试程序 MFC


这节课的思考题是用 mouse_event 和SendMessage 各完成一次鼠标模拟点击


jiayuanfu 发表于 2013-1-4 00:28:40

继续学习。增长知识

hackbs 发表于 2013-1-4 10:56:37

了解下参数

Joewm 发表于 2013-1-4 13:46:58

正好学习下.

hackbs 发表于 2013-1-4 18:01:28

每秒模拟点击 按钮

mouse_event 的实现方法、// sdk13.cpp : Defines the entry point for the application.

//


#include "stdafx.h"
#include <tchar.h>

int APIENTRY WinMain(HINSTANCE hInstance,
                                       
                     HINSTANCE hPrevInstance,
                                       
                     LPSTR   lpCmdLine,
                                       
                     int       nCmdShow)
                                       
{
        //API一日一练 第16天。 xqmsyk.com支持小七论坛。
        //love 小七论坛    By:hackbs
    while(1)
        {
                HWND Hfile=FindWindow(NULL,"小七论坛");
                if (!Hfile)
                {
                        MessageBox(NULL,_T("未发现程序运行"),_T("温馨提示"),NULL);
                        return 0;
                }
                HWND Button=FindWindowEx(Hfile,NULL,"Button",NULL);
                if(!Button)
                {
                        MessageBox(NULL,"按钮没有被发现","温馨提示",NULL);
                        return 0;
                }
               
                //用GetWindowRect得到按钮的大小尺寸
                RECT rect;
                GetWindowRect(Button,&rect);
               
                //计算按钮中心坐标 用SetCursoPos把鼠标移到中心点。
                POINT center;   
                center.x = 0.5*(rect.left+rect.right);   
                center.y = 0.5*(rect.top+rect.bottom);   
                SetCursorPos(center.x, center.y);
               
               
               
               
               
               
                //鼠标在当前位置抬起
                mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
                mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
                //还原鼠标位置
                mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
               
                Sleep(1000);//过一秒 再执行后边的       
        }
        return 0;
}SendMessage的实现方法 、#include "stdafx.h"
#include <tchar.h>
void KeybdEvent (BYTE bKeyCode,DWORD dwFlags)
{
        keybd_event(bKeyCode, MapVirtualKey(bKeyCode, 0), dwFlags, 0);
}
int APIENTRY WinMain(HINSTANCE hInstance,
                                       
                     HINSTANCE hPrevInstance,
                                       
                     LPSTR   lpCmdLine,
                                       
                     int       nCmdShow)
{
        //API一日一练 第16天。 xqmsyk.com支持小七论坛。
        //love 小七论坛    By:hackbs
            while(1)
        {
                HWND Hfile=FindWindow(NULL,"小七论坛");
                if (!Hfile)
                {
                        MessageBox(NULL,_T("未发现程序运行"),_T("温馨提示"),NULL);
                        return 0;
                }
                HWND Button=FindWindowEx(Hfile,NULL,"Button",NULL);
                if(!Button)
                {
                        MessageBox(NULL,"按钮没有被发现","温馨提示",NULL);
                        return 0;
                }


//点击抬起。
SendMessage(Button,WM_LBUTTONDOWN,1,0x1f0028);
Sleep(50);
SendMessage(Button,WM_MOUSEMOVE,1,0x1f0028);
Sleep(50);
SendMessage(Button,WM_LBUTTONUP,0,0x1f0028);
   KeybdEvent(VK_LBUTTON,KEYEVENTF_KEYUP);
                Sleep(1000);
                }


        return 0;
}

君子坦蛋蛋 发表于 2013-1-4 19:24:11

惭愧,刚下班,作业还没交呢。等会补上。。。

君子坦蛋蛋 发表于 2013-1-4 22:13:31

额。。没时间了。发现我不会。。哎。。交白卷了。惭愧。。

yhnujmik 发表于 2013-1-6 10:54:32

来看看啊啊啊

thanwea 发表于 2013-1-7 18:03:37

这个很好 可以看看

星空一偶 发表于 2013-1-15 15:16:03

学习 学习 学习看源码
页: [1] 2 3 4
查看完整版本: api一日一练 第16天 FindWindowEx PostMessage 模拟点击