Message Boxes in C++

This simple tutorial will teach you how to create message boxes in C++.

Note: The following is for people who already know C++.

In case you don’t know what message boxes are, here are some examples:

There are only two things needed to create Message Boxes:

  1. Include the “windows.h” library:
  2. Use this function :

So, here is the complete code to generate a message box:

The above code generates this message box:

Simple… right? But what does the “MB_OK” mean? All it does is tell the Message Box to make a button that says “OK.” You can make the button on the Message Box say different things.


Here is the complete list:


  • MB_ABORTRETRYIGNORE

  • MB_CANCELTRYCONTINUE

  • MB_HELP

  • MB_OK

  • MB_OKCANCEL

  • MB_RETRYCANCEL

  • MB_YESNO
  • MB_YESNOCANCEL

Go ahead! Try them! Replace the part in the code that say “MB_OK” with any of these and see what you get! Try them all!

Ok, so now you know how to make buttons. But what about the icons like these?: ? To make them, you add any of these:

  • MB_ICONEXCLAMATION:

  • MB_ICONWARNING:

  • MB_ICONINFORMATION:

  • MB_ICONASTERISK:

  • MB_ICONQUESTION:

  • MB_ICONSTOP:

  • MB_ICONERROR:

  • MB_ICONHAND:

Here is an example of a Message Box with icons:>

The above code generates this Message Box:

But what if you want to make a combination of buttons with icons? What if you want MB_YESNO as the button and MB_ICONERROR as the icon? The answer is simple, you would use “MB_YESNO | MB_ICONERROR”.


Here is an example:

The above code generates the following Message Box:

Yay, now you know how to create Message Boxes but how do you know if they press a button in the Message Box?


Here is an example of how you check:

As you can see, the code Checks if you clicked the button yes (shown as IDYES) and writes “you pressed yes.” If you clicked no, it writes “you pressed no.” here is the list of the possible buttons.

  • IDABORT: Checks if you pressed the button “Abort”

  • IDCANCEL: Checks if you pressed the button “Cancel”
  • IDCONTINUE: Checks if you pressed the button “Continue”

  • IDIGNORE: Checks if you pressed the button “ignore”

  • IDNO: (Checks if you pressed the button “No”

  • IDOK: Checks if you pressed the button “OK”

  • IDRETRY: Checks if you pressed the button “Retry”

  • IDTRYAGAIN: Checks if you pressed the button “Try Again”

  • IDYES: Checks if you pressed the button “Yes”

Yay, you are now an expert with using Message Boxes. I hope you liked this tutorial!

Leave Your Response