Basic Steps
- Set the ·TDF_CALLBACK_TIMER· bit in TASKDIALOGCONFIG::dwFlags. This will invoke a callback (if specified) approximately every 200 ms.
- Assign a callback function to
TASKDIALOGCONFIG::pfCallback. - 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 );
本文介绍如何使用Windows API设置任务对话框自动关闭。通过设置TDF_CALLBACK_TIMER标志并指定回调函数,可以实现在设定时间后自动关闭对话框。示例代码展示了如何配置任务对话框,设置超时时间为5秒,并在超时后自动点击确定按钮。
1923

被折叠的 条评论
为什么被折叠?



