User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 375,229 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,266 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 641 | Replies: 12
Reply
Join Date: May 2008
Posts: 38
Reputation: tondeuse34 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
tondeuse34's Avatar
tondeuse34 tondeuse34 is offline Offline
Light Poster

Mouse Click

  #1  
May 9th, 2008
Hey guys, i have done C++ before but gave up on it . But i've came back to it and i'm wondering what is a good way to simulate a left and right click? i guess you would start with this:
#include <iostream>
#include <windows.h>

using namespace std;

int main ()
{
 int n = 1;
 int x;
 int y;

 while (n !=NULL)

 {

 if(GetAsyncKeyState(VK_NUMPAD0))
 (

 mouse_event(MOUSEEVENT_LEFTDOWN, x, y, 100, 100));
 mouse_event(MOUSEEVENT_LEFTDOWN, x, y, 200, 200));

 }

 if(GetAsyncKeyState(VK_NUMBPAD1))
 break;

 }

 return 0;


 }
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2008
Posts: 78
Reputation: n1337 is on a distinguished road 
Rep Power: 1
Solved Threads: 7
n1337 n1337 is offline Offline
Junior Poster in Training

Re: Mouse Click

  #2  
May 9th, 2008
Ummm well, left and right click gets into events and event handling...that is, Win32 programming.

I suggest you start by doing some google searches on this topic As an added bonus, if you learn Win32, you can leave the boring old console and start making pretty GUIs...haha
Reply With Quote  
Join Date: May 2008
Posts: 38
Reputation: tondeuse34 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
tondeuse34's Avatar
tondeuse34 tondeuse34 is offline Offline
Light Poster

Re: Mouse Click

  #3  
May 9th, 2008
talk about some confusing stuff, python has spoiled me lol
Last edited by tondeuse34 : May 9th, 2008 at 11:36 pm.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,199
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 34
Solved Threads: 824
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Mouse Click

  #4  
May 9th, 2008
Yes, writing GUI in c++ is pretty difficult stuff. Other languages such as python, vb, and C# are better suited for that.

I don't think you can process mouse events like that in console programs. The program needs a message pump in order to get and process windows event messages, and console programs don't have that capability.

Read this about windows console functions
'Politics' is made up of two words, 'poli,' which is Greek for 'many,' and 'tics,' which are blood-sucking insects.
- Gore Vidal
Being ignorant is not so much a shame as being unwilling to learn. - Benjamin Franklin
Reply With Quote  
Join Date: Mar 2008
Location: UK
Posts: 240
Reputation: williamhemswort has a spectacular aura about williamhemswort has a spectacular aura about 
Rep Power: 2
Solved Threads: 31
williamhemswort's Avatar
williamhemswort williamhemswort is offline Offline
Posting Whiz in Training

Re: Mouse Click

  #5  
May 10th, 2008
It looks to me like the code you wrote never releases the click. This is how you fully emulate a mouse click:

mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); // Left click
mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0); // Right click
Reply With Quote  
Join Date: May 2008
Posts: 10
Reputation: elementz is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
elementz elementz is offline Offline
Newbie Poster

Re: Mouse Click

  #6  
May 10th, 2008
Originally Posted by williamhemswort View Post
It looks to me like the code you wrote never releases the click. This is how you fully emulate a mouse click:

mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); // Left click
mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0); // Right click


does this work?
Reply With Quote  
Join Date: Mar 2008
Location: UK
Posts: 240
Reputation: williamhemswort has a spectacular aura about williamhemswort has a spectacular aura about 
Rep Power: 2
Solved Threads: 31
williamhemswort's Avatar
williamhemswort williamhemswort is offline Offline
Posting Whiz in Training

Re: Mouse Click

  #7  
May 10th, 2008
Originally Posted by elementz View Post
does this work?


Yes it does
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,199
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 34
Solved Threads: 824
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Mouse Click

  #8  
May 10th, 2008
Originally Posted by williamhemswort View Post
Yes it does

Not in console programs.
'Politics' is made up of two words, 'poli,' which is Greek for 'many,' and 'tics,' which are blood-sucking insects.
- Gore Vidal
Being ignorant is not so much a shame as being unwilling to learn. - Benjamin Franklin
Reply With Quote  
Join Date: Mar 2008
Location: UK
Posts: 240
Reputation: williamhemswort has a spectacular aura about williamhemswort has a spectacular aura about 
Rep Power: 2
Solved Threads: 31
williamhemswort's Avatar
williamhemswort williamhemswort is offline Offline
Posting Whiz in Training

Re: Mouse Click

  #9  
May 10th, 2008
It works with me

As long as you include the window header I think you can still use any of the functions there.
heres an example of what you can do with a console application.
#include <windows.h>
#include <cmath>

int main() {
	system("start c:\\windows\\system32\\mspaint.exe");
	HWND paint;

	do {
		paint = FindWindow("MSPaintApp", "Untitled - Paint");
		Sleep(10);
	} while (paint == NULL);

	Sleep(200);
	RECT r;
	GetWindowRect(paint, &r);
	RECT cr;
	GetClientRect(paint, &cr);

	int x = r.left + (cr.right / 2);
	int y = r.top + (cr.bottom / 2);

	SetCursorPos(x,y);

	POINT p;
	GetCursorPos(&p);
	paint = WindowFromPoint(p);

	GetWindowRect(paint, &r);
	GetClientRect(paint, &cr);

	x = r.left + (cr.right / 2);
	y = r.top + (cr.bottom / 2);

	SetCursorPos(x,y);

	int radius = 0;
	double angle = 0;

	int max = min(cr.right/2,cr.bottom/2);

	mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);

	for (; radius < max; radius++, angle += 0.1) {
		SetCursorPos(x+(cos(angle)*radius), y+(sin(angle)*radius));
		Sleep(20);
	}

	mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);

	return 0;
}
Last edited by williamhemswort : May 10th, 2008 at 9:36 am.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,199
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 34
Solved Threads: 824
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Mouse Click

  #10  
May 10th, 2008
You are right -- it does work, and even in console programs Change main() like this too:
int main() {
	HWND paint = GetConsoleWindow();
'Politics' is made up of two words, 'poli,' which is Greek for 'many,' and 'tics,' which are blood-sucking insects.
- Gore Vidal
Being ignorant is not so much a shame as being unwilling to learn. - Benjamin Franklin
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 3:57 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC