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

    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 );
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

You'd usually use 'mouse_event()' (or 'SendInput()') for that purpose. See e.g. John Robbins's "Bugslayer coulumn where he introduces that technique to automate testing: ("Tester Utility, Take 3: Adding Mouse Recording and Playback")

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MugWumpBWAuthor Commented:
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?
MugWumpBWAuthor Commented:
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.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.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.
MugWumpBWAuthor Commented:
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()
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.