用SetTimer(NULL, 0, dwInterval, (TIMERPROC)MyTimerProc)生成定时器以后,并不能进入我的定时器处理函数,必须填入窗口句柄和自定义的定时器消息,系统才能回调我的定时器处理函数,假设我程序中没有窗口,这个定时器应该怎么设?
VOID CALLBACK MyTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)请解惑!最好能有说明。

解决方案 »

  1.   

    这个TIMER事件是针对窗口的,如果没有窗口,将不会起作用。
      

  2.   

    我试过了,没有问题。::SetTimer(NULL,0,1000,MyFunc); void CALLBACK MyFunc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime )
    {
    AfxMessageBox("test");
    }
      

  3.   

    没有窗口的WINDOW程序我没见过,不知道怎么做Parameters
    hWnd 
    Handle to the window to be associated with the timer. This window must be owned by the calling thread. If this parameter is NULL, no window is associated with the timer and the nIDEvent parameter is ignored. 
    nIDEvent 
    Specifies a nonzero timer identifier. If the hWnd parameter is NULL, this parameter is ignored. Res
    An application can process WM_TIMER messages by including a WM_TIMER case statement in the window procedure or by specifying a TimerProc callback function when creating the timer. When you specify a TimerProc callback function, the default window procedure calls the callback function when it processes WM_TIMER. Therefore, you need to dispatch messages in the calling thread, even when you use TimerProc instead of processing WM_TIMER. The wParam parameter of the WM_TIMER message contains the value of the nIDEvent parameter所以我认为没有窗口连接该消息会被忽略
      

  4.   

    我有一个全局Timer的源程序,想要就说一声,不过和你的要求是不相同的
      

  5.   

    An application can process WM_TIMER messages by including a WM_TIMER case statement in the window procedure or by specifying a TimerProc callback function when creating the timer. When you specify a TimerProc callback function, the default window procedure calls the callback function when it processes WM_TIMER. Therefore, you need to dispatch messages in the calling thread, even when you use TimerProc instead of processing WM_TIMER. 当第四个参数为回调函数时,第一个参数也需要设置窗口句柄,因为这个回调
    函数是缺省窗口处理过程所调用的,所以WM_TIMER消息与窗口句柄密不可分。
    试想,没有这个窗口句柄,系统就不知道这个WM_TIMER事件所属的线程,当然就
    不知道向哪个线程发送这个WM_TIMER事件。至于“假设我程序中没有窗口,这个定时器应该怎么设?“我想是没有办法的。
      

  6.   

    不管怎样谢谢txdxun(),我的EMAIL: [email protected]
      

  7.   

    to  vc_boy():#include "windows.h"void CALLBACK MyTimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime )
    {
    MessageBox(NULL, "定时器时间到!", "通知", MB_OK);
    }int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
    ::SetTimer(NULL, 0, 1000, (TIMERPROC)MyTimerProc);
    while(true)
    Sleep(10); return 0;
    }我也试了一下,这个程序仍然不能触发定时器消息!
      

  8.   

    别用Callback了,用全局定时器,即SetTimer的HWND参数置为NULL,并请注意第二个参数和返回值与HWND!=NULL时是不一样的,说明参考MSDN。没有窗口也是可以有消息循环的,举例如下(Win32 Console Application):#include "stdafx.h"
    #include <Windows.h>int main ( void )
    {
    MSG msg;
    DWORD id; id = SetTimer ( NULL, 0, 1000, NULL ); while ( GetMessage(&msg, 0, 0, 0) )
    {
    if ( msg.message == WM_TIMER )
    {
    printf ( "WM_TIMER\n" );
    break;
    }
    // TranslateMessage ( &msg );
    DispatchMessage ( &msg );
    } return 0;
    }
      

  9.   

    别用Callback了,用全局定时器,即SetTimer的HWND参数置为NULL,并请注意第二个参数和返回值与HWND!=NULL时是不一样的,说明参考MSDN。没有窗口也是可以有消息循环的,举例如下(Win32 Console Application):#include "stdafx.h"
    #include <Windows.h>int main ( void )
    {
    MSG msg;
    DWORD id; id = SetTimer ( NULL, 0, 1000, NULL ); while ( GetMessage(&msg, 0, 0, 0) )
    {
    if ( msg.message == WM_TIMER )
    {
    printf ( "WM_TIMER\n" );
    break;
    }
    // TranslateMessage ( &msg );
    DispatchMessage ( &msg );
    } KillTimer ( NULL, id ); return 0;
    }
      

  10.   

    终于明白了,就算你指定了回调函数,还需要往窗口消息处理过程中发送WM_TIMER消息。
    When you specify a TimerProc callback function, the default window procedure calls the callback function when it processes WM_TIMER. Therefore, you need to dispatch messages in the calling thread, even when you use TimerProc instead of processing WM_TIMER. 
    散分了!
      

  11.   

    #include "windows.h"void CALLBACK MyTimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime )
    {
    MessageBox(NULL, "定时器时间到!", "通知", MB_OK);
    }int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
    MSG msg; ::SetTimer(NULL, 0, 1000, (TIMERPROC)MyTimerProc);
    PostMessage(NULL, WM_TIMER, 0, 0);
    while ( GetMessage(&msg, 0, 0, 0))
    {
    DispatchMessage(&msg);
    } return 0;
    }终于生效了!
      

  12.   

    用线程。把while(run)
    {
      WaitForSingleObject(hEvent,time)
     ///do something }
    //////////////////////////////////////////////////////////////////
      学习,再学习。