使用代碼關閉CTaskDialogp窗口

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章