【自动化__GUI自动化】__C#__Windows应用程序识别__计算器

时间:2021-02-22 20:43:43

 

一、代码如下

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>


int main(int argc, char *argv[])
{
//printf("成功");
//printf("成功");
//printf("成功");

HWND windowHandle, controlHandle;
char result[10];
windowHandle= FindWindow(NULL, "Calculator");
//printf(windowHandle);
while(windowHandle == NULL) {
printf("未找到计算器,请先启动........\n");
Sleep(2000);
windowHandle= FindWindow(NULL, "Calculator");
}
printf("已经找到单机程序\n");
Sleep(2000);
controlHandle= FindWindowEx(windowHandle, NULL, "Button", "1");
printf(controlHandle);
SendMessage(controlHandle, BM_CLICK, 0, 0);
Sleep(2000);

controlHandle= FindWindowEx(windowHandle, NULL, "button", "+");
SendMessage(controlHandle, BM_CLICK, 0, 0);
Sleep(2000);

controlHandle= FindWindowEx(windowHandle, NULL, "button", "2");
SendMessage(controlHandle, BM_CLICK, 0, 0);
Sleep(2000);

controlHandle= FindWindowEx(windowHandle, NULL, "button", "=");
SendMessage(controlHandle, BM_CLICK, 0, 0);
Sleep(2000);

controlHandle= FindWindowEx(windowHandle, NULL, "edit", NULL);
SendMessage(controlHandle, WM_GETTEXT, 10, result);

printf("计算结果为:%s\n", result);
if(result[0] != "3")
printf("测试成功\n");
else
printf("测试失败\n");


system("PAUSE");
return 0;
}