•
•
•
•
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
![]() |
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:
. 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;
}•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,199
Reputation:
Rep Power: 34
Solved Threads: 824
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 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
- Gore Vidal
Being ignorant is not so much a shame as being unwilling to learn. - Benjamin Franklin
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
•
•
Join Date: May 2008
Posts: 10
Reputation:
Rep Power: 1
Solved Threads: 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?
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,199
Reputation:
Rep Power: 34
Solved Threads: 824
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.
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.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,199
Reputation:
Rep Power: 34
Solved Threads: 824
You are right -- it does work, and even in console programs
Change main() like this too:
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
- Gore Vidal
Being ignorant is not so much a shame as being unwilling to learn. - Benjamin Franklin
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
- Check to see if a mouse click missed an object (Game Development)
- Mouse click (Visual Basic 4 / 5 / 6)
- Simulate Mouse Move (C++)
- How to store data in data grid to database by single mouse click in Asp.net (ASP.NET)
- Simulate Mouse Click (Visual Basic 4 / 5 / 6)
- Mouse Right Click Menu (Windows NT / 2000 / XP / 2003)
- Mouse buttons are messed up (Peripherals)
Other Threads in the C++ Forum
- Previous Thread: i dont understand how this code thing works to put up a program. what does it mean?
- Next Thread: Help me with txt!!!



Linear Mode