Programming Tips - C++: ask the user a yes/no question in win32

Date: 2008jun7 Update: 2025jul5 Platform: win32 Language: C/C++ Level: beginner Q. C++: ask the user a yes/no question in win32 A. Use the MessageBox() function with flag MB_YESNO. It will popup a dialog. If the user clicks on "Yes" you get IDYES back and it they click on "No" you get IDNO back. There is a parameter for the caption where the program name traditionally goes. So we can make this handy little function:
BOOL YesNo(const HWND hwnd, LPCSTR szQuestion, LPCSTR szProgramName) { return MessageBox(hwnd, szQuestion, szProgramName, MB_YESNO) == IDYES; } void ExampleUse(const HWND hwnd) { if (YesNo(hwnd, "Do you want to delete that file?", "My Program")) { DeleteFile("c:\\temp\\myfile.txt"); } }