0

Hey guys, i have done C++ before but gave up on it :P. 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;


 }
5
Contributors
12
Replies
18
Views
8 Years
Discussion Span
Last Post by William Hemsworth
Featured Replies
  • 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. [CODE] #include <windows.h> #include <cmath> int main() { system("start c:\\windows\\system32\\mspaint.exe"); HWND paint; do { … Read More

  • Not only that, most people think that it is entirely impossible to do graphics in console applications. Heres the proof againts that: [CODE] #define _WIN32_WINNT 0x0500 #include<windows.h> #include<iostream> using namespace std; int main() { HWND console = GetConsoleWindow(); HDC hdc = GetDC(console); SelectObject(hdc, CreatePen(0, 30, RGB(255,255,255))); POINT p; GetCursorPos(&p); ScreenToClient(console, … Read More

0

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

0

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

0

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
0

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?

2

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;
}
Votes + Comments
a nice one
excellent example :)
0

You are right -- it does work, and even in console programs :) Change main() like this too:

int main() {
	HWND paint = GetConsoleWindow();
1

Not only that, most people think that it is entirely impossible to do graphics in console applications. Heres the proof againts that:

#define _WIN32_WINNT 0x0500
#include<windows.h>
#include<iostream>
using namespace std;

int main() {
	HWND console = GetConsoleWindow();
	HDC hdc = GetDC(console);
	SelectObject(hdc, CreatePen(0, 30, RGB(255,255,255)));
	POINT p;
	GetCursorPos(&p);
	ScreenToClient(console, &p);
	MoveToEx(hdc, p.x, p.y, NULL);
	for (;;) {
		GetCursorPos(&p);
		ScreenToClient(console, &p);
		LineTo(hdc, p.x, p.y);
		Sleep(1);
	}
	ReleaseDC(console, hdc);
	cin.ignore();
	return 0;
}
Votes + Comments
good stuff
0

how very interesting...

I suppose my point was only that, why would you want to detect a mouse click if only using a text-based console...Hence why I would have never considered exploring the subject on a console...

Thanks for that will!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.