问题描述
我的问题是正常的mfc SetTimer,如下
My question is for normal mfc SetTimer, as follows
void CTimersDlg::OnButtonBegin()
{
// create the timer
SetTimer(m_nTimerID, uElapse, NULL);
}
void CTimersDlg::OnButtonStop()
{
// destroy the timer
KillTimer(m_nTimerID);
}
void CTimersDlg::OnTimer(UINT nIDEvent) // called every uElapse milliseconds
{
// do something, but quickly
CDialog::OnTimer(nIDEvent);
}
但如果我需要在非dialog.cpp中使用SetTimer,我的sender.cpp
如何创建计时器?如在SetTimer字段中,处理程序(回调)函数?
but if I need to use SetTimer in non dialog.cpp, for example in my sender.cpp how do I create the timer? As in the SetTimer fields, the handler(callback) function?
推荐答案
您可以传递NULL作为窗口句柄,并包含回调函数调用 SetTimer
。这将允许您接收计时器通知,而无需将其与特定窗口相关联。
You can pass NULL as the window handle and include callback function in the call to SetTimer
. This will allow you to receive timer notifications without associating it with a specific window.
如果计时器用于单独的工作线程窗口),您仍然需要处理消息队列以接收计时器通知。如果你使用 CWinThread
对象创建一个线程,这已经在你的 CWinThread :: Run
。
If the timer is intended to be used in a separate "worker" thread (one without a window) you will still need to process the message queue in order to receive timer notifications. If you are creating a thread using a CWinThread
object this is already handled for you in the default implementation of CWinThread::Run
.
如果您可以更新您的问题,以包含有关 sender.cpp
内容的更多信息,一个更合适的例子。这使用纯Windows API来创建计时器并处理所需的分派队列。
If you can update your question to include more information about the contents of sender.cpp
I can provide a more suitable example. This uses the plain Windows API to create a timer and handle the required dispatch queue.
// Example only.
VOID CALLBACK timerCallback(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
printf("Timer called\n");
}
void SomeFunc()
{
SetTimer(NULL, 1, 1000, timerCallback);
MSG msg;
// msg-pump
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
这篇关于如何在我的mfc应用程序中的非对话框.cpp中使用SetTimer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!