Sending mouseclicks with sendmessage()

Started by
17 comments, last by archr926 19 years, 5 months ago
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, 0x80000000); } And was wondering how to send mouseclicks through sendmessage
Advertisement
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 :)
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?
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
CheersChris
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.
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
CheersChris
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();
}
Nevermind, it worked when I moved minesweeper to the ipwindowname, and then removed references to the findwindowex function.
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...

Niko Suni

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.

This topic is closed to new replies.

Advertisement