|
-
Jul 2nd, 2002, 01:41 PM
#1
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.
-
Jul 2nd, 2002, 02:58 PM
#2
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.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jul 2nd, 2002, 03:03 PM
#3
Can you give me an example of how to do this? (Just fiddling with C++, really a complete newb...)
-
Jul 2nd, 2002, 06:37 PM
#4
So no one has done this before?
-
Jul 2nd, 2002, 06:43 PM
#5
Frenzied Member
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
}
Harry.
"From one thing, know ten thousand things."
-
Jul 2nd, 2002, 06:49 PM
#6
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);
}
-
Jul 10th, 2002, 07:17 PM
#7
That doesn't work unless GetMessage is called - so it doesn't work in console apps.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jul 10th, 2002, 08:10 PM
#8
Ok, so how do I make certain events fire off at a certain incrememnt in a console app?
-
Jul 10th, 2002, 08:32 PM
#9
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|