#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,
"Is there a question mark in this MessageBox?",
" You have 10 seconds to answer",
MB_YESNO,
10000, 12345, hIcon );
switch ( mb )
{ case IDYES : printf ( "YES!!\n" );
break;
case IDNO : printf ( "no ...\n" );
break;
case 12345 : printf ( "Please respond within 10 seconds next time\n" );
break;
default : printf ( "???\n" );
break; } }