-
Timer
Hi!
I'm having some trouble with creating a timer (for the 1st time) for my win32 dialog app. I tried to call SetTimer() from the dialog procedure, and created a Timer procedure for it. But it didn't work. The breakpoint inside TimerProc() never got hit. What i'm doing wrong, please?
Code:
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
DialogBox(hInstance, MAKEINTRESOURCE(IDMAINDLG), hWnd,
reinterpret_cast<DLGPROC>(DlgProc));
...............
...............
}
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg,
WPARAM wParam, LPARAM lParam)
{
...............
...............
SetTimer(hWndDlg,IDTIMER1,1000,(TIMERPROC)TimerProc);
}
VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,
UINT_PTR idEvent,DWORD dwTime){
...............
...............
}
-
Try this
Code:
SetTimer(hWndDlg,IDTIMER1,1000,(TIMERPROC)&TimerProc c);
-
Sorry it doesn't seems to work.
I also tried to call SetTimer() within WinMain(), and placed a WN_TIMER event handler in DlgProc(). But no luck either.
Code:
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
HWND Dlg;
SetTimer(Dlg,IDTIMER1,100,NULL);
..........
}
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg,
WPARAM wParam, LPARAM lParam)
{
...............
switch(Msg){
case WM_TIMER:
switch(wParam){
case IDTIMER1:
.....
.....
}
.......
}
}
-
This should work for you
Wherever you declare your globals:
Code:
#define IDC_TIMER 600 /* Your choice for ID */
UINT g_unTimerRet;
In your message callback:
Code:
LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_CREATE: {
g_unTimerRet = SetTimer(hWnd, IDC_TIMER, 1000, &TimerProc);
break;
}
case WM_DESTROY: {
KillTimer(hWnd, g_unTimerRet); /* Don't forget to cleanup your mess! */
break;
}
Declare your callback for the timer (prototype) wherever you plan on putting the whole function. For example in main.c:
Code:
/* Prototype */
void CALLBACK TimerProc(HWND, UINT, UINT, DWORD);
/* The timer function that is executed every second according to when it was created */
void CALLBACK TimerProc(HWND hWnd, UINT uMsg, UINT idEvent, DWORD dwTime) {
/* Whatever code you want executed here */
}
I know this works because I use it in my apps and I acquired it from MSDN.
-