Visual Programming by x6PeyJM


									                      Lecture – 4
         Miss. SADAF MAJEED SIAL

      Computer Science Department
Bahauddin Zakariya University Multan.
   About Microsoft Foundation Class
   How to write basic MFC programs
   How to work with resources
   About the CString class
   How to create dialog-based applications
   How to work with message maps
   How to build an application framework with
    the MFC Application Wizard

                                                        TVP2121 Visual
                          Visual C++ and MFC – part 1   Programming
   The Microsoft Foundation Classes, or MFC, is
    a class library that assists programmers in
    creating Windows-based applications
   Perhaps two of the most important aspects of
    MFC programming are:
    ◦ MFC adds object-oriented programming
      capabilities to Windows API programming
      (Procedural  OOP)
    ◦ MFC encapsulates the Windows API into a logically
      organized hierarchy

                                                             TVP2121 Visual
                               Visual C++ and MFC – part 1   Programming
   MFC Library is a collection of C++ Classes that
    encapsulates the functionality of applications written for
    the Microsoft Windows OS.

   In which two main libraries are used as:
    ◦ LIB………Static Link Library
    ◦ DLL……..Dynamic Link Library

   LIB code is embed with program
   DLL consists of executable functions that are loaded
    into memory and are independent from any application.

   A Class library gives us a complete application
   Windows OD has 3 major componets:
    ◦ User , GDI and Kernel.

   User: is a module of code that services input devices
    such as keyborad.

   GDI: is a module that services output to graphics
    devices – screens , printers..etc

   Kernel: is a module that services on file management
    and internal memory management.

   These 3 components are called API.
   These three components interact with the
    MFC application.

   An MFC applications calls functions in the

   The base classes in the MFC libraray
    incorporate API functions, so the MFC classes
    also call functions in the API.
   The MFC class library consists of two major
    ◦ The MFC class hierarchy
    ◦ Global functions and macros

                                                            TVP2121 Visual
                              Visual C++ and MFC – part 1   Programming
            // CDC, class device context
                              TVP2121 Visual
Visual C++ and MFC – part 1   Programming
   A

                                      TVP2121 Visual
        Visual C++ and MFC – part 1   Programming
   Most classes in the MFC class hierarchy derive
    from the CObject base class
   The most important branch of the MFC class
    hierarchy is the CCmdTarget class, which
    encapsulates the messaging features of the
    Windows API
   The CWinApp (subclass of CCmdTarget) class,
    also known as the application class, is
    responsible for initializing, starting, running,
    and stopping an MFC windows application

                                                          TVP2121 Visual
                            Visual C++ and MFC – part 1   Programming
   The CWnd class encapsulates the various
    Windows API functions, data types, and other
    code used for creating and instantiating
   Derived from the CWnd class are more
    specialized types of window classes such as
    the CFrameWnd class, which creates a
    standard type of window known as a frame
    window, and the CDialog class for creating
    dialog boxes

                                                         TVP2121 Visual
                           Visual C++ and MFC – part 1   Programming
       If a function is not a
        member of an MFC class,
        then it is a global
        function that is available
        to all MFC classes,
        regardless of their
        position in the MFC class
       All MFC global functions
        begin with a prefix of
       Figure shows an example
        of a message box created
        with the AfxMessageBox()
TVP2121 Visual Programming           Visual C++ and MFC – part 1
   You can easily create the MFC framework for an
    MFC program using the MFC Application Wizard
   The classes that turn a standard C++ program into
    an MFC program derive from MFC classes
   By default, Win32 application projects do not
    support MFC programming
   In order to enable MFC support in a Win32
    application project, you must select the Use MFC in
    a Shared DLL setting in the Use of MFC combo box
    in the General category of the Property Pages
    dialog box

                                                            TVP2121 Visual
                              Visual C++ and MFC – part 1   Programming
   Framework: defines an architecture for
    integrating the user interface of an
    application for Windows with the rest of the

   Programming in MFC reduces the code as
    compared to SDK.

   All MFC class names begin with C

   Additionally, data members of MFC classes
    are prefixed with m_
   In which two objects are created, these are:
      Application Object
      Window Object

   Window Object is attached with AppObject
   MFC is the C++ class library Microsoft provides
    to place an object-oriented wrapper around the
    Windows API.

   MFC is not only the library of classes , MFC is
    also an Application framework.

   MFC helps define the structure of an application
    and handles many routines, mainly two Classes:
      CWInApp Class
      CFrameWnd Class
1.   Declare the window and application class.

2.   Implement the InitInstance member function
     of the application class.

3.   Declare an instance of the application class.

4.   Defining the window and application classes
   An application class is the starting point of
    any MFC application
   You derive an application class from the
    CWinApp class
   The application class object you instantiate in
    an MFC program represents the application
    as a whole
   When you derive an application object, you
    ◦ Override the virtual BOOL InitInstance() function
    ◦ Instantiate a global object of your application class

                                                              TVP2121 Visual
                                Visual C++ and MFC – part 1   Programming
   The InitInstance() function is called by the
    inherited WinMain() function each time a new
    instance of your MFC program starts. MFC
    provides the WinMain() function for you, and
    don’t write it.

   You can instantiate only one global
    application class object, and it is usually
    instantiated within the application class’s
    implementation file

                                                          TVP2121 Visual
                            Visual C++ and MFC – part 1   Programming
   The class CWinApp is fundamental to any
    windows program written using MFC.

   The First thing that we need to produce our
    application is to derive our own application
    class from CWinApp.

       Class myapp:Public CWinApp
              Bool InitInstance();
   InitInstance is the virtual Function.

     Bool myapp::InitInstance()
             myapp *p=new myapp();
             m_pMainWnd = p;
              m_pMainWnd  ShowWindow(m_nCmdShow)
             return TRUE;
myapp *p=new myapp();
 creates an instance of the program’s window class Myapp.

m_pMainWnd  ShowWindow(m_nCmdShow);
 Window Creation Style
   The class you will use in this section is the
    CFrameWnd class, which creates a simple
    window with a frame, title bar, control menu,
    and control buttons
   One task that is required for all window
    classes is calling the inherited Create()
    function from the class constructor
   The Create() function creates the window
    itself when an object of the window class is

                                                         TVP2121 Visual
                           Visual C++ and MFC – part 1   Programming
   In standard Windows API programming, you
    use the ShowWindow() function to display a
   MFC programming also uses a ShowWindow()
    function to display windows
   It is important that you understand that in an
    MFC Program, the ShowWindow() function is
    not called from the window class
   Instead, you call the ShowWindow() function
    from the application class’s InitInstance()
    function using an instantiated object of the
    window class
                                                          TVP2121 Visual
                            Visual C++ and MFC – part 1   Programming
   An important concept to understand is that
    m_pMainWnd object is not the window itself
   All objects that display windows are not the
    windows themselves
   The window is only a visual representation of
    the object
   To add a window class to the “Basic MFC
    Program”: m_pMainWnd = new

                                                         TVP2121 Visual
                           Visual C++ and MFC – part 1   Programming
   CFrameWnd provide creating and managing a
    window for application.
        class myframe:public CFrameWnd
                        Create(0,”Hello MFC”);
    The myframe class inherits a large set of data members and
    member functions from its base class, CFrameWnd, which is derived
    from CWnd.
   The program must call the member function create() to explicitly
    create the window.

   Create function is declared in class constructor. And its has 6

   Parameters of Create Class:
    1. lpClassname… the name of the Window class.
    2. lpWindowName….. Specifies the text that is to be displayed in
        the title bar.
    3. Dwstyle…..specifies the window style
    4. Rect…..specifies the initial size and position of the window.
    5. pParentEWnd…..specifies the parent window of the window
        which you are creating.
    6. lpMenuName… …identifies the window menu. The default value
        , Null indicates that the window does not have a menu.
   The following is a list of some of the
    significant events that occur when we write in
    the class.
    1. The CWinApp constructor is called.

    2. The program entry function WinMain receives control.

    3. WinMain calls the program’s InitInstance function.

    4. WinMain enters a loop for processing message.

    5. WinMain exits and the program terminates.
Dated:   25-10-10

To top