-
Timer in Console...
How do I manipulate the WM_TIMER message in a console program? Here is my code:
PHP Code:
#include <iostream.h>
#include <windows.h>
void sleep(int milliseconds);
void time();
int main()
{
HWND myhwnd;
myhwnd = FindWindow("ConsoleWindowClass", NULL);
SetTimer(myhwnd, 1, 1000, NULL);
cout << "Hello." << endl;
cout << "Exiting in 2 seconds.";
sleep(2000);
return 0;
}
void sleep(int milliseconds)
{
Sleep(milliseconds);
}
void time()
{
HWND myhwnd;
myhwnd = FindWindow("ConsoleWindowClass", NULL);
MessageBox(myhwnd, "Hello, this is a timer", "MOO!!", NULL);
}
I know there isn't much about timers in there, but if you all could just tell me how to manipulate WM_TIMER, I'd be grateful.
-
You need to setup a TimerProc callback
PHP Code:
[i]From MSDN[/i]
VOID CALLBACK TimerProc(
HWND hwnd, // handle to window
UINT uMsg, // WM_TIMER message
UINT_PTR idEvent, // timer identifier
DWORD dwTime // current system time
);
Then pass the address of TimerProc into SetTimer.
:)
-
Can you give me an example of how to do this? (Just fiddling with C++, really a complete newb...)
-
So no one has done this before?
-
You define whatever you want the timer to do in the TimerProc function (or whatever your function is called), then give SetTimer() a pointer to that function so it can call it whenever the timer goes off.
Crptcblade already gave you the function declaration, you just need to define its content:
Code:
VOID CALLBACK TimerProc(
HWND hwnd, // handle to window
UINT uMsg, // WM_TIMER message
UINT_PTR idEvent, // timer identifier
DWORD dwTime // current system time
)
{
// your code here
}
-
Ok I fixed the error but the message box in the function is not showing up. Here is my code:
PHP Code:
//Includes
#include <iostream.h>
#include <windows.h>
//Globals
HWND myhwnd=FindWindow("ConsoleWindowClass", NULL);
//Function Prototypes
void sleep(int milliseconds);
VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
int main()
{
SetTimer(myhwnd, 1, 1000, (TIMERPROC) &TimerProc);
cout << "Hello." << endl;
MessageBox(myhwnd, "Wow, I can make message boxes...haha", "WEEEEE!!!", MB_OK + MB_ICONSTOP);
cout << "Exiting in 2 seconds.\n";
//sleep(2000);
return 0;
}
void sleep(int milliseconds)
{
Sleep(milliseconds);
}
VOID CALLBACK TimerProc(
HWND hwnd, // handle to window
UINT uMsg, // WM_TIMER message
UINT_PTR idEvent, // timer identifier
DWORD dwTime // current system time
)
{
MessageBox(myhwnd, "HELLO", "HELLO", MB_OK);
}
-
That doesn't work unless GetMessage is called - so it doesn't work in console apps.
-
Ok, so how do I make certain events fire off at a certain incrememnt in a console app?
-
You can create a high-performance timer, it's events are fired in a seperate thread.
Basically you don't have timers in console apps, they are designed for linear execution.