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 332,647 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,278 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: 241 | Replies: 12
Reply
Join Date: May 2008
Posts: 23
Reputation: tondeuse34 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
tondeuse34's Avatar
tondeuse34 tondeuse34 is offline Offline
Newbie Poster

Mouse Click

  #1  
7 Days Ago
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: 39
Reputation: n1337 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
n1337 n1337 is offline Offline
Light Poster

Re: Mouse Click

  #2  
7 Days Ago
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: 23
Reputation: tondeuse34 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
tondeuse34's Avatar
tondeuse34 tondeuse34 is offline Offline
Newbie Poster

Re: Mouse Click

  #3  
7 Days Ago
talk about some confusing stuff, python has spoiled me lol
Last edited by tondeuse34 : 7 Days Ago at 11:36 pm.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 9,212
Reputation: Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold 
Rep Power: 29
Solved Threads: 720
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Posting Prodigy

Re: Mouse Click

  #4  
7 Days Ago
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
I Like Ike. Vote for Dwight Eisenhower this November.
--This message brought to you by the the Procrastinators Club Of America.
Reply With Quote  
Join Date: Mar 2008
Location: UK
Posts: 86
Reputation: williamhemswort is on a distinguished road 
Rep Power: 1
Solved Threads: 12
williamhemswort's Avatar
williamhemswort williamhemswort is offline Offline
Junior Poster in Training

Re: Mouse Click

  #5  
6 Days Ago
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  
6 Days Ago
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: 86
Reputation: williamhemswort is on a distinguished road 
Rep Power: 1
Solved Threads: 12
williamhemswort's Avatar
williamhemswort williamhemswort is offline Offline
Junior Poster in Training

Re: Mouse Click

  #7  
6 Days Ago
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: 9,212
Reputation: Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold 
Rep Power: 29
Solved Threads: 720
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Posting Prodigy

Re: Mouse Click

  #8  
6 Days Ago
Originally Posted by williamhemswort View Post
Yes it does

Not in console programs.
I Like Ike. Vote for Dwight Eisenhower this November.
--This message brought to you by the the Procrastinators Club Of America.
Reply With Quote  
Join Date: Mar 2008
Location: UK
Posts: 86
Reputation: williamhemswort is on a distinguished road 
Rep Power: 1
Solved Threads: 12
williamhemswort's Avatar
williamhemswort williamhemswort is offline Offline
Junior Poster in Training

Re: Mouse Click

  #9  
6 Days Ago
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 : 6 Days Ago at 9:36 am.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 9,212
Reputation: Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold 
Rep Power: 29
Solved Threads: 720
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Posting Prodigy

Re: Mouse Click

  #10  
6 Days Ago
You are right -- it does work, and even in console programs Change main() like this too:
int main() {
	HWND paint = GetConsoleWindow();
I Like Ike. Vote for Dwight Eisenhower this November.
--This message brought to you by the the Procrastinators Club Of America.
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 2:55 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC