Valhalla Legends Forums Archive | C/C++ Programming | SetTime() along with mouse_event()

AuthorMessageTime
Eli_1
[code][/code]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:
[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;
}
[/code]

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


Add-on:
How would I do the constants in C++ using #define?
[code]
#define EVENT_MOUSEDOWN ??? (what is &H2)?
#define EVENT_MOUSEUP ??? (what is &H4)? ect...
[/code]
In addition, how would I make it equal to something like this?
&H100101
&HFFFFFF
&H776F
&H3C3C
&H11010
March 31, 2004, 12:00 PM
j0k3r
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:

[code]#define studentsPerClass 15
[/code]

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++:

[code]const unsigned short int studentsPerClass = 15;
[/code]

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
March 31, 2004, 12:25 PM
Adron
[quote author=j0k3r link=board=30;threadid=6087;start=0#msg52817 date=1080735913]
DON'T use the term int. Use short and long to make it clear which size number you intended.
[/quote]

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.
March 31, 2004, 1:33 PM
Zakath
[quote author=Eli_1 link=board=30;threadid=6087;start=0#msg52814 date=1080734425]
Add-on:
How would I do the constants in C++ using #define?
[code]
#define EVENT_MOUSEDOWN ??? (what is &H2)?
#define EVENT_MOUSEUP ??? (what is &H4)? ect...
[/code]
In addition, how would I make it equal to something like this?
&H100101
&HFFFFFF
&H776F
&H3C3C
&H11010
[/quote]

[code]
#define EVENT_MOUSEDOWN 0x2
#define EVENT_MOUSEUP 0x4
[/code]
0x100101
0xffffff
0x776f
0x3c3c
0x11010
March 31, 2004, 5:37 PM
Eli_1
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:
[code]
while(GetMessage(&msg, 0, 0, 0) == TRUE && click_time != 0)
DispatchMessage(&msg);
[/code]
even after setting click_time to 0?


[Edit 1] When I change the loop to:
[code]
while(click_time != 0 && GetMessage(&msg, 0, 0, 0) == TRUE)
   DispatchMessage(&msg);
[/code]
It works fine and doesn't stall there when I change click_time. Why?
March 31, 2004, 7:35 PM
Eibro
[quote author=Eli_1 link=board=30;threadid=6087;start=0#msg52874 date=1080761757]
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:
[code]
while(GetMessage(&msg, 0, 0, 0) == TRUE && click_time != 0)
DispatchMessage(&msg);
[/code]
even after setting click_time to 0?


[Edit 1] When I change the loop to:
[code]
while(click_time != 0 && GetMessage(&msg, 0, 0, 0) == TRUE)
DispatchMessage(&msg);
[/code]
It works fine and doesn't stall there when I change click_time. Why?
[/quote]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().
March 31, 2004, 10:08 PM

Search