Know about Frames in VC++

Started by sukishan, Jul 12, 2009, 05:13 PM

Previous topic - Next topic

sukishan

Introduction to Frames

As its name implies, a frame of a window includes the borders, the location, and the dimensions of a window. There are two types of MFC applications: those that use a frame and those that don't. A frame-based application uses a concept known as the Document/View Architecture. This allows the frame to serve as a place holder for other parts of an application (such as the document and the view).

To create a frame, the MFC library provides various classes. One of these is called CFrameWnd and it is the most commonly used frame class. To use a frame, you can derive your own class from CFrameWnd as follows:


class CApplicationFrame : public CFrameWnd
{

};

Because there can be many frames or various types of frames in an application, the first or main frame is usually called CMainFrame, we will follow the same habit but you can call your frame class anything you want:

class CMainFrame : public CFrameWnd
{
};


The skeleton of this frame only serves as a foundation for your class. You must actually create a window frame that would display to the user. To create a window frame, the CFrameWnd class provides the Create() method. Its syntax is:

BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName,
       DWORD dwStyle = WS_OVERLAPPEDWINDOW, const RECT& rect = rectDefault,
       CWnd* pParentWnd = NULL, LPCTSTR lpszMenuName = NULL, DWORD dwExStyle = 0,
       CCreateContext* pContext = NULL );


As you can see, the only two required arguments are the class name and the window name. We will come back to all these arguments when we study window classes in more detail. For now, a minimum frame can be created by simply passing the class name as NULL and the window name with a null-terminated string. Here is an example:

class CMainFrame : public CFrameWnd
{
public:
    CMainFrame();
};

CMainFrame::CMainFrame()
{
    Create(NULL, "MFC Fundamentals");
}


In order to provide a window to the application, you must create a thread. This would be done using the CWinThread class. To make this a little easy, CWinThread is equipped with a public member variable called m_pMainWnd. This variable can be used to create a thread for the main window of the application. One of its advantages is that it makes sure that your application terminates smoothly when the user decides to close it. CWinThread is the base class of CWinApp and therefore makes m_pMainWnd available to any CWinThread derived class such as CFrameWnd. Based on this, to create a thread for the main window to display, you can assign a pointer of your frame class to m_pMainWnd. After this assignment, m_pMainWnd can be used as the window object to display the frame, which is usually done by calling the ShowWindow() method. This would be done as follows:

BOOL CExerciseApp::InitInstance()
{
    m_pMainWnd = new CMainFrame;
    m_pMainWnd->ShowWindow(SW_NORMAL);

    return TRUE;
}
A good beginning makes a good ending