Hello2
Now that we have the basics down, it's time to upgrade Hello1 to something you can at least see. Because many Windows CE systems don't have the console driver, Hello2 creates a message box with the "Hello CE" text instead of using printf. Hello2 is shown in Listing 1-2.Listing 1-2: Hello2, a simple Windows application using the MessageBox function
Hello2.cpp
//======================================================================
// Hello2 - A simple application for Windows CE
//
// Written for the book Programming Windows CE
// Copyright (C) 2003 Douglas Boling
//======================================================================
#include "windows.h"
//
// Program entry point
//
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow) {
MessageBox (NULL, TEXT ("Hello World"), TEXT ("Hello2"), MB_OK);
return 0;
}
When you compile and run Hello2, you should see a small window like the one shown in Figure 1-2.

Figure 1-2: Hello2 running on a Windows CE desktop
The MessageBox function that replaces printf provides two features for Hello2. First and most obvious, it creates a window and places the "Hello World" text in the window. The second feature is that the MessageBox function doesn't return until the user closes the message box window. This feature allows Hello2 to keep running until the user dismisses the window.The MessageBox function is prototyped as
int MessageBox (HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);
The first parameter of MessageBox is the handle to the top-level window that will be the parent of the message box when it is displayed. For now, we can leave this parameter NULL because Hello2 doesn't have any other windows. The second parameter is the text that appears in the window. Notice that the string passed is couched in the TEXT macro, ensuring that it will be compiled as Unicode. The third parameter, lpCaption, is the text that will appear in the title bar of the window. The last parameter, uType, is a series of flags that specify how the message box appears on the screen. The flags specify the number and type of buttons on the message box; the icon, if any, on the message box; and the settings of style flags for the message box window.The flags listed in Table 1-2 are valid under Windows CE.
Flags | Button or Icon |
---|---|
For Buttons: | |
MB_OK | OK |
MB_OKCANCEL | OK and Cancel |
MB_RETRYCANCEL | Retry and Cancel |
MB_YESNO | Yes and No |
MB_YESNOCANCEL | Yes, No, and Cancel |
MB_ABORTRETRYIGNORE | Abort, Retry, and Ignore |
For Icons: | |
MB_ICONEXCLAMATION, MB_ICONWARNING | Exclamation point |
MB_ICONINFORMATION, MB_ICONASTERISK | Lower case i within a circle |
MB_ICONQUESTION | Question mark |
MB_YESNO | Yes and No |
MB_ICONSTOP, MB_ICONERROR, MB_ICONHAND | Stop sign |
MB_DEFBUTTON1 | First button |
MB_DEFBUTTON2 | Second button |
MB_DEFBUTTON3 | Third button |
For Window Styles: | |
MB_SETFOREGROUND | Bring the message box to the foreground. |
MB_TOPMOST | Make the message box the topmost window. |
The return value from MessageBox indicates the button pressed by the user. The return values are as follows:
IDOK | OK button pressed |
IDYES | Yes button pressed |
IDNO | No button pressed |
IDCANCEL | Cancel button pressed or Esc key pressed |
IDABORT | Abort button pressed |
IDRETRY | Retry button pressed |
IDIGNORE | Ignore button pressed |
MessageBox is a handy function to have an application display a simple but informative dialog box.One gotcha to look out for here: If you're debugging and recompiling the program, it can't be downloaded again if an earlier version of the program is still running on the target system. That is, make sure Hello2 isn't running on the remote system when you start a new build in eMbedded Visual C++, or the autodownload part of the compile process will fail. If this happens, close the application and choose the Update Remote File menu command in eMbedded Visual C++ to download the newly compiled file.Hello2 displays a simple window, but that window is only as configurable as the MessageBox function allows. How about showing a window that is completely configurable by the application? Before we can do that, a quick review of how a Windows application really works is in order.