Link to home
Start Free TrialLog in
Avatar of MugWumpBW
MugWumpBW

asked on

Sending mouse events to another application

I have written a VC++ 6.0 application which monitors and interacts with third party Windows applications.  Specifically, it screen scrapes the graphics in the application window and, based on what it finds, sends mouse events to check boxes and push buttons to drive the application.  Everything works fine for the first application I tried, but the second application doesn't seem to recognize and process the mouse clicks.  The screen scraping works for the second application, but not the mouse clicks.  

Is there some other way this application could get and process mouse clicks other than the normal message processing?  

If so, is there any way I can simulate that process in my program?  

Are there other messages I could send which would work?

Below is the pertinent code used to accomplish this (commented lines were tried but did not fix the problem).

// Get a handle to the window based on the window class and name.
HWND  hWindow = FindWindow( className, windowName );
CWnd* pWindow = CWnd::FromHandle( hWindow );

if ( ( pWindow != NULL ) && ( IsWindow( pWindow->m_hWnd ) == TRUE ) )
{
    // ...  Screen scrape the window to determine the button location

    WPARAM wParam = MK_LBUTTON;
    LPARAM lParam = buttonX + 65536 * buttonY;

    // pWindow->SetForegroundWindow();
    // pWindow->PostMessage( WM_MOUSEMOVE, 0, lParam );
    pWindow->PostMessage( WM_LBUTTONDOWN, wParam, lParam );
    pWindow->PostMessage( WM_LBUTTONUP, wParam, lParam );
}
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MugWumpBW
MugWumpBW

ASKER

I tried to add the code to use SendInput() by declaring a variable of type INPUT and including the Windows.h header file, but when I attempt to compile I get:

       error C2065: 'INPUT' : undeclared identifier
       error C2065: 'SendInput' : undeclared identifier

This struct and function are declared in Winuser.h which is included in Windows.h.  The application is a standard MFC/SDI application and I haven't set any unusual options other than what the wizard generated.  I assume it finds the header file since there is no error there, but for some reason is not finding the typedef for INPUT.  Is there some order dependency or compile flag I need to set here?
I resolved the above compile error and changed the original code to be:

INPUT input[ 2 ];

input[ 0 ].type           = INPUT_MOUSE;
input[ 0 ].mi.dx          = ( buttonX * 65535 ) / 1280;
input[ 0 ].mi.dy          = ( buttonY * 65535 ) / 1024;
input[ 0 ].mi.mouseData   = 0;
input[ 0 ].mi.dwFlags     = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN;
input[ 0 ].mi.time        = 0;
input[ 0 ].mi.dwExtraInfo = NULL;

input[ 1 ].type           = INPUT_MOUSE;
input[ 1 ].mi.dx          = ( buttonX * 65535 ) / 1280;   // Assume 1280 x 1024 monitor
input[ 1 ].mi.dy          = ( buttonY * 65535 ) / 1024;
input[ 1 ].mi.mouseData   = 0;
input[ 1 ].mi.dwFlags     = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP;
input[ 1 ].mi.time        = 0;
input[ 1 ].mi.dwExtraInfo = NULL;

::SendInput( 2, input, sizeof( INPUT ) );

However, now the application that used to work no longer works.  Plus, when I move the mouse over the button in my application which is used to send the mouse click message, the button flickers as if hovering causes some continuous series of events.
It seems the missing link was a call to SetCursorPos() to move the mouse cursor to the click point prior to calling SendInput().  To make the whole thing transparent to the user, the cursor may then be returned to the original position after calling SendInput()