M.C
- #include <windows.h>
- #include <WinUser.h>
- #include <stdio.h>
- #include <stdlib.h>
- int main(){
- BOOL opened; // 计算器是否已经成功打开的标志
- STARTUPINFO si ={sizeof(si)}; // 计算器启动信息
- PROCESS_INFORMATION pi; // 计算器进程信息
- HWND h1 = NULL; // 计算器窗口指针
- int i=0;
- BOOL postMsgResult;
- // 打开计算器
- opened = CreateProcess(NULL, "calc.exe", NULL, NULL, FALSE, 0, NULL,NULL, &si, &pi);
- if ( !opened ) {
- printf("无法打开计算器,错误码 = %d\n", GetLastError());
- return;
- }
- // 找到该窗口
- while ( h1 == NULL && i < 3 ){
- h1 = FindWindow(NULL, "计算器");
- if ( h1 == NULL ) {
- i ++;
- Sleep(1000);
- }
- }
- if ( h1 == NULL ) {
- printf("找不到已经打开的计算器,错误码 = %d\n", GetLastError());
- return;
- }
- for(i = 0; i < 3; i++){
- // keybd_event(0x39,0,KEYEVENTF_KEYUP,0); // FIXME
- // msgResult = SendMessage(h1,WM_CHAR,'4',0); // FIXME
- SetLastError(0);
- postMsgResult = PostMessage(h1,WM_CHAR,'1'+i,0);
- if ( i < 2 ){
- PostMessage(h1,WM_CHAR,'+',0);
- } else {
- PostMessage(h1,WM_CHAR,'=',0);
- }
- printf("result=%d, error=%d\n", postMsgResult, GetLastError());
- Sleep(1000);
- }
- system("Pause");
- return 0;
- }