My program is a console application.
I want to break when ESC is pressed, but it can't work when I debug it at VS2010.
If I delete the sentence which have MessageBox, it could work.
What's the reason ?
Thanks for the help in advance.
#include <iostream>
#include <Windows.h>
usingnamespace std;
int main()
{
MessageBox(GetConsoleWindow(),TEXT("Press ESC to break!"),TEXT("info"),MB_OK | MB_ICONINFORMATION);
while(true)
{
//Note the use of GetAsyncKeyState function
//and also the test with 0x8000
//
if((GetAsyncKeyState(27) & 0x8000) == 0x8000) //test for escape key
break;
}
cout<<"have breaked!"<<endl;
system("pause");
return 0;
}
MessageBox is a Blocking Function - Unless you specify a Parent Window. It will NOT return until you press a button.
So, first you click a button, THEN it will check for a keypress.
Not if you want to close your MessageBox. You cannot close a MessageBox unless you know its Window Handle (HWND). You only can ask a user input to close it. But you can create your own MessageBox-ish function that creates a new MessageBox and returns also a HWND so you can close it.