Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MessageBox icon

Status
Not open for further replies.

UmmmOk

Programmer
Aug 1, 2001
3
TR
I am wondering if it is possible to change the icon that is in the taskbar for a MessageBox. I have a dialog-based application and have been able to set the icon for the dialog, but I have a MessageBox that pops up at the end and the icon in the taskbar is the default MFC icon. I do not see anywhere to change this. Any help would be greatly appreciated.


Thanks,

J.
 
Do You mean somewhat like this?
MessageBox(NULL, "Another Icon...", "Message:", MB_OK | MB_ICONINFORMATION);

If You wish to change Icon in the Taskbar, You should change Application Icon before You call MessageBox:
HICON hIcon = LoadIcon(hInstance, (char*)IDI_APPICON); //IDI_APPICON - Icon - ID You need
if(hIcon)
::SendMessage(hWnd, WM_SETICON, TRUE, (LPARAM)hIcon);
 
You need an other thread to do this, because the MessageBox only returns after the user has responded. I did have some code which made it possible to put a time-out on a MessageBox, and I have made some changes for you to support a user-defined icon in the MessageBox. You get the time-out as an extra. If you do not want the time-out feature, specify INFINITE for the MaxTime parameter.

Code:
#include <windows.h>
#include <stdio.h>


struct MyMBStruct { HWND   hWnd;
                    HICON  hIcon;
                    char * pMsg;
                    char * pTitle;
                    UINT   Flags;
                    int    ReturnValue; };


BOOL CALLBACK MyMBEnumChildProc ( HWND hWnd, LPARAM lParam );


int MyMessageBox  ( HWND   hWnd,
                    char * pMsg,
                    char * pTitle,
                    UINT   Flags,
                    DWORD  MaxTime,
                    int    DefaultValue,
                    HICON  hIcon );


int MyMBThread    ( MyMBStruct *MyMB );



BOOL CALLBACK MyMBEnumChildProc ( HWND hWnd, LPARAM lParam )
{ // Forces a click on one of the Message Boxes buttons
  if ( IsWindow ( hWnd ))
     { SendMessage ( hWnd, BM_CLICK, (WPARAM)0, (LPARAM)0 ); }
  return TRUE; }



int MyMessageBox  ( HWND   hWnd,
                    char * pMsg,
                    char * pTitle,
                    UINT   Flags,
                    DWORD  MaxTime,
                    int    DefaultValue,
                    HICON  hIcon )

{ MyMBStruct MyMB;
  MyMB.hWnd        = hWnd;
  MyMB.hIcon       = hIcon;
  MyMB.pMsg        = pMsg;
  MyMB.pTitle      = pTitle;
  MyMB.Flags       = Flags;
  MyMB.ReturnValue = DefaultValue;


  DWORD  dwThreadID;
  HANDLE hThread = CreateThread ( NULL, 0,
                                  (LPTHREAD_START_ROUTINE)MyMBThread,
                                  &MyMB, 0, &dwThreadID );
  if ( hThread == NULL )
     { return MessageBox ( hWnd, pMsg, pTitle, Flags ); }

  SetThreadPriority ( hThread, THREAD_PRIORITY_HIGHEST );

  HWND hMB = NULL;
  BOOL fMessageBoxCreated = FALSE;
  DWORD dwCount = 20;  // Messagebox should be created in 20 retries
                       // with .05 second of sleep in between. If after
                       // this time no message box is present,
                       // assume failure

  while ( dwCount && !fMessageBoxCreated )
     { Sleep ( 50 );
       dwCount--;
       hMB = FindWindow ( (LPCTSTR)((DWORD)32770), MyMB.pTitle );
       if ( hMB != NULL ) fMessageBoxCreated = TRUE; }

  if ( !fMessageBoxCreated )
     { TerminateThread ( hThread, 0 );
       CloseHandle ( hThread );
       return MessageBox ( hWnd, pMsg, pTitle, Flags ); }

  if ( MyMB.hIcon != NULL )
     { SendMessage ( hMB, 
                     WM_SETICON,
                     (WPARAM)(BOOL)(TRUE),
                     (LPARAM)MyMB.hIcon ); }

  DWORD fWait = WaitForSingleObject ( hThread, MaxTime );

  if ( fWait == WAIT_TIMEOUT )
     { while ( EnumChildWindows ( hMB, (WNDENUMPROC)MyMBEnumChildProc,
                                  (LPARAM)0 )) { /* do nothing */ }
       WaitForSingleObject ( hThread, INFINITE );
       CloseHandle ( hThread );
       return DefaultValue; }

  CloseHandle ( hThread );
  return MyMB.ReturnValue; }

int MyMBThread    ( MyMBStruct *MyMB )
{ MyMB->ReturnValue = MessageBox ( MyMB->hWnd,
                                   MyMB->pMsg,
                                   MyMB->pTitle,
                                   MyMB->Flags );
  return MyMB->ReturnValue; }



void main ( )
{ HICON hIcon = LoadIcon ( NULL, IDI_QUESTION );
  int mb = MyMessageBox ( NULL,
                          &quot;Is there a question mark in this MessageBox?&quot;,
                          &quot;          You have 10 seconds to answer&quot;,
                          MB_YESNO,
                          10000, 12345, hIcon );
  switch ( mb )
     { case IDYES : printf ( &quot;YES!!\n&quot; );
                    break;
       case IDNO  : printf ( &quot;no ...\n&quot; );
                    break;
       case 12345 : printf ( &quot;Please respond within 10 seconds next time\n&quot; );
                    break;
       default    : printf ( &quot;???\n&quot; );
                    break; } }


Marcel
 
Thank you very much for your help. I feel much more comfortable with this material now. I may have answered my own question though somehow. I burned my application to a CD and ran it off of that and the MessageBox had the custom icon loaded in the taskbar that I wanted rather than the default MFC icon. I don't know why this happened, but I'll be sure to look over your code and see if I accidentally implemented any of that without knowledge of it.


Thanks again,

J.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top