Jump to content
  • Advertisement
Sign in to follow this  
archr926

Sending mouseclicks with sendmessage()

This topic is 5057 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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, 0x80000000); } And was wondering how to send mouseclicks through sendmessage

Share this post


Link to post
Share on other sites
Advertisement
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
Nevermind, it worked when I moved minesweeper to the ipwindowname, and then removed references to the findwindowex function.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!