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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

responding to system message box 1

Status
Not open for further replies.

TalosBlack

Programmer
Jan 6, 2004
33
0
0
US
I am having trouble responding to system message boxes. I am writting a program that causes Windows (2000) to basically say are you sure you want to continue? YES/NO. What do i need to do in my program to get it to respond YES? (Or NO or OK or whatever but in this case YES) Thanks a lot for any help you may be able to offer.

John
 
if(IDYES == MessageBox(NULL,"something",MB_YesNo)
// do something

I might have forgotten one of the parameters or something, but you should get the general idea. Hope it helped

bdiamond
 
I may have mis represented what i want to do. If i understand your code correctly it makes a YesNo message boxand does something when someone clicks yes. I would like to respond to a windows message box that is already created with a Yes without having to click on the yes. (Probablly without seeing the message box because IDYES would be set quicker then i would see the box.)

Simplfied Example If you click on a file and then hit the delete key windows says are you sure you want to delete this file. So my program does something like deleting a file automatically and i don't want the user to be asked are they sure. I just want to message box to be set to IDYES. I hope that is more clear. Any further help is vastly appreciated.

John
 
here's some code that I found on the msdn. You should just about be able to cut and paste this code. Except in WinMain function where it calls the TimedMessageBox() function, you just put in 0 instead of 5000. Hope this works! (BTW, you can find this article on the msdn entitled: 'HOWTO: Create a Timed MessageBox')

/***********************************************************************
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

Copyright 1998 Microsoft Corporation. All Rights Reserved.
***********************************************************************/

/***********************************************************************
*
* MsgBox.c
*
* Abstract:
*
* Sample program to demonstrate how a program can display a
* timed message box.
*
**********************************************************************/

#define STRICT
#include <windows.h>


/**********************************************************************
*
* Overview
*
* The key to creating a timed message box is exiting the dialog
* box message loop internal to the message box. Since the
* message loop for a message box is part of USER, you cannot
* modify it without using hooks and other such methods.

*
* However, all message loops exit when they receive a
* WM_QUIT message. Furthermore, a nested message loop, if it
* receives a WM_QUIT message, must break the loop and then re-post
* the quit message so that the next outer layer can process it.
*
* Therefore, you can get the nested message loop to exit by
* calling PostQuitMessage(). The nested message loop will
* clean up and post a new quit message. When the MessageBox
* returns, you peek to see if there is a quit message. If so,
* then it means that the message loop was abnormally terminated.
* You also consume the WM_QUIT message instead of re-posting it
* so that the application continues running.
*
* Essentially, you have "tricked" the nested message loop into
* thinking that the application is terminating. When it returns,
* you "eat" the quit message, effectively canceling the fake
* quit that you generated.
*
**********************************************************************/

/**********************************************************************
*
* MessageBoxTimer
*
* The timer callback function that posts the fake quit message,
* which causes the message box to exit because it thinks that
* the application is exiting.
*

**********************************************************************/

void CALLBACK
MessageBoxTimer(HWND hwnd, UINT uiMsg, UINT idEvent, DWORD dwTime)
{
PostQuitMessage(0);
}

/***********************************************************************
*
* TimedMessageBox
*
* The same as the standard MessageBox, except it also accepts
* a timeout. If the user does not respond within the
* specified timeout, then the value 0 is returned instead
* of one of the ID* values.
*
**********************************************************************/

UINT
TimedMessageBox(
HWND hwndParent,
LPCTSTR ptszMessage,
LPCTSTR ptszTitle,
UINT flags,
DWORD dwTimeout)
{
UINT idTimer;
UINT uiResult;
MSG msg;

/*
* Set a timer to dismiss the message box.
*/
idTimer = SetTimer(NULL, 0, dwTimeout, (TIMERPROC)MessageBoxTimer);

uiResult = MessageBox(hwndParent, ptszMessage, ptszTitle, flags);

/*
* Finished with the timer.
*/
KillTimer(NULL, idTimer);

/*
* See if there is a WM_QUIT message in the queue. If so,
* then you timed out. Eat the message so you don't quit the
* entire application.
*/
if (PeekMessage(&msg, NULL, WM_QUIT, WM_QUIT, PM_REMOVE)) {

/*
* If you timed out, then return zero.
*/
uiResult = 0;
}

return uiResult;
}

/***********************************************************************
*
* WinMain
*
* Program entry point.
*
* Demonstrate TimedMessageBox().
*
**********************************************************************/

int WINAPI
WinMain(
HINSTANCE hinst,
HINSTANCE hinstPrev,
LPSTR pszCmdLine,
int nCmdShow)
{

UINT uiResult;

/*
* Ask the user a question, and give him or her five seconds to
* answer it.
*/

uiResult = TimedMessageBox(NULL, "Does a triangle have three
sides?",
"Quiz", MB_YESNO, 5000); // NULL first parameter is important.


switch (uiResult) {
case IDYES:
MessageBox(NULL, "That's right!", "Result", MB_OK);
break;

case IDNO:
MessageBox(NULL, "Believe it or not, triangles "
"really do have three sides.", "Result",
MB_OK);
break;

case 0:
MessageBox(NULL, "I sensed some hesitation there. "
"The correct answer is Yes.", "Result", MB_OK);
break;
}

return 0;
}


bdiamond
 
I'll have to do some reading thanks for your help. I did try to look on msdn but couldn't find what i needed hopefully this will be it or at least put me on the right track.

Thanks

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top