使用代码关闭CTaskDialog窗口

本文介绍如何使用Windows API设置任务对话框自动关闭。通过设置TDF_CALLBACK_TIMER标志并指定回调函数,可以实现在设定时间后自动关闭对话框。示例代码展示了如何配置任务对话框,设置超时时间为5秒,并在超时后自动点击确定按钮。
部署运行你感兴趣的模型镜像

Basic Steps

  1. Set the ·TDF_CALLBACK_TIMER· bit in TASKDIALOGCONFIG::dwFlags. This will invoke a callback (if specified) approximately every 200 ms.
  2. Assign a callback function to TASKDIALOGCONFIG::pfCallback.
  3. When the callback gets called with the TDN_TIMER notification code, compare the elapsed time that is passed to the callback via wParam, with your desired timeout value. To close the dialog, send it a TDM_CLICK_BUTTON message.

You can go fancy and display a progress bar (TDF_SHOW_PROGRESS_BAR) or just show a countdown text that you update in the timer callback. See Task Dialog Messages.

Example

Here is an example using plain windows API. MFC has the CTaskDialog class, but I think this answer will be more useful if it doesn’t depend much on MFC. For non-MFC users, only the assignments to tc.hInstance and tc.hwndParent need to be changed.

TASKDIALOGCONFIG tc{ sizeof(tc) };
tc.hInstance = AfxGetInstanceHandle();
tc.hwndParent = GetSafeHwnd();  // assuming you call this from a dialog member function
tc.dwFlags = TDF_CALLBACK_TIMER | TDF_SIZE_TO_CONTENT;
tc.dwCommonButtons = TDCBF_OK_BUTTON;
tc.pszWindowTitle = L"Task dialog with timeout";
tc.pszContent = L"This dialog will close after 5 seconds!";

DWORD timeout = 5000;  // milliseconds
tc.lpCallbackData = reinterpret_cast<LONG_PTR>( &timeout );

// Assign a lambda function as callback.
tc.pfCallback = []( HWND hwnd, UINT uNotification, WPARAM wParam, LPARAM lParam, LONG_PTR dwRefData )
{
    if( uNotification == TDN_TIMER )
    {
        DWORD* pTimeout = reinterpret_cast<DWORD*>( dwRefData );  // = tc.lpCallbackData
        DWORD timeElapsed = static_cast<DWORD>( wParam );
        if( *pTimeout && timeElapsed >= *pTimeout )
        {
            *pTimeout = 0; // Make sure we don't send the button message multiple times.
            SendMessage( hwnd, TDM_CLICK_BUTTON, IDOK, 0 );
        }
    }
    return S_OK;
};

::TaskDialogIndirect( &tc, nullptr, nullptr, nullptr );

参考

https://stackoverflow.com/questions/51985841/custom-message-box-that-automatically-disappears/51989826#51989826

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值