Control mouse programmatically

You may want to control the mouse programmatically for automation test. Basically, you can send Window Message to the target window. Use Spy++ to find out the Window messages you need to send.

As an example, start MS Paint and run the program below. It draws a picture for you.

 

HWND g_hFoundWindow = NULL;

void ShowHelp(const std::string& message){
    cout << "auto_mouse" << endl;
    cout << endl;
    cout << message << endl;
}

DWORD MakePosition(WORD x, WORD y){
    return ((DWORD)y << 16) | (DWORD)x;
}

BOOL CALLBACK FirstWindow(HWND hwnd, LPARAM lParam){
    cout << format("[FirstWindow] hWnd = 0x%08x") % hwnd << endl;
    g_hFoundWindow = hwnd;
    return FALSE;
}

int main(int argc, char** argv){
    try{
        // Find MS-Paint Window
        HWND hMSPaintMainWindw = ::FindWindowEx(NULL, NULL, "MSPaintApp", NULL);
        if(hMSPaintMainWindw == NULL){
            throw std::runtime_error("Failed to find Window with class name = MSPaintApp");
        }

        // Find the client window
        g_hFoundWindow = NULL;
        EnumChildWindows(hMSPaintMainWindw, FirstWindow, NULL);
        if(g_hFoundWindow == NULL){
            throw std::runtime_error("Failed to find child window of MS Paint");
        }
        HWND hChildWindow = g_hFoundWindow;

        // Find the window for the draw area
        g_hFoundWindow = NULL;
        EnumChildWindows(hChildWindow, FirstWindow, NULL);
        if(g_hFoundWindow == NULL){
            throw std::runtime_error("Failed to find draw area of MSPaint");
        }
        HWND hDrawArea = g_hFoundWindow;

        // Draw spiral.
        int px = 0;
        int py = 0;
        bool firstPoint = true;

        for(double i = 0; i < 1; i += 0.001){
            double rad = 10 * 2 * 3.14 * i;
            double x = cos(rad) * 100 * i;
            double y = sin(rad) * 100 * i;
            
            px = (int)x + 300;
            py = (int)y + 300;

            if(firstPoint){
                PostMessage(hDrawArea, WM_LBUTTONDOWN, MK_LBUTTON, MakePosition(px, py));
                firstPoint = false;
            }
            PostMessage(hDrawArea, WM_MOUSEMOVE, MK_LBUTTON, MakePosition(px, py));
            PostMessage(hDrawArea, WM_MOUSEMOVE, MK_LBUTTON, MakePosition(px, py));
            Sleep(10);
        }
        
        PostMessage(hDrawArea, WM_LBUTTONUP, MK_LBUTTON, MakePosition(px, py));
        
        return 0;
    }catch(std::exception& e){
        cout << e.what() << endl;
    }
    return 1;
}
image 
 
Advertisements

About Moto

Engineer who likes coding
This entry was posted in Tips. Bookmark the permalink.

2 Responses to Control mouse programmatically

  1. Peter says:

    very cool

  2. Pingback: [Help] C# Move mouse while left / right button pressed.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s