Sunday, May 1, 2011

SendMessage example win32

This is a SendMessage example to control window of another application.
The sequence you follow is simple.
First, find out the target window handle by using FindWindow() function.
Second, send desired message to the target window using SendMessage() or PostMessage() function.

Example code to control "calculator" window.

void    FindWindowAndSendMessage(void)
{
    HWND        hWnd;
    LRESULT        result;
    UINT        uMsg;
    WPARAM        wParam;
    LPARAM        lParam;
    int            i, j;
    WINDOWPOS    winPos;


    hWnd = FindWindow(NULL, TEXT("calculator"));
    if( hWnd )
    {
        // It works, I can handle to another window and control them.
        //


        // Change windows text.
        //
        SendMessage(hWnd, WM_SETTEXT, NULL, (LPARAM)TEXT("my own calculator"));

        // Close
        //
        uMsg = WM_CLOSE;
        SendMessage(hWnd, uMsg, NULL, NULL);


    }
    return;
}