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
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>>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
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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
#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
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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
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
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343