954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++: Clicking Locations on Screen

I've been teaching myself C++ for the last two weeks. One of the things that was difficult for me to find on google was using the left click on a certain coordinate on the screen. The goal is to eventually be able to recognize images on the screen and alter the path accordingly, but until I gain more experience, I'd like to focus on the previously stated problem. Any help would be appreciated. Thank you.

portege
Light Poster
26 posts since Jul 2006
Reputation Points: 13
Solved Threads: 1
 
I've been teaching myself C++ for the last two weeks. One of the things that was difficult for me to find on google was using the left click on a certain coordinate on the screen. The goal is to eventually be able to recognize images on the screen and alter the path accordingly, but until I gain more experience, I'd like to focus on the previously stated problem. Any help would be appreciated. Thank you.


Clicking, will depend on what operating system you are using.

If you're using windows check out the windows api from the msdn website.

The latter is more difficult. To recognise an image, that is to have your program intelligently match an image to another by itself, would require some sort of artificial intelligence programming.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

windows api will return the x,y coordinates of the mouse. If you know the coordinates of the image's bounding rectangle then you just need to find out which image rectangle contains the mouse coordinates. Using MFC will simplify that, but you can also use windows RECT structure and write your own code to make the "hit test".

If you are not using MS-Windows, I have no clue how to do it with *nix.

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

All of the code I've seen for it has been incomplete pieces in Microsoft Visual C++. Is there a simpler way to just return the coordinates of the cursor or move the cursor to a coordinate using a format I am more familar with?

portege
Light Poster
26 posts since Jul 2006
Reputation Points: 13
Solved Threads: 1
 

I don't think you'll be able to do this with 2 weeks of C++ experience. Of course I have no idea about how good you are at C++, but I think it's too advanced.

You should search www.msdn.com , there are several APIs that will handle mouse-things.

There is also something to record mouse movements, I don't really remember much of it.

codeproject has a mouse-macro recorder with source, you might want to check it.

Goodluck,
Eddy

Eddy Dean
Junior Poster in Training
56 posts since Jul 2006
Reputation Points: 38
Solved Threads: 3
 

The window's message pump should be checking for WM_MOUSEMOVE messages. This message will give you the cordinates of the mouse. If you are not at all familiar with win32 api functions, then a good place to start is with this tutorial .

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I've spent the last couple of days trying to put together from bits I've found online with no success. I'm not if I failed because the code doesn't compile on Dev-C++ or if I just don't understand it. Is there any chance someone could put together the whole thing for me? Output a number with teh current cursor position or move the cursor to a coordinate. I can't imagine it taking up much more than 10-15 lines. :-/

portege
Light Poster
26 posts since Jul 2006
Reputation Points: 13
Solved Threads: 1
 

>>I can't imagine it taking up much more than 10-15 lines

HaHa you really are new to this aren't you! run through the tutorial I posted previously to get down the basic window then add a little code to check for WM_MOUSEMOVE events. You will probably know how to do that once you have completed the tutorial.

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I read through the tutorial. I'm sure this is nowhere near correct, but could you help me from here?

#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;
int WINAPI WinMain()
{
LRESULT CALLBACK WndProc(WPARAM wParam, LPARAM lParam)
{
WM_MOUSEMOVE fwKeys = wParam; 
int xPos = LOWORD(lParam); 
int yPos = HIWORD(lParam);
cout<<xPos<<", "<<yPos;
system("pause");
}
}
portege
Light Poster
26 posts since Jul 2006
Reputation Points: 13
Solved Threads: 1
 

If you really did go through that tutorial -- thoroughly I might add -- you should know that what you posted is not even close to being correct. Read it thorougly again, or again, or as many times as it takes for you to know it well. If you are not willing to put in the effort to do that, then nobody else is going to do it for you.

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

^^I know. That's why I said it's nowhere near correct. :P

I found a MUCH easier way to do this. GetCursorPos() and SetCursorPos(x,y). ;-) One question though: what do I define x and y as? I tried point, but that didn't work.

portege
Light Poster
26 posts since Jul 2006
Reputation Points: 13
Solved Threads: 1
 

Just wack in some numbers?

#include <windows.h>

int main(void)
{
    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
    return 0;
}

#include <windows.h>

int main(void)
{
    SetCursorPos(10, 20);
    return 0;
}
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

I'd like to be able to set the value for the variables. What would I identify the variables as?

portege
Light Poster
26 posts since Jul 2006
Reputation Points: 13
Solved Threads: 1
 
>>I can't imagine it taking up much more than 10-15 lines HaHa you really are new to this aren't you! run through the tutorial I posted previously to get down the basic window then add a little code to check for WM_MOUSEMOVE events. You will probably know how to do that once you have completed the tutorial.



I got 10 lines. ;)

#include <windows.h>
#include <iostream>
using namespace std;
int main(){
POINT c;
GetCursorPos(&c);
int x=c.x;
int y=c.y;
cout<<x<<", "<<y;
system("PAUSE");}
portege
Light Poster
26 posts since Jul 2006
Reputation Points: 13
Solved Threads: 1
 
#include <windows.h>
#include <iostream>
int main(){
POINT c;
GetCursorPos(&c);
std::cout<<c.x<<", "<<c.y;
}


is enough...

7 lines

You were wrong with your estimation 10-15 lines!

Some compilers might not agree that it does not have a return value. I had to include StdAfx.h because I used visual c++.


Greetz, Eddy

Eddy Dean
Junior Poster in Training
56 posts since Jul 2006
Reputation Points: 38
Solved Threads: 3
 

that seems to work ok :) next trick is how to know when the cursor moves? or when the left mouse button is pressed? How will you know when to call GetCursorPos() as stated in your original post? A console program does not get windows events so it will not know when the cursor is moved over the bitmap, or between bitmaps or anywhere else on the window.

[edit]There is a whole series of console functions that might help you. Look for consol win events where you can set up an event handler for mouse events. I haven't tried it so you might have to do some experimentation with it.

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Any chance anyone could give me a line or two that tells it to leftclick at its active location? I can't find this in C++. :-/

portege
Light Poster
26 posts since Jul 2006
Reputation Points: 13
Solved Threads: 1
 

I got 10 lines. ;)

#include <windows.h>
#include <iostream>
using namespace std;
int main(){
POINT c;
GetCursorPos(&c);
int x=c.x;
int y=c.y;
cout<<x<<", "<<y;
system("PAUSE");}

Dont say things without reading the entire post submitted by a very experienced and senior member.

What he wanted to said that if u intended on drawing a window or creating a window u need atleast 60 - 70 lines of code which is well formatted.

What u have written above is not a GUI or window application but a console application.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 
Any chance anyone could give me a line or two that tells it to leftclick at its active location? I can't find this in C++. :-/

catch the WM_LBUTTONDOWN event in the WinProc event handler.

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I'm looking for a single command that does it not a case to alert me when it happens. Do you know of one?

For example, if this was the mouse move, I would want SetPosition() not WM_MOUSEMOVE. It is hard for me to find the command that works in C++.

portege
Light Poster
26 posts since Jul 2006
Reputation Points: 13
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You