Udostępnij za pośrednictwem


Code Explanation: Hello World: A Multiplatform MFC Application for Smart Devices

Whether you use Visual C++ to target Windows CE (Mobile) and other mobile devices, or embedded Visual C++ to develop device applications, the C++ Multiplatform MFC Application for Smart Devices Wizard simplifies most of the mundane tasks of generating project files. Some of the files include resource files, and multiplatform project configuration files. Because the wizard also introduces jumpstart code, you can concentrate on developing your core business application functions.

Because this walkthrough familiarizes you with the code automatically generated by the Multiplatform MFC Application for Smart Devices Wizard, you can easily extend and modify the application to suit your needs.

For more information, see Walkthrough: Creating a Multiplatform MFC Application for Smart Devices, MFC Reference for Devices, and MFC Reference.

Code Listing for a Multiplatform MFC Application for Smart Devices C++

The wizard generated code includes the following:

  • include section.

    #include "stdafx.h"
    
  • stdafx.h. For more information, see Precompiled Header Files.

    #include "MFCHello1.h"
    #include "MFCHello1Doc.h"
    #include "MFCHello1View.h"
    
  • // Debug configuration.

    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    
    IMPLEMENT_DYNCREATE(CMFCHello1View, CView)
    
  • MFC message maps and more specifically BEGIN_MESSAGE_MAP.

  • IMPLEMENT_DYNCREATE see IMPLEMENT_DYNCREATE.

    BEGIN_MESSAGE_MAP(CMFCHello1View, CView)
    END_MESSAGE_MAP()
    
    
  • Construction code. For more information, see Constructor Design and for exception handling, see Exceptions: Exceptions in Constructors.

    // CMFCHello1View construction/destruction
    CMFCHello1View::CMFCHello1View()
    {
            // TODO: add construction code here.
    }
    CMFCHello1View::~CMFCHello1View()
    {
    }
    
  • MFC PreCreateWindow.

    
    BOOL CMFCHello1View::PreCreateWindow(CREATESTRUCT& cs)
    {
            // TODO: Modify the Window class or styles here by modifying
            //  the CREATESTRUCT cs.
    
            return CView::PreCreateWindow(cs);
    }
    
  • Remember that the drawing on the screen, screen painting, occurs in the CView::OnDraw method. In this method, you can take control of the Graphical Device interface (GDI), namely in code, CDC* pDC, which is provided to you as a parameter. Do not forget to uncomment it. For example, you can use the full power of the GDI for everything from text to drawing graphics for animated games in your application. In this GDI example, CDC::DrawText method is then used for drawing the "Hello World" text inside the rectangle defined by CWnd::GetClientRect.

  • BEGIN_MESSAGE_MAP.

  • CDC Class.

  • CWnd::GetClientRect.

    // CMFCHello1View drawing
    void CMFCHello1View::OnDraw(CDC* pDC)
    {
            CMFCHello1Doc* pDoc = GetDocument();
            ASSERT_VALID(pDoc);
    
            // TODO: add draw code for native data here.
            CRect rect;
          GetClientRect(&rect);
         // Length and string to draw are hard coded for simplicity of 
         // example.
    
  • DrawText and other related methods. For more information, see CDC Class.

            pDC->DrawTextW(_T("Hello World"),11, &rect,1);
            // nCount ( set to 11) can be a –1, then 
            //lpszString is assumed to be
            // a long pointer to a null-terminated string 
            // and DrawText method automatically 
            // computes the character count.
    }
    // CMFCHello1View diagnostics
    #ifdef _DEBUG
    void CMFCHello1View::AssertValid() const
    {
            CView::AssertValid();
    }
    #ifndef _WIN32_WCE
    void CMFCHello1View::Dump(CDumpContext& dc) const
    {
            CView::Dump(dc);
    }
    #endif // !_WIN32_WCE
    CMFCHello1Doc* CMFCHello1View::GetDocument() const 
    // non-debug version is inline
    {
            ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMFCHello1Doc)));
            return (CMFCHello1Doc*)m_pDocument;
    }
    #endif //_DEBUG
    // CMFCHello1View message handlers
    
    

Information Available in the Readme File Created by the Wizard

The Application Wizard has created this MFCHello1 application for you. This application not only demonstrates the basics of using the Microsoft Foundation Classes, but is also a starting point for writing your application.

The files generated are listed here, together with a summary of what you will find in each file. These files collectively make up your jumpstart point into your MFC application development.

The name HelloMFC is used for an example. You may want to use your own project name.

HelloMFC.vcproj

The main project file for Visual C++ projects generated by using an application wizard. It contains information about the version of Visual C++ that generated the file, and information about the platforms, configurations, and project features selected with the application wizard.

HelloMFC.h

The main header file for the application. It includes other project-specific headers and declares the CMFCHello1App application class.

HelloMFC.cpp

The main application source file that contains the class definition for the application class CMFCHello1App.

HelloMFCppc.rc

The project's main resource file listing of all the Microsoft Windows resources that the project uses when it compiles for the Pocket PC platform, or a platform that supports the same user interface model. It includes the icons, bitmaps, and cursors that are stored in the RES subdirectory. This file can be directly edited in Microsoft Visual C++. Your project resources are in 1033. When the .rc file is persisted, the defines in the data section are persisted as the hexadecimal version of the numeric value they are defined to, rather than the friendly name of the define.

res\HelloMFCppc.rc2

A file that contains resources that are not edited by Microsoft Visual C++. You should put all resources not editable by the resource editor in this file.

HelloMFCsp.rc

The project's main resource file listing of all the Microsoft Windows resources that the project uses when it compiles for the Smartphone platform, or a platform that supports the same user interface model. It includes the icons, bitmaps, and cursors that are stored in the RES subdirectory. This file can be directly edited in Microsoft Visual C++. Your project resources are in 1033. When the .rc file is persisted, the defines in the data section are persisted as the hexadecimal version of the numeric value they are defined to rather than the friendly name of the define.

res\HelloMFCsp.rc2

A file that contains resources that are not edited by Microsoft Visual C++. You should put all resources not editable by the resource editor in this file.

res\HelloMFC.ico

An icon file, used as the application's icon. This icon is included by the main resource file.

MainFrm.h, MainFrm.cpp

Files that contain the frame class CMainFrame, which is derived from CFrameWnd and controls all SDI frame features.

The application wizard also creates one MFC document type and one MFC view:

  • HelloMFCDoc.h, HelloMFCDoc.cpp

    Files that contain your HelloMFCDoc class. Edit these files to add your special document data and to implement file-saving and loading (using CMFCHello1Doc::Serialize).

  • HelloMFCView.h, HelloMFCView.cpp

    Files that contain your HelloMFCView class. HelloMFCView objects are used to view HelloMFCDoc objects.

StdAfx.h, StdAfx.cpp

Files used to build a precompiled header (PCH) file that is named HelloMFC.pch and a precompiled types file that is named StdAfx.obj.

Resourceppc.h and Resourcesp.h

The standard header file, which defines new resource IDs. Microsoft Visual C++ reads and updates this file.

The application wizard uses TODO: to indicate parts of the source code you can add to or customize.

If your application uses MFC in a shared DLL, and your application is in a language other than the operating system's current language, you may want to copy the corresponding localized resource's MFC80XXX.DLL to your application directory. In this name, "XXX" represents the language abbreviation. For example, MFC80DEU.DLL contains resources translated to German. Otherwise, some of the UI elements of your application will remain in the language of the operating system.

See Also

Concepts

Smart Device Samples