如何在我的 mfc 应用程序的非对话框 .cpp 中使用 SetTimer?

发布于 2024-11-25 05:41:44 字数 503 浏览 0 评论 0原文

我的问题是针对普通的 mfc SetTimer,如下所示

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 字段中一样,处理程序(回调)函数?

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);
}

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱本泡沫多脆弱 2024-12-02 05:41:44

您可以传递 NULL 作为窗口句柄,并在对 SetTimer 的调用中包含回调函数。这将允许您接收计时器通知,而无需将其与特定窗口关联。

如果计时器打算在单独的“工作”线程(没有窗口的线程)中使用,您仍然需要处理消息队列才能接收计时器通知。如果您使用 CWinThread 对象创建线程,则在 CWinThread::Run 的默认实现中已经为您处理了这一点。

如果您可以更新您的问题以包含有关 sender.cpp 内容的更多信息,我可以提供一个更合适的示例。这使用普通的 Windows API 来创建计时器并处理所需的调度队列。

// 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);
    }
}

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.

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.

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);
    }
}
要走就滚别墨迹 2024-12-02 05:41:44

SetTimer 与窗口关联,需要窗口句柄才能使用它。对于非窗口类,您应该考虑使用 CreateWaitableTimer 或使用 定时器队列

SetTimer is associated with windows, you need windows handle to use it. For non window classes you should consider using CreateWaitableTimer or use TimerQueue

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文