| | |
Mouse Click
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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: C++ Syntax (Toggle Plain Text)
#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; }
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
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Mar 2008
Posts: 1,400
Reputation:
Solved Threads: 113
It looks to me like the code you wrote never releases the click. This is how you fully emulate a mouse click:
C++ Syntax (Toggle Plain Text)
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:
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:
C++ Syntax (Toggle Plain Text)
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: Mar 2008
Posts: 1,400
Reputation:
Solved Threads: 113
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.
C++ Syntax (Toggle Plain Text)
#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 William Hemsworth; May 10th, 2008 at 10:36 am.
You are right -- it does work, and even in console programs
Change main() like this too:
Change main() like this too: C++ Syntax (Toggle Plain Text)
int main() { HWND paint = GetConsoleWindow();
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- 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)
- Mouse buttons are messed up (USB Devices and other 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!!!
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






