A MessageBox with your own icon, wow!!

Windows API provides a messagebox function called MessageBoxIndirect. It takes a MSGBOXPARAMS structure pointer as parameter which contains necessary information to display a dialog box.

Here is the code…

[sourcecode language=’cpp’]// Custom message box function
static int CustomMsgBox( LPCTSTR lpctszMessage_i,
                         LPCTSTR lpctszTitle_i,
                         UINT uFlags,
                         HWND hWndParent_i = 0,
                        LPCTSTR lpctszIconResource_i = 0 )
{
// Flag that indicates custom icon usage
const bool bUseCustomIcon = ( lpctszIconResource_i != 0 );

   // Fill out message box parameters
   MSGBOXPARAMS msgParams = { 0 };
   msgParams.cbSize = sizeof( msgParams );
   msgParams.hwndOwner = hWndParent_i;
   msgParams.dwStyle = uFlags | ( bUseCustomIcon ? MB_USERICON : 0 );
   msgParams.hInstance = ( bUseCustomIcon ? AfxGetInstanceHandle() : 0 );
   msgParams.lpszCaption = lpctszTitle_i;
   msgParams.lpszText = lpctszMessage_i;
   msgParams.lpszIcon = lpctszIconResource_i;

   // Show message box
   return MessageBoxIndirect( &msgParams );
}

// To show a message box with custom ICON
CustomMsgBox( _T( “I am Nibu babu thomas” ),
_T( “Who am I” ),
MB_OK,
AfxGetMainWnd()->GetSafeHwnd(),
MAKEINTRESOURCE( IDR_MAINFRAME ));

// To show a normal message box with standard icons
CustomMsgBox( _T( “I am Nibu babu thomas” ),
_T( “Who am I” ),
MB_OK | MB_ICONINFORMATION,
hSomeParentWindow );[/sourcecode]

One thought on “A MessageBox with your own icon, wow!!

Appreciate your comments...