I can't understand how to use SendMessage or PostMessage calls

前端 未结 5 824
南方客
南方客 2020-12-29 14:39

I need to simulate a keypress in a third party application. Let\'s say I have a C# application that needs to send an \"8\" to the Calculator application. I can\'t use the Se

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-29 15:04

    Because it is an Edit Child window inside the notepad window. You should send messages to the right child window. It is a working example in C:

    #include 
    #include 
    
    void main(void) {
        STARTUPINFO si;
        PROCESS_INFORMATION pi;
        HWND mainwnd,editwnd;
        char c;
        si.cb=sizeof(si);
        si.lpReserved=NULL;
        si.lpDesktop=NULL;
        si.lpTitle=NULL;
        si.dwFlags=0;
        si.cbReserved2=0;
        si.lpReserved2=NULL;
        if(!CreateProcess("c:\\windows\\notepad.exe",NULL,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi)) {
            printf("Failed to run app");
            return;
        }
        WaitForInputIdle(pi.hProcess,INFINITE);
        mainwnd=FindWindow(NULL,"Untitled - Notepad");
        if(!mainwnd) {
            printf("Main window not found");
            return;
        }
        editwnd=FindWindowEx(mainwnd,NULL,"Edit","");
        if(!editwnd) {
            printf("Edit window not found");
            return;
        }
        for(c='1';c<='9';c++) {
            PostMessage(editwnd,WM_CHAR,c,1);
            Sleep(100);
        }
    }
    

提交回复
热议问题