Sign in to follow this  
archr926

Sending mouseclicks with sendmessage()

Recommended Posts

I found this code a while back, that sends strings to notepad: #include <windows.h> #include <iostream> #include <string> using namespace std; void main () { HWND notepad_window = FindWindow("Notepad", NULL); HWND textbox_window = FindWindowEx(notepad_window, NULL, "Edit", NULL); if (!textbox_window) { printf("Could not find Notepad\n"); return; } string my_string; cout << "Enter a word to send to Notepad: "; cin >> my_string; for (int i = 0; i < my_string.size(); i++) SendMessage(textbox_window, WM_CHAR, (int)my_string[i], 0x80000000); } And was wondering how to send mouseclicks through sendmessage

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You do it the same way.

Instead of sending the WM_CHAR you send WM_LBUTTONDOWN/WM_LBUTTONUP.

You'll also need to form the WPARAM and LPARAM values correctly. Check out http://msdn.microsoft.com/.

But I'm curious why you want to do that? Clicking in notepad isn't very interesting :)

Share this post


Link to post
Share on other sites
Actually, I was going to do it with minesweeper, unfortunately, when I change the references to notepad, the program can't find the window, even though it's running.

Aditionally, what would the lparam and wparams be?

Share this post


Link to post
Share on other sites
If you use Spy++ you can find out what window to send teh messages to. That would be my guess as to why you can't find the window. It needs to go to a subwindow of the app.

As for the params you should really have googled this but ...
msdn.microsoft.com/.../userinput/mouseinput/ mouseinputreference/mouseinputmessages/wm_lbuttondown.asp

Share this post


Link to post
Share on other sites
Everything is working correctly except for two things:

I still can't find windows other than notepad.

The Lparam will only let me store the x position.

Share this post


Link to post
Share on other sites
To find other windows replace the notepad text with the titlebar name of the app you want to find.

As for the LParam check out the LOWORD and HIWORD macros.

Cheers
Chris

Share this post


Link to post
Share on other sites
I feel very close to completing this, except for that annoying minesweeper window. Can someone look over this and tell me why it's not working?

NOTE: It works if you replace "Minesweeper" with "Notepad"

EDIT: Is there a code tag on this board? I don't like having the code distorted.


#include <windows.h>
#include <iostream>
#include <string>
#include <conio.h>
#include <wincon.h>

using namespace std;

main () {
int bx;
bx=100;
int by;
by=100;
HWND send_window = FindWindow("Minesweeper",NULL);
HWND click_window = FindWindowEx(send_window, NULL, NULL, NULL);
cout<<"Send click to what x coordinate? "<<endl;
cin>>bx;
cout<<"Send click to what y coordinate? "<<endl;
cin>>by;
if (!click_window) {
printf("Could not find window\n");
getch();
return 0;
}
SendMessage(click_window, WM_LBUTTONDOWN, MK_LBUTTON, LPARAM MAKELPARAM(bx, by) );
getch();
}

Share this post


Link to post
Share on other sites
Minesweeper doesn't actually have any child windows, only the main program window (and a few helper dialogs such as "about", to be exact). See the FindWindowEx function reference on MSDN for more info as to why this is a problem in your code [smile]

Kind regards,
-Nik

EDIT: I'm late...

Share this post


Link to post
Share on other sites
The minesweeper window may not have an actual window name of "Minesweeper". You can find its window name / class name with Spy++.

use the source tag.

//edit: I'm later.

Share this post


Link to post
Share on other sites
Try PostMessage. I've had inexplicable problems with SendMessage before, such as the messages not being received.

Share this post


Link to post
Share on other sites
I think Spy++ comes with visual studio.

Inspect32 will give you similar results to Spy++.

http://www.microsoft.com/downloads/details.aspx?FamilyId=3755582A-A707-460A-BF21-1373316E13F0&displaylang=en

Hope that helps,

Share this post


Link to post
Share on other sites
In my experience, a more reliable method of sending mouse clicks is SendInput.

You use it something like this, calling SetCursorPos(int x,int y) to move the mouse to the correct location. Then calling SendMouseClickDown() waiting for a little bit, then SendMouseClickUp().


void SendMouseClickDown() {
INPUT input;
input.type = INPUT_MOUSE;
input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
input.mi.time = 0;
SendInput(1, &input, sizeof(INPUT));
}

void SendMouseClickUp() {
INPUT input;
input.type = INPUT_MOUSE;
input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
input.mi.time = 0;
SendInput(1, &input, sizeof(INPUT));
}

Share this post


Link to post
Share on other sites
I don't see any reference to SendMouseClickUp/Down in the MSDN Library, what headers does it need?

In fact, google doesnt come up with anything for sendmouseclickup/down, in any language.

I think it would be easier, not to send the mouseclick to a specific window, but to make the whole OS think there was a click. (like SetCursorPos)

[Edited by - archr926 on November 14, 2004 7:01:30 AM]

Share this post


Link to post
Share on other sites
Well, you don't see any references to SendMouseClickUp/Down because they are functions I made :). What you should look for is the SendInput() function. And that is exactly what SendInput() does, it inserts a mouse event into the global event queue. It doesn't rely on the application handling WM_MOUSEDOWN/etc.

Share this post


Link to post
Share on other sites
Thanks, I'm all set now, here's my code:

#include <iostream.h>
#include <windows.h>
#include <conio.h>


main () {

int mx;
int my;

int a;
while (1==1) {

a=0;

while (a==0) {

cout<<"Move cursor to (x):"<<endl;
cin>>mx;
cout<<"Move cursor to (y):"<<endl;
cin>>my;

SetCursorPos(mx,my);

cout<<"Correct position (1), Incorrect position (0):"<<endl;
cin>>a;
}
mouse_event(

MOUSEEVENTF_LEFTDOWN,
mx,
my,
0,
0
);

mouse_event(

MOUSEEVENTF_LEFTUP,
mx,
my,
0,
0
);

}
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this