使用代码关闭CTaskDialog窗口

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值