Thursday, March 8, 2007

How to Create a Button or Edit box at runtime?


(P.S :- Create all these in the ………..Dlg.h and …………..Dlg.cpp only)

First create an object for the button or edit box you want to create.

If you want a button create a object for the CButton class or else for a edit box create it from CEdit and likewise.

A sample code snippet is shown below;

CButton *pButton;

CEdit *pEdit;

Then in the Constructor

Add the following lines of code

pButton = new CButton;

pEdit = new CEdit;

In the Init Method call the Create member function and pass the necessary parameters.

Its code snippet is shown below.

pButton->Create("My Button",

WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON ,

CRect(10, 10, 100, 30),

this,

IDC_BUTTON1

);

pEdit->Create( WS_CHILD| WS_VISIBLE, CRect(50,50,150,80), this, 3 );

And thus your Button is created.

Now you have to make it active to user input.

You have to create an ID for it and capture its messages through message maps.

For that you have to do the following.

In the file named (resource.h) add the control id. Below is a code snippet to do it.

#define IDC_BUTTON1 2000

#define IDC_EDIT1 3

If you want your control to accept the user button you have to add to the message map.

For that do the following

In the file named (……Dlg.cpp) -> Go to Message Map Definition and add the following code snippet

ON_BN_CLICKED(IDC_BUTTON2, OnButton2)

In ON_BN_CLICKED, the BN denotes Button and CLICKED denotes click event. You can also add other message handlers.

Then in the file name (………Dlg.h) add the following code snippet

afx_msg void OnButton2();

The void OnButton2(); is the function where we tell the Button what to do when a click event as occurred.

Then in file named (………..Dlg.cpp) define the function,

Sample of a function is shown below.

void CCreateDialog1Dlg::OnButton2()

{

MessageBox("Button 2 is clicked");

}

Here we just make the Message Box pop-up once the button is clicked.

How to Create a Dialog Box in MFC SDI Environment?


In a SDI application to launch a dialog box you have to launch it from the menu or toolbar.

Listed below are the procedures by which you can accomplish the above.

First Creates a menu item and dialog box.

Now Create a class for the dialog box.

Once you create a dialog box and right click it, it will show a window where you can name your class for the new dialog.

However it will be derived from CDialog.

For Example Imagine you have a new dialog and you have created a new class named CTest.

Then In the Class wizard create a function for the Menu or Tool Bar item, which when clicked will launch the dialog box.

In that function create a object for the CTest class previously created by us.

The code Snippet is shown below.

CTest test;

test.DoModal();

Now run the program. Once you go to the particular menu item and click it the dialog box will be launced.

Note:- Wherever you create the class object be sure to include the Header file.

MFC Architecture





The MFC Architecture is sub-divided into the above three.

So in SDI we will get only one window at a time and in MDI we get one or more window at a time.

A window here is called document.

The document-view application architecture in MFC includes

  1. An application object,------------------------------------------
  2. A frame window,------------------------------------------------

MainFrm.h and MainFrm.cpp

  1. A document object (or several), and--------------------------

Diagram.h, Diagram.cpp DiagramDoc.h, and DiagramDoc.cpp

  1. One or more view objects for each document.--------------

DiagramView.h and DiagramView.cpp.

1) An application object

The Application Object is derived from the Application Class CWinApp. CWinApp is derived from CWinThread, which represents the main thread of execution for your application, which might have one or more threads.

This is solely responsible for initialization, running, and termination of an application for the Windows operating system.

If your application creates separate threads of execution — for example, to perform calculations in the background — you'll use classes derived from CWinThread. CWinApp itself is derived from CWinThread and represents the primary thread of execution (or the main process) in your application. You can also use MFC in secondary threads.

2) A frame window

Views are displayed inside "document frame windows." In an SDI application, the document frame window is also the "main frame window" for the application. In an MDI application, document windows are child windows displayed inside a main frame window. Your derived main frame-window class specifies the styles and other characteristics of the frame windows that contain your views. If you need to customize frame windows, derive from CFrameWnd to customize the document frame window for SDI applications. Derive from CMDIFrameWnd to customize the main frame window for MDI applications. Also derive a class from CMDIChildWnd to customize each distinct kind of MDI document frame windows that your application supports.

3) A document object

Your document class (derived from CDocument) specifies your application's data. If you want OLE functionality in your application, derive your document class from COleDocument or one of its derived classes, depending on the type of functionality you need.

4) View objects

Your view class (derived from CView) is the user's "window on the data." The view class controls how the user sees your document's data and interacts with it. In some cases, you may want a document to have multiple views of the data.

If you need scrolling, derive from CScrollView. If your view has a user interface that is laid out in a dialog-template resource, derive from CFormView. For simple text data, use or derive from CEditView. For a form-based data-access application, such as a data-entry program, derive from CRecordView (for ODBC). Also available are classes CTreeView, CListView, and CRichEditView.

The document template or templates

A document template orchestrates the creation of documents, views, and frame windows. A particular document-template class, derived from class CDocTemplate, creates and manages all open documents of one type. Applications that support more than one type of document have multiple document templates. Use class CSingleDocTemplate for SDI applications, or use class CMultiDocTemplate for MDI applications.

The document object and the view objects are connected with a document template.

Frame Windows

When an application runs under Microsoft Windows, the user interacts with documents displayed in frame windows. A document frame window has two major components: the frame and the contents that it frames. A document frame window can be a single document interface (SDI) frame window or a multiple document interface (MDI) child window. Windows manages most of the user's interaction with the frame window: moving and resizing the window, closing it, and minimizing and maximizing it. You manage the contents inside the frame.

The Full Picture

The white space in the middle is called client area. And all the other parts the border; menu, toolbar etc are termed as non-client area.

Ø The Frame Window is the non-client area.

Ø The view object is the client area and contains the displays the user needs

Ø The document object stores the data the user needs. That is what the view object makes use of.

Ø The Application Object is needed to launch this application. (Derived from Thread Class).

Ø And a Document template is needed to communicate between view and document objects

Ø A thread is used to open many instances of an application.

What is Run-Time Class Information?


During the execution of the program we may query about the data type of an object like int or float like that, and more over incase of inheritance we check the object whether it is an object of the specified class
Ex:
Base class is CEncoder
derived classes like Windows media Encoder...
At runtime we may check whether this object is related to Windows media encoder...
Both are classes

How do I filter characters in a CEdit?



A. Create a CEdit derivative (CMyEdit) and create an OnChar() message map entry.
 
   // Assume that CMyEdit is derived from the CEdit class.
 
   afx_msg void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
   {
      if (nChar == 'a' || nChar == 'A')
      {
          nChar = 'X';
      }
      DefWindowProc(WM_CHAR, nChar, MAKELONG(nRepCnt, nFlags));
   }

Difference between Modal and Modeless Dialog Boxes

Modal Dialog Box
A dialog box that, once opened, blocks input to the rest of the application
or to the whole system until it is answered by a user event.
Modeless Dialog Box
A dialog box that allows the user to switch to another window while the
dialog box is still on the screen. The opposite of a modal dialog box.

Purpose of MFC Class


CObject

It is the principal base class for the Microsoft Foundation Class Library.

Provides Support for

  • Serialization support
  • Run-time class information
  • Object diagnostic output
  • Compatibility with collection classes

Serialization

  • Serialization is the process of writing or reading an object to or from a persistent storage medium such as a disk file.
  • Used when the data has to be in structured format.
  • Serialize is the method used The Serialize member function, which is defined in the CObject class, is responsible for actually serializing the data necessary to capture an object's current state.

Run-Time class information

  • Used to get the current object during run-time.
  • Uses the IskindOf() function to do this.

Object Diagnostic Output

The Microsoft Foundation Class Library supplies many diagnostic services that make debugging your programs easier. These diagnostic services include macros and global functions that allow you to track your program's memory allocations, dump the contents of objects during run time, and print debugging messages during run time.

Compatibility with collection classes

Collections are used to store any type of object, and retrieve back from the collections

For example if we have a data in database, for every time we need to make a connection to database and retrieve the data, instead of it we may retrieve all the entries from database and store it in collection class

This will somewhat speeds up the performance of an application

MFC Collection Classes

MFC collection classes are categorized briefly as:

I) Array Classes : they are :

1.CByteArray

2.CWordArray

3.CUintArray

4.CStringArray

5.CPtrArray

6.CObArray

II)List Classes :They are-

1.CObList

2.CPtrList

3.CStringList

III)Map Classes:

1.CMapWordToPtr

2.CMapPtrToWord

3.CMapPtrToPtr

4.CMapWordToOb

5.CMapStringToOb

6.CMapStringToPtr

7.CmapStringToString

· List

The list class provides an ordered, nonindexed list of elements, implemented as a doubly linked list. A list has a "head" and a "tail," and adding or removing elements from the head or tail, or inserting or deleting elements in the middle, is very fast.

· Array

The array class provides a dynamically sized, ordered, and integer-indexed array of objects.

· Map (also known as a dictionary)

A map is a collection that associates a key object with a value object.

CCmdTarget

It is the base class for the Microsoft Foundation Class Library message-map architecture.

CCmdTarget include CView, CWinApp, CDocument, CWnd, and CFrameWnd.

  • CWnd class provides the base functionality of all window classes in the Microsoft Foundation Class Library.

  • CView class provides the basic functionality for user-defined view classes.

  • CWinApp class is the base class from which you derive a Windows application object.

  • CDocument class provides the basic functionality for user-defined document classes.

  • CFrameWnd class provides the functionality of a Windows single document interface (SDI) overlapped or pop-up frame window, along with members for managing the window.

CMDIFrameWnd

The CMDIFrameWnd class is derived from the CFrameWnd base class. The class CMDIFrameWnd provides the functionality of a Windows multiple document interface (MDI) frame window, along with members for managing the window.

CDataExchange

Supports the dialog data exchange (DDX) and dialog data validation (DDV) routines used by the Microsoft Foundation classes.

CDC

Defines a class of device-context objects.

CDialog: The CDialog class is derived from the base class CWnd. The CDialog class provides the basic functionality of a Windows dialog box window, along with members for managing the window.

CDocTemplate: The CDocTemplate class is derived from the CCmdTarget base class. The MFC class CDocTemplate provides some basic functionality for the MFC document / view architecture and works in conjunction with the CDocument and CView classes.

Some of the most commonly used derived MFC classes are CCmdTarget, CWinApp, CDocTemplate, CWnd, CDocument, CFrameWnd, CView, CDialog, and CMDIFrameWnd.