• Welcome to Valhalla Legends Archive.
 

SetTime() along with mouse_event()

Started by Eli_1, March 31, 2004, 06:00 AM

Previous topic - Next topic

Eli_1

After answering on how to change the cursor position and send mouse events in VB, I decided I wanted to try to do it in C++ (comp. reformated and my VB cd is scratched  :'().

The program runs fine and all, but it isn't cleaning up the timer right.

Code:

#include <windows.h>
#include <winuser.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

long click_time = 1000;
HWND hwnd;
int click_count = 10;
long timer_id;

VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) {
   if (click_count == 0) {
      printf("Cleaning up...\n");
      click_time = 0;
      KillTimer(hwnd, timer_id);
   } else {
      click_count--;
      mouse_event(2, 0, 0, 0, 0);
      mouse_event(4, 0, 0, 0, 0);
   }

}

int main() {

   char input_buff[256];
   printf("Click every x seconds (clicks 10 times): ");
   fgets(input_buff, 256, stdin);
   
   click_time = atoi(input_buff);
   if (click_time < 1000) {
      printf("For the sake of this comp, the click_time is now set to 1000 :/\n");
      click_time = 1000;
   }
   
   timer_id = SetTimer(hwnd, 0, click_time, &TimerProc);
   MSG msg;
   while(GetMessage(&msg, 0, 0, 0) == TRUE && click_time != 0)
      DispatchMessage(&msg);
      
   printf("KillTimer()\n");
   printf("Done.\n");
   return 0;
}


Output:
Quote
Click every x seconds (clicks 10 times): 1000
*clicks 10 times every second*
Cleaning up...
And it just hangs there... :/


Add-on:
How would I do the constants in C++ using #define?

#define EVENT_MOUSEDOWN ??? (what is &H2)?
#define EVENT_MOUSEUP       ??? (what is &H4)? ect...

In addition, how would I make it equal to something like this?
&H100101
&HFFFFFF
&H776F
&H3C3C
&H11010

j0k3r

#1
There are two ways to declare a symbolic constant in C++. The old, traditional, and now obsolete way is with a preprocessor directive, #define. Defining Constants with #define To define a constant the traditional way, you would enter this:

#define studentsPerClass 15


Note that studentsPerClass is of no particular type (int, char, and so on). #define does a simple text substitution. Every time the preprocessor sees the word studentsPerClass, it puts in the text 15.

Because the preprocessor runs before the compiler, your compiler never sees your constant; it sees the number 15. Defining Constants with const Although #define works, there is a new, much better way to define constants in C++:

const unsigned short int studentsPerClass = 15;


This example also declares a symbolic constant named studentsPerClass, but this time studentsPerClass is typed as an unsigned short int. This method has several advantages in making your code easier to maintain and in preventing bugs. The biggest difference is that this constant has a type, and the compiler can enforce that it is used according to its type.

--------------------------------------------------------------------------------
NOTE: Constants cannot be changed while the program is running. If you need to change studentsPerClass, for example, you need to change the code and recompile.
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
DON'T use the term int. Use short and long to make it clear which size number you intended. DO watch for numbers overrunning the size of the integer and wrapping around incorrect values. DO give your variables meaningful names that reflect their use. DON'T use keywords as variable names.
--------------------------------------------------------------------------------

-- Teach Yourself C++ in 21 days,  Chapter 03
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

Adron

Quote from: j0k3r on March 31, 2004, 06:25 AM
DON'T use the term int. Use short and long to make it clear which size number you intended.

That's rather stupid, as short and long don't make it all that much clearer anyway. You should use an int when you don't care about the size. If you care about the size and about portability, you should use a typedef. If you care about the size and don't care that much about portability you can use short, int or long, whichever works right on your compiler.

Zakath

Quote from: Eli_1 on March 31, 2004, 06:00 AM
Add-on:
How would I do the constants in C++ using #define?

#define EVENT_MOUSEDOWN ??? (what is &H2)?
#define EVENT_MOUSEUP       ??? (what is &H4)? ect...

In addition, how would I make it equal to something like this?
&H100101
&HFFFFFF
&H776F
&H3C3C
&H11010


#define EVENT_MOUSEDOWN 0x2
#define EVENT_MOUSEUP 0x4

0x100101
0xffffff
0x776f
0x3c3c
0x11010
Quote from: iago on February 02, 2005, 03:07 PM
Yes, you can't have everybody...contributing to the main source repository.  That would be stupid and create chaos.

Opensource projects...would be dumb.

Eli_1

#4
Oh, I tryed using "\0x01" which got a compiler error becase of, "got char *, expected long". Thanks ;D

And jok3r, I think you mis-understood my question, I didn't literally want to know how to define a constant, I just wanted to know how to write something like &H### in C++.

Add-on:
Any idea why my program is never exiting this loop:

while(GetMessage(&msg, 0, 0, 0) == TRUE && click_time != 0)
     DispatchMessage(&msg);

even after setting click_time to 0?


[Edit 1] When I change the loop to:

while(click_time != 0 && GetMessage(&msg, 0, 0, 0) == TRUE)
   DispatchMessage(&msg);

It works fine and doesn't stall there when I change click_time. Why?

Eibro

Quote from: Eli_1 on March 31, 2004, 01:35 PM
Oh, I tryed using "\0x01" which got a compiler error becase of, "got char *, expected long". Thanks ;D

And jok3r, I think you mis-understood my question, I didn't literally want to know how to define a constant, I just wanted to know how to write something like &H### in C++.

Add-on:
Any idea why my program is never exiting this loop:

while(GetMessage(&msg, 0, 0, 0) == TRUE && click_time != 0)
     DispatchMessage(&msg);

even after setting click_time to 0?


[Edit 1] When I change the loop to:

while(click_time != 0 && GetMessage(&msg, 0, 0, 0) == TRUE)
  DispatchMessage(&msg);

It works fine and doesn't stall there when I change click_time. Why?
It probably stalls because GetMessage blocks while waiting for a message. Once there's no more WM_TIMER messages going through, it blocks indefinitely. GetMessage will only return 0 ( or -1 ) if it receives a WM_QUIT message, or an error occours. Try calling SendMessage( hwnd, WM_QUIT, 0, 0 ); after KillTimer().
Eibro of Yeti Lovers.