Advertisement
Guest User

Untitled

a guest
Nov 24th, 2012
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <progrock/winapi/String.h>                         // String
  4. #include <progrock/winapi/failure/throwWinX.h>              // throwWinX, ErrorCode
  5.  
  6. #include <progrock/headerwrappers/winsdk/commctrl_h.h>      // TaskDialogIndirect
  7. #include <progrock/headerwrappers/winsdk/windows_h.h>       // MessageBox
  8.  
  9. #include <progrock/headerwrappers/winsdk/windows_h.h>
  10.  
  11. #include <progrock/cppx/macro/is_unused.h>          // CPPX_IS_INTENTIONALLY_UNUSED
  12.  
  13.  
  14. #undef WINAPI_TASKDIALOG_AVAILABLE
  15. #if _WIN32_WINNT < _WIN32_WINNT_VISTA || !defined( TD_INFORMATION_ICON )
  16. #   define WINAPI_VISTA_TASKDIALOG_AVAILABLE  0
  17. #else
  18. #   define WINAPI_VISTA_TASKDIALOG_AVAILABLE  1
  19. #endif
  20.  
  21. namespace progrock{ namespace winapi{
  22.  
  23.     inline void infoMessageBox(
  24.         HWND const          parentWindow,
  25.         String const&       boxTitle,
  26.         String const&       heading,
  27.         String const&       msg
  28.         )
  29.     {
  30.         static auto const spaced = []( String const& s ) -> String
  31.         {
  32.             String result;
  33.             for( auto const c : s )
  34.             {
  35.                 if( size( result ) > 0 ) { result += L" "; }
  36.                 result += c;
  37.             }
  38.             return result;
  39.         };
  40.  
  41.         DWORD const options = 0
  42.             | MB_ICONINFORMATION
  43.             | (parentWindow == 0? MB_SETFOREGROUND | MB_TASKMODAL : 0);
  44.         String const s = (heading.size() == 0? msg : spaced( heading ) + L"\n\n" + msg);
  45.  
  46.         int const choice = MessageBox( parentWindow, s.cString(), boxTitle.cString(), options );
  47.         hopefully( choice != 0 )
  48.             || throwWinX(
  49.                 ErrorCode( ::GetLastError() ),
  50.                 L"winapi::infoMessageBox: MessageBox failed"
  51.                 );
  52.     }
  53.  
  54. #if WINAPI_VISTA_TASKDIALOG_AVAILABLE
  55.     inline void infoTaskDialog(
  56.         HWND const          parentWindow,
  57.         String const&       boxTitle,
  58.         String const&       heading,
  59.         String const&       msg
  60.         )
  61.     {
  62.         TASKDIALOGCONFIG params     = {sizeof( params )};
  63.         params.hwndParent = parentWindow;
  64.         params.hInstance = GetModuleHandle( 0 );
  65.         params.dwFlags = TDF_SIZE_TO_CONTENT;
  66.         params.dwCommonButtons = TDCBF_OK_BUTTON;
  67.         params.pszWindowTitle = boxTitle.cString();
  68.         params.pszMainIcon = TD_INFORMATION_ICON;
  69.         params.pszMainInstruction = heading.cString();
  70.         params.pszContent = msg.cString();
  71.         params.nDefaultButton = IDOK;
  72.         HRESULT const hr = TaskDialogIndirect( &params, nullptr, nullptr, nullptr );
  73.         if( FAILED( hr ) )
  74.         {
  75.             throwWinX( ResultCode( hr ), L"vistaInfoBox: TaskDialogIndirect failed" );
  76.         }
  77.     }
  78.  
  79.     inline void infoBox(
  80.         HWND const          parentWindow,
  81.         String const&       boxTitle,
  82.         String const&       heading,
  83.         String const&       msg
  84.         )
  85.     {
  86.         infoTaskDialog( parentWindow, boxTitle, heading, msg );
  87.     }
  88. #else
  89.     inline void infoBox(
  90.         HWND const          parentWindow,
  91.         String const&       boxTitle,
  92.         String const&       heading,
  93.         String const&       msg
  94.         )
  95.     {
  96.         infoMessageBox( parentWindow, boxTitle, heading, msg );
  97.     }
  98. #endif
  99.  
  100.     inline void infoBox(
  101.         String const&       boxTitle,
  102.         String const&       heading,
  103.         String const&       msg
  104.         )
  105.     {
  106.         infoBox( 0, boxTitle, heading, msg );
  107.     }
  108.  
  109. } }  // namespace progrock::winapi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement