用SetTimer(NULL, 0, dwInterval, (TIMERPROC)MyTimerProc)生成定时器以后,并不能进入我的定时器处理函数,必须填入窗口句柄和自定义的定时器消息,系统才能回调我的定时器处理函数,假设我程序中没有窗口,这个定时器应该怎么设?
VOID CALLBACK MyTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)请解惑!最好能有说明。
VOID CALLBACK MyTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)请解惑!最好能有说明。
解决方案 »
- 可否动态的定义变量呢?
- 如何添加No base class的类?
- 跨平台GUI 窗体设计器1.7 by [email protected](目前命名为RCEditor)
- VC6.0非MFC的动态库中使用CString,编译不过(带错误信息)
- 求window消息及消息参数大全,关键是后面的参数
- 对话框中控件能否与Tab Control子对话框中控件的焦点次序串成一个循环?
- 怎么样静态切分窗口呀?
- 大量读取,计算操作画面刷新问题
- 请教static_cast的用法
- 搞了好久弄不出来,mfc 获取编辑框控件输入中间有 '\r ' 的出现???
- 简单--关于WriteFile函数问题(在线等待)
- 在vc++.net中初始做化数组?!比如a[4][4]总是初始化为a[4]?
{
AfxMessageBox("test");
}
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所以我认为没有窗口连接该消息会被忽略
函数是缺省窗口处理过程所调用的,所以WM_TIMER消息与窗口句柄密不可分。
试想,没有这个窗口句柄,系统就不知道这个WM_TIMER事件所属的线程,当然就
不知道向哪个线程发送这个WM_TIMER事件。至于“假设我程序中没有窗口,这个定时器应该怎么设?“我想是没有办法的。
{
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;
}我也试了一下,这个程序仍然不能触发定时器消息!
#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;
}
#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;
}
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.
散分了!
{
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;
}终于生效了!
{
WaitForSingleObject(hEvent,time)
///do something }
//////////////////////////////////////////////////////////////////
学习,再学习。