Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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
  • 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…

578 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