Home > Windows API > A MessageBox with your own icon, wow!!

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…

// 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 );
Advertisements
  1. July 1, 2009 at 3:09 am

    interesting post, will come back here, bookmarked your site

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: