Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <progrock/winapi/String.h> // String
- #include <progrock/winapi/failure/throwWinX.h> // throwWinX, ErrorCode
- #include <progrock/headerwrappers/winsdk/commctrl_h.h> // TaskDialogIndirect
- #include <progrock/headerwrappers/winsdk/windows_h.h> // MessageBox
- #include <progrock/headerwrappers/winsdk/windows_h.h>
- #include <progrock/cppx/macro/is_unused.h> // CPPX_IS_INTENTIONALLY_UNUSED
- #undef WINAPI_TASKDIALOG_AVAILABLE
- #if _WIN32_WINNT < _WIN32_WINNT_VISTA || !defined( TD_INFORMATION_ICON )
- # define WINAPI_VISTA_TASKDIALOG_AVAILABLE 0
- #else
- # define WINAPI_VISTA_TASKDIALOG_AVAILABLE 1
- #endif
- namespace progrock{ namespace winapi{
- inline void infoMessageBox(
- HWND const parentWindow,
- String const& boxTitle,
- String const& heading,
- String const& msg
- )
- {
- static auto const spaced = []( String const& s ) -> String
- {
- String result;
- for( auto const c : s )
- {
- if( size( result ) > 0 ) { result += L" "; }
- result += c;
- }
- return result;
- };
- DWORD const options = 0
- | MB_ICONINFORMATION
- | (parentWindow == 0? MB_SETFOREGROUND | MB_TASKMODAL : 0);
- String const s = (heading.size() == 0? msg : spaced( heading ) + L"\n\n" + msg);
- int const choice = MessageBox( parentWindow, s.cString(), boxTitle.cString(), options );
- hopefully( choice != 0 )
- || throwWinX(
- ErrorCode( ::GetLastError() ),
- L"winapi::infoMessageBox: MessageBox failed"
- );
- }
- #if WINAPI_VISTA_TASKDIALOG_AVAILABLE
- inline void infoTaskDialog(
- HWND const parentWindow,
- String const& boxTitle,
- String const& heading,
- String const& msg
- )
- {
- TASKDIALOGCONFIG params = {sizeof( params )};
- params.hwndParent = parentWindow;
- params.hInstance = GetModuleHandle( 0 );
- params.dwFlags = TDF_SIZE_TO_CONTENT;
- params.dwCommonButtons = TDCBF_OK_BUTTON;
- params.pszWindowTitle = boxTitle.cString();
- params.pszMainIcon = TD_INFORMATION_ICON;
- params.pszMainInstruction = heading.cString();
- params.pszContent = msg.cString();
- params.nDefaultButton = IDOK;
- HRESULT const hr = TaskDialogIndirect( ¶ms, nullptr, nullptr, nullptr );
- if( FAILED( hr ) )
- {
- throwWinX( ResultCode( hr ), L"vistaInfoBox: TaskDialogIndirect failed" );
- }
- }
- inline void infoBox(
- HWND const parentWindow,
- String const& boxTitle,
- String const& heading,
- String const& msg
- )
- {
- infoTaskDialog( parentWindow, boxTitle, heading, msg );
- }
- #else
- inline void infoBox(
- HWND const parentWindow,
- String const& boxTitle,
- String const& heading,
- String const& msg
- )
- {
- infoMessageBox( parentWindow, boxTitle, heading, msg );
- }
- #endif
- inline void infoBox(
- String const& boxTitle,
- String const& heading,
- String const& msg
- )
- {
- infoBox( 0, boxTitle, heading, msg );
- }
- } } // namespace progrock::winapi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement