Visual C++ for Windows CE

Visual C++ for Windows CE is an add-on to Visual C++. When you install C++ for Windows CE, it extends the Visual C++ environment by adding several Windows CE-specific features:

One interesting aspect of Visual C++ for Windows CE is the fact that it also supports the older 1.0 and 1.01 versions of Windows CE. Figure 38-5 shows the Windows CE operating system and processor configuration bars that have been added to Visual C++.

While the environment lets you remotely run and debug your applications on a connected Windows CE device, it also includes a very powerful Windows CE emulation environment. The Windows CE emulator (WCE) is an Intel-based software-only version of Windows CE that runs on your desktop and gives you the convenience of being able to run and test your applications on your development machine. Of course, to ensure that your applications work correctly, you still need to test on real devices, but the emulator takes much of the pain out of the early compile and debug stages of Windows CE development. Figure 38-6 shows the emulation environment in action.

There are four Windows-CE-specific AppWizards that ship with Visual C++ for WCE:

Click to view at full size.

Figure 38-5. Visual C++ for the Windows CE environment.

Click to view at full size.

Figure 38-6. The Windows CE emulator.

The WCE AppWizards are basically the same as their big brother Win32 counterparts, except that they have different features that take advantage of the Windows CE operating system, such as the Windows CE help environment. Figure 38-7 shows the first three steps of the Windows CE MFC executable AppWizard. Notice that there are only two project types: SDI and dialog-based. Notice also the variety of Windows-CE-specific options that you can choose from.

Click to view at full size.

Click to view at full size.

Click to view at full size.

Figure 38-7. The Windows CE MFC AppWizard.

MFC for Windows CE

Visual C++ for Windows CE ships with a smaller version of MFC named Mini MFC. To get a feel for which MFC classes are and are not supported, see Figure 38-8—the MFC for Windows CE hierarchy chart. The grayed out classes are not supported on Windows CE.

Several classes have been added to Mini MFC, including classes for command bars, object store, and socket classes. Windows CE functions provide the command bars in the Mini MFC CFrameWnd class.

Instead of implementing a file metaphor, Windows CE provides an object store. Several new MFC classes were added to give the Windows CE developer access to the object store:

In addition to these data store classes, a new class CCeSocket is provided. CCeSocket implements an asynchronous CSocket derivative.

Click to view at full size.

Click to view at full size.

Figure 38-8. MFC for Windows CE hierarchy chart.

Using Mini MFC

Let's look at a couple of examples to get a feel for the Mini version of MFC. In example EX38A, we will create a basic SDI application that draws some text in the view and displays a dialog when the user presses the left mouse button (or taps the screen on a Windows CE machine). EX38A is similar to the EX06A example, so you can compare the steps in creating a similar application for Windows 98, Windows NT, and Windows CE. For this example, you will create a simulated expense-tracking application for Windows CE—a perfect candidate for portable computing. For demonstration purposes, we will focus on creating a dialog that allows the user to enter expense information. Figure 38-9 shows the application running in the emulation environment.

Click to view at full size.

Figure 38-9. The EX38A expense-tracking application.

Here are the steps to create the EX38A example:

  1. Run the MFC Executable for Windows CE AppWizard to produce \vcpp32\ex38a\ex38a. Select the WCE MFC AppWizard project type and accept all the defaults. The options and the default class names are shown here.

  2. Select the WCE x86em Debug configuration. Choose the Set Active Configuration command from the Set menu and then select Ex38a-Win32 (WCE x86em) Debug from the list. Click OK. This configuration will allow you to work with the desktop emulator instead of working remotely with a connected Windows CE device. (If you have a connected Windows CE device you can select its configuration instead. For example, select Ex38a—Win32 [WCE SH] Debug if you have a connected HP 620LX.)

    Click to view at full size.

  3. Use the dialog editor to create a dialog resource. Choose Resource from the Insert menu, select Dialog, and then click New. The dialog editor assigns the ID IDD_DIALOG1 to the new dialog. Change the dialog caption to The Dialog that Ate Windows CE! You can resize the dialog to be wider but not much taller. (Windows CE displays are much wider than they are tall.)

  4. Remove the OK and CANCEL buttons and add an OK caption button. Since screen real estate is at a premium in Windows CE, we can save a good deal of space in our dialog by deleting the OK and CANCEL buttons. For Cancel functionality, users can use the close button that is part of the dialog caption. Windows CE also supports an OK caption button that you can create by setting the dialog's property bar. To set the property, open the properties editor for the dialog, click the Extended Styles tab, and then check the Caption Bar OK (WCE Only) option as shown here.

    (You might also need to set the dialog's Visible property on the More Styles tab.)

  5. Add the dialog's controls. Add the following controls shown in Figure 38-9 and accept the default names:

  6. Add the CEx38aDialog class. After adding the controls, double-click on the dialog. ClassWizard detects that you have created a dialog resource and asks whether you want to create a class for the dialog. Click OK and accept the defaults to create the CEx06aDialog class.

  7. Program the controls. Use ClassWizard to create the following member variables in the dialog class:

    If you need a refresher on how to program modal dialogs, please refer to Chapter 6.

    Next, add the following code to the CEx38aDialog::OnInitDialog handler to initialize the controls:

    BOOL CWindowsCEDlg::OnInitDialog() 
    {
        CDialog::OnInitDialog();
        m_pComboBox.AddString(_T("Travel"));
        m_pComboBox.AddString(_T("Meal"));
        m_pComboBox.AddString(_T("Cab Fare"));
        m_pComboBox.AddString(_T("Entertainment"));
        m_pComboBox.AddString(_T("Other"));
        m_pProgressCtrl.SetPos(50);
    
        return TRUE;  
    }

    Notice that you must use the _T macro whenever you have inline strings.

  8. Connect the dialog to the View. In ClassWizard, select the CEx38aView class and use ClassWizard to add the OnLButtonDown member function.

  9. Write the code for the OnLButtonDown handler. Add the boldface code below:

    void CEx38aView::OnLButtonDown(UINT nFlags, CPoint point) 
    {
        CWindowsCEDlg dlg;
        dlg.DoModal();
        CView::OnLButtonDown(nFlags, point);
    }

  10. Add code to the virtual OnDraw function in file ex38aView.cpp. The CDC::TextOut function used in previous examples is not supported on Windows CE, so we need to use the CDC::DrawText function as shown here:

    void CEx38aView::OnDraw(CDC* pDC)
    {
        CRect rect;
        GetClientRect(rect);
    
        pDC->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
        pDC->SetBkMode(TRANSPARENT);
    
        pDC->DrawText(_T("Press the left mouse button here."), 
            rect, DT_SINGLELINE);
    }

  11. Add the ex38aDialog.h header file to ex38aView.cpp. Insert the include statement

    #include "ex38aDialog.h"

    at the top of the ex38aView.cpp source file, after the statement

    #include "ex38aView.h"

  12. Build and run the application. The EX38A application should appear as shown in Figure 38-10. If everything works satisfactorily, you can change the configuration for a real Windows CE device. Then you can run (or debug) the application remotely on the device to ensure it works in a real-world situation.

    Click to view at full size.

    Figure 38-10. The EX 38A application running in the Windows CE emulator.

As you can tell from the EX38A application, programming for Windows CE is very similar to programming for Windows 98 and Windows.

Many Windows CE developers are interested in porting existing applications to the Windows CE environment. The next section shows you how to tackle this problem.

Porting an Existing MFC Application to Windows CE

In example EX38B we will port an existing application (EX06B from Chapter 6) from Windows 98 and Windows NT to Windows CE.

We chose the EX06B sample because it is an SDI application. If you are porting an MDI application to Windows CE, we recommend that you first convert it into an SDI application (or a series of SDI applications, if you have several views) and then port it to Windows CE.

Here are the steps for converting EX06B:

  1. Run the MFC AppWizard to produce \vcpp32\ex38b\ex38b. Select the WCE MFC AppWizard project type and accept all defaults. (You might have thought that we could copy the EX06B workspace and then add a Windows CE configuration. However, it is actually easier to start with a WCE MFC AppWizard project instead because there are many complicated build settings that the wizard automatically sets up for you.)

  2. Using Windows Explorer, copy the EX06B files to the EX38B directory. Be sure to copy the following files: Ex06bDialog.h, Ex06bDialog.cpp, Ex06bDoc.h, Ex06bDoc.cpp, ex06bView.h, and ex06bView.cpp.

  3. Insert the new files into the project. Choose the Add To Project/Files command from the Project menu and insert the files from step 2 into the project.

  4. Copy the dialog and Icon resources from EX06B. Choose Open from the File menu and select \vcpp32\ex06b\ex06b\ex06b.rc. Drag and drop IDD_DIALOG1 from ex06b.rc into the EX38B project. Next, drag and drop the color icon resources (IDI_BLACK, IDI_BLUE, and so on) from the ex06b.rc file into the EX38B project.

  5. Build the application and repair any compiler errors or warnings. Now that you have moved the key files that you need (the document, view, and dialog classes) from the EX06B application to the Windows CE EX38B application, try to build the project. You should see a number of errors that can be fixed with the following steps:

  6. Replace the wizard-generated view with the real view. Open the ex38b.cpp file and do a global search and replace:

    Also, make sure that ex38b.cpp #includes both the ex06bView.h and ex06bDoc.h header files.

  7. Using the dialog editor, adjust the dialog's layout for Windows CE. As it stands, the dialog is far too tall for Windows CE. You can rearrange it by following these steps:

  8. Clean up the project. Now you can remove the document and view classes created by the MFC AppWizard by selecting the files in FileView and pressing the Delete key.

  9. Build and test. In eight easy steps, you have converted a small MFC application from Windows 98 and Windows NT to Windows CE.

    Figure 38-11 shows EX38B running in emulation mode for Window CE.

Click to view at full size.

Figure 38-11. The EX38B application running in the Windows CE emulator.

ATL and Windows CE

In addition to Mini MFC, Visual C++ for Windows CE also provides a Windows CE-friendly version of ATL. ATL is already a lightweight framework, so Microsoft didn't need to reduce the feature set for size constraints. However, there are some areas of COM not covered by Windows CE 2.0 that impact the feature set of ATL for Windows CE.

Windows CE doesn't support the apartment-threading model, so ATL for Windows CE doesn't implement the CComApartment, CComAutoThreadModule, CComClassFactoryAutoThread, or CComSimpleThreadAllocator classes.

Windows CE also doesn't support asynchronous monikers, so ATL for Windows CE doesn't implement the IBindStatusCallbackImpl or CBindStatusCallback classes. A variety of other ATL class member functions that behave differently on Windows CE are documented in the Visual C++ for Windows CE documentation.

In addition to ATL, a Windows CE version of the ATL Wizard is provided. Figure 38-12 shows the wizard that has only one option: to use MFC or not.

Click to view at full size.

Figure 38-12. The WCE ATL COM AppWizard.

When you write ActiveX controls for Windows CE, remember that they are binary objects and therefore processor-dependent. Depending on the devices you plan to support, you might have to provide several versions of the control (MIPS or SH, for example).