首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

win32程序下怎么设定定时器

2013-09-06 
win32程序下如何设定定时器回调函数的使用我不是很明确,调试的时候总是不成功,直接使用settimer(NULL,0,10

win32程序下如何设定定时器
回调函数的使用我不是很明确,调试的时候总是不成功,直接使用settimer(NULL,0,100000,NULL)的时候,晚上说可以直接调用timeon()函数的,但是我用vs2012没有找到这个函数啊!求哪位朋友指导下或者给个能跑通的例子啊   谢谢了  win32 Visual?Studio?2012 c++
[解决办法]
去看windows程序设计(第五版)计时器的那章 里面有使用方法 和能用的例子 如果你确实对timer的使用都比较熟悉的  那我就不知道vs2012怎么运行不了了  我用2008的 没有不能使用timer的情况
[解决办法]
仅供参考

#pragma comment(lib,"user32")
#include <stdio.h>
#include <time.h>
#include <sys/timeb.h>
#include <windows.h>
char datestr[16];
char timestr[16];
char mss[4];
void log(char *s) {
    struct tm *now;
    struct timeb tb;

    ftime(&tb);
    now=localtime(&tb.time);
    sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
    sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );
    sprintf(mss,"%03d",tb.millitm);
    printf("%s %s.%s %s",datestr,timestr,mss,s);
}
VOID CALLBACK myTimerProc1(
  HWND hwnd, // handle of window for timer messages
  UINT uMsg, // WM_TIMER message
  UINT idEvent, // timer identifier
  DWORD dwTime // current system time
) {
 log("In myTimerProc1\n");
}
VOID CALLBACK myTimerProc2(
  HWND hwnd, // handle of window for timer messages
  UINT uMsg, // WM_TIMER message
  UINT idEvent, // timer identifier
  DWORD dwTime // current system time
) {
 log("In myTimerProc2\n");
}
int main() {
    int i;
    MSG msg;

    SetTimer(NULL,0,1000,myTimerProc1);


    SetTimer(NULL,0,2000,myTimerProc2);
    for (i=0;i<20;i++) {
        Sleep(500);
        log("In main\n");
        if (GetMessage(&msg,NULL,0,0)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

    }
    return 0;
}
//2012-07-26 17:29:06.375 In main
//2012-07-26 17:29:06.875 In myTimerProc1
//2012-07-26 17:29:07.375 In main
//2012-07-26 17:29:07.875 In myTimerProc2
//2012-07-26 17:29:08.375 In main
//2012-07-26 17:29:08.375 In myTimerProc1
//2012-07-26 17:29:08.875 In main
//2012-07-26 17:29:08.875 In myTimerProc1
//2012-07-26 17:29:09.375 In main
//2012-07-26 17:29:09.890 In myTimerProc2
//2012-07-26 17:29:10.390 In main
//2012-07-26 17:29:10.390 In myTimerProc1
//2012-07-26 17:29:10.890 In main
//2012-07-26 17:29:10.890 In myTimerProc1
//2012-07-26 17:29:11.390 In main
//2012-07-26 17:29:11.890 In myTimerProc2
//2012-07-26 17:29:12.390 In main
//2012-07-26 17:29:12.390 In myTimerProc1
//2012-07-26 17:29:12.890 In main
//2012-07-26 17:29:12.890 In myTimerProc1
//2012-07-26 17:29:13.390 In main
//2012-07-26 17:29:13.890 In myTimerProc2
//2012-07-26 17:29:14.390 In main
//2012-07-26 17:29:14.390 In myTimerProc1
//2012-07-26 17:29:14.890 In main
//2012-07-26 17:29:14.890 In myTimerProc1
//2012-07-26 17:29:15.390 In main
//2012-07-26 17:29:15.890 In myTimerProc2
//2012-07-26 17:29:16.390 In main
//2012-07-26 17:29:16.390 In myTimerProc1
//2012-07-26 17:29:16.890 In main
//2012-07-26 17:29:16.890 In myTimerProc1
//2012-07-26 17:29:17.390 In main
//2012-07-26 17:29:17.890 In myTimerProc2
//2012-07-26 17:29:18.390 In main
//2012-07-26 17:29:18.390 In myTimerProc1
//2012-07-26 17:29:18.890 In main
//2012-07-26 17:29:18.890 In myTimerProc1
//2012-07-26 17:29:19.390 In main
//2012-07-26 17:29:19.890 In myTimerProc2



[解决办法]
C:****:OnTimer
[解决办法]
CWnd::SetTimer
UINT SetTimer( UINT nIDEvent, UINT nElapse, void (CALLBACK EXPORT* lpfnTimer)(HWND, UINT, UINT, DWORD) );

Return Value

The timer identifier of the new timer if the function is successful. An application passes this value to the KillTimer member function to kill the timer. Nonzero if successful; otherwise 0.

Parameters

nIDEvent

Specifies a nonzero timer identifier.

nElapse

Specifies the time-out value, in milliseconds.

lpfnTimer

Specifies the address of the application-supplied TimerProc callback function that processes the WM_TIMER messages. If this parameter is NULL, the WM_TIMER messages are placed in the application’s message queue and handled by the CWnd object.

Remarks

Installs a system timer. A time-out value is specified, and every time a time-out occurs, the system posts aWM_TIMER message to the installing application’s message queue or passes the message to an application-defined TimerProc callback function. 

The lpfnTimer callback function need not be named TimerProc, but it must be defined as follows:

void CALLBACK EXPORT TimerProc(
   HWND hWnd,      // handle of CWnd that called SetTimer
   UINT nMsg,      // WM_TIMER
   UINT nIDEvent   // timer identification
   DWORD dwTime    // system time
);

Timers are a limited global resource; therefore it is important that an application check the value returned by the SetTimer member function to verify that a timer is actually available.

CWnd Overview 
------解决方案--------------------


  Class Members 
[解决办法]
  Hierarchy Chart

See Also   WM_TIMER, CWnd::KillTimer,::SetTimer



CWnd::KillTimer
BOOL KillTimer( int nIDEvent );

Return Value

Specifies the outcome of the function. The value is nonzero if the event was killed. It is 0 if the KillTimer member function could not find the specified timer event.

Parameters

nIDEvent

The value of the timer event passed to SetTimer.

Remarks

Kills the timer event identified by nIDEvent from the earlier call to SetTimer. Any pending WM_TIMER messages associated with the timer are removed from the message queue.

CWnd Overview 
[解决办法]
  Class Members 
[解决办法]
  Hierarchy Chart

See Also   CWnd::SetTimer,::KillTimer


CWnd::OnTimer  
afx_msg void OnTimer( UINT nIDEvent );

Parameters

nIDEvent

Specifies the identifier of the timer.

Remarks

The framework calls this member function after each interval specified in the SetTimer member function used to install a timer. 

TheDispatchMessage Windows function sends aWM_TIMER message when no other messages are in the application’s message queue.

Note   This member function is called by the framework to allow your application to handle a Windows message. The parameters passed to your function reflect the parameters received by the framework when the message was received. If you call the base-class implementation of this function, that implementation will use the parameters originally passed with the message and not the parameters you supply to the function.


CWnd Overview 
[解决办法]
  Class Members 
[解决办法]
  Hierarchy Chart

See Also   CWnd::SetTimer,WM_TIMER

[解决办法]
一言以蔽之,就是在需要启动定时器的地方调用SetTimer
在OnTimer事件响应函数中或WM_TIMER消息处理中干到时间想干的事,
干完事后,
如果以后再也不干了,KillTimer
如果希望隔设定时间后再干一次,在程序退出前KillTimer
需要注意的是:OnTimer事件要想被触发,需要在消息映射中添加如:
BEGIN_MESSAGE_MAP(CmyDlg, CDialog)
    //{{AFX_MSG_MAP(CmyDlg)
    ON_WM_PAINT()
    ON_WM_TIMER()
    ON_WM_DESTROY()
    ON_WM_CLOSE()
    ON_WM_CTLCOLOR()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

热点排行