Building Graphical User Interfaces (GUIs)
Using the Windows Application Programming Interface (API)
All of these examples assume that Microsoft Visual C++ 2010 or 2012 is the compiler being used.
Exercise 1: A very simple first GUI program
There is a tradition among C programmers that your first program should do nothing but
print "Hello, world" on the screen. Here you will follow that tradition.
If you have not already created the FirstWinApp project then click
back on your browser and click the link Creating Projects in Visual Studio
for GUI applications and follow the instructions for creating the project
for exercise 1.
Add a .cpp source file to the project. (Right click Source Files and select
Add->New item. In the dialog box select .cpp file and give it a name
like FirstWinAppMain.cpp.
Copy and paste the following text into the .cpp file changing John Doe
to your name:
Compile and run the application. You should see a small message box like the
one below:
What's it all mean?
#include <windows.h> gives you the basic Windows library
WINAPI specifies the calling convention. Dont worry about it.
Just accept that it is needed for a Windows app.
WinMain() Windows equivalent of main()
HINSTANCE hInstance Handle to the program's executable module (the .exe file
in memory). You will see how to use this later. For now just ignore it.
HINSTANCE hPrevInstance Always NULL for Win32 programs. Used to be the
handle to the previously run instance of your program (if any) in Win16. This no
longer applies. In Win32 you ignore this parameter.
LPSTR lpCmdLine The command line arguments, if any, as a single string. NOT including
the program name.
int nCmdShow An integer value which may be passed to ShowWindow(). You will
get to this later.
MessageBox() Function to call to display a simple message box. The args are:
a. NULL Means there is no parent window owning this dialog.
b. "Hello, world!" Message to display. If you didnt set the
Character Set to "Not Set" when creating the project as explained above
then you have to put an L in front of the opening quote. The L
means that this string should be interpreted as a wide char (wchar_t or
Unicode character) string instead of a plain char string. MessageBox
expects this.
c. "Note" Title of the window appearing in the title bar.
d. MB_OK | MB_ICONEXCLAMATION A flag which does a bitwise OR
of two flag values to tell MessageBox to display an OK button and use
the exclamation point icon.
Now try modifying this "Hello, World" code:
Change the text of the message and note that the message box always resizes to
accomodate the text.
Try inserting the '\n' character to create multi-line messages.
Try different combinations for the fourth argument to the MessageBox function call
MB_ABORTRETRYIGNORE - The message box contains three push buttons:
Abort, Retry, and Ignore.
MB_CANCELTRYCONTINUE - Microsoft Windows 2000/XP: The message box
contains three push buttons: Cancel, Try Again, Continue. Use this message box
type instead of MB_ABORTRETRYIGNORE.
MB_HELP - Windows 95/98/Me, Windows NT 4.0 and later: Adds a
Help button to the message box. When the user clicks the Help button or
presses F1, the system sends a WM_HELP message to the owner.
MB_OK - The message box contains one push button: OK.
This is the default.
MB_OKCANCEL - The message box contains two push buttons:
OK and Cancel.
MB_RETRYCANCEL - The message box contains two push buttons:
Retry and Cancel.
MB_YESNO - The message box contains two push buttons: Yes and No.
MB_YESNOCANCEL - The message box contains three push buttons:
Yes, No, and Cancel. To display an icon in the message box, specify one of
the following values.
MB_ICONEXCLAMATION - An exclamation-point icon appears in the
message box.
MB_ICONWARNING - An exclamation-point icon appears in the message box.
MB_ICONINFORMATION - An icon consisting of a lowercase letter
i in a circle appears in the message box.
MB_ICONASTERISK - An icon consisting of a lowercase
letter i in a circle appears in the message box.
MB_ICONQUESTION - A question-mark icon appears in the
message box. The question-mark message icon is no longer recommended
because it does not clearly represent a specific type of message and
because the phrasing of a message as a question could apply to any
message type. In addition, users can confuse the message symbol question
mark with Help information. Therefore, do not use this question mark
message symbol in your message boxes. The system continues to support
its inclusion only for backward compatibility.
MB_ICONSTOP - A red circle icon with an X on it appears
in the message box and a warning tone is sounded.
MB_ICONERROR - A red circle icon with an X on it appears
in the message box and a warning tone is sounded.
MB_ICONHAND - A red circle icon with an X on it appears
in the message box and a warning tone is sounded.
To indicate the default button, specify one of the following values.
MB_DEFBUTTON1 - The first button is the default button.
MB_DEFBUTTON1 is the default unless MB_DEFBUTTON2, MB_DEFBUTTON3, or
MB_DEFBUTTON4 is specified.
MB_DEFBUTTON2 - The second button is the default button.
MB_DEFBUTTON3 - The third button is the default button.
MB_DEFBUTTON4 - The fourth button is the default button.
For more information about the various flags and use of MessageBox check the
MSDN MessageBox link.
This may not seem like much of an exercise, but you have taken a big step forward. Now you know how to
display a message box any time you need to in a Windows GUI program.