Sending mouse events to another application

Posted on 2005-03-09
Medium Priority
Last Modified: 2013-12-03
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 );
Question by:MugWumpBW
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 86

Accepted Solution

jkr earned 1500 total points
ID: 13498687
>>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: http://msdn.microsoft.com/msdnmag/issues/02/03/Bugslayer/ ("Tester Utility, Take 3: Adding Mouse Recording and Playback")

Author Comment

ID: 13501693
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?

Author Comment

ID: 13513753
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.

Author Comment

ID: 13565170
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()

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question