Introduction to Win32 Programming by ojp13483

VIEWS: 14 PAGES: 3

									ITB457 Window Programming                                                    Tutorial 4


                             Introduction to MFC
The goal of this tutorial is to implement similar functionality to the previous project,
but to use MFC rather than the Win32 API. Normally when using MFC you would
create an “MFC AppWizard (exe)” project where you would start by using
AppWizard to create a starter application. In this tutorial we will construct an MFC
application completely by hand, without the use of the AppWizard. This exercise will
help you understand how a basic MFC application works, and what AppWizard
normally does for you.

 Create a new Win32 Application project:
      Select “File” - ”New” - ”Projects” - ”Win32 Application”
      Select “An empty project”

 Create a new (empty) C header file “MyApp.h” and add it to the project.
 Add the following code to that header file:
   #include <afxwin.h>   // MFC library

   class MyApp: public CWinApp
   {
   public:
       BOOL InitInstance();
   };

 Create a new (empty) C++ source file “MyApp.cpp” and add it to the project.
 Add the following code to that source file:
   #include "MyApp.h"

   MyApp theApp;   // the single instance of a CwinApp derived object

   BOOL MyApp::InitInstance()
   {
       return FALSE;
   }

 Try Compiling your program.

 Your program won’t link because we haven’t told it to link with the MFC library.
     o Select “Project” – “Settings…” – “General”
     o Set “Microsoft Foundation Classes” to “Use MFC in a Shared DLL”
 Compile and test your program.

 The program runs, but it doesn’t do anything, because we haven’t created any
  windows in InitInstance.

 Add the following code to InitInstance to create a generic frame window.

   CFrameWnd *myMainWnd = new CFrameWnd;

   myMainWnd->Create(NULL, "My Window Title");
   myMainWnd->ShowWindow(m_nCmdShow);

   m_pMainWnd = myMainWnd;

   return TRUE;
ITB457 Window Programming                                                  Tutorial 4


 In order to create a window that we can actually use, we need to create our own
  class derived from CFrameWnd.
      o Create a new (empty) C header file “MyWnd.h” and add it to the project.
      o Add the following code to that header file:
           #include <afxwin.h>

           class MyWnd: public CFrameWnd
           {
           };

    o #include “MyWnd.h” at the top of MyApp.cpp
    o Change InitInstance to create a MyWnd object rather than a
        CFrameWnd object.
 Compile and test your program.

 It still doesn’t do anything very interesting because we haven’t customized it yet.
  Let’s add some code to paint our new window.
       o First we’ll set up a message map for this new class. Add the following
            macro to the protected section of the MyWnd class definition:
           DECLARE_MESSAGE_MAP():


       o Create a new (empty) C++ source file “MyWnd.cpp” and add it to the
         project.
       o Add the following code to that source file:
           #include "MyWnd.h"

           BEGIN_MESSAGE_MAP(MyWnd, CFrameWnd)
           END_MESSAGE_MAP()

 Compile and test your program.

 It still doesn’t do anything interesting because we haven’t added any message
  handlers to the message map. Let’s add one for handling paint messages.
      o Add the following message map entry between the BEGIN and END
           MESSAGE MAP macros:
           ON_WM_PAINT()

       o Add the following method declaration to the public section of the MyWnd
         class:
           void OnPaint();


       o Add the following method implementation to MyWnd.cpp:
           void MyWnd::OnPaint()
           {
               CPaintDC dc(this);
               dc.TextOut(200, 10, "Hello World!");
           }


 Compile and test your program.
ITB457 Window Programming                                               Tutorial 4


 Now let’s make use of some resources.
     o Create a new resource script and add it to the project.
     o Create a menu template resource as in the previous tutorial with a “File” –
        “Exit” option with ID “ID_APP_EXIT”.
     o Change the menu template’s resource ID to “MY_MAIN_FRAME”
     o Create an Icon resource (with 16x16 and 32x32 images) and change it’s
        resource ID to “MY_MAIN_FRAME”.
     o Create a String table resource, and include an entry with ID
        “MY_MAIN_FRAME” and Caption “My Window Caption”.
 The simplest way to “connect” these resources to our window is to use the
  CFrameWnd::LoadFrame method rather than CFrameWnd::Create.
     o #include “resource.h” at the top of MyApp.cpp
     o Replace the call to Create in InitInstance with the following:

          myMainWnd->LoadFrame(MY_MAIN_FRAME);


 Compile and test your program..

								
To top