MFC Tutorial Part 1 - Creating a Window

MFC provides really nice classes for doing windows programming. It has taken away all the pains of SDK, where a programmer is expected to handle everything in the world except writing for application logic. It used to be such a big pain, that people hated SDK so much and lots of them shifted to VB development.

   This series of MFC Tutorial articles, will try to cover some of the basics for a good start in MFC Programming.

The first task to be done in any application is to create a window for an application. MFC provides two important classes viz. CWinApp & CFrameWnd, which can be used for creating a window & the application. Both these classes have their own message handling mechanisms, screen-drawing functions etc., To be precise, CWinApp provides the application level functionalities and CFrameWnd provides the functionalities related to GUI.

    All these classes are derived from CCmdTarget which in turn is derived from CObject. CCmdTarget is created with the capability to handle windows messages, which is referred as Message Maps. If you wonder what this Message map means, "Message maps are macros which take care of the event handling". This topic will be dealt in the next part of our MFC Tutorial (Part 2).

     The CWinApp class has an important over-ridable function InitInstance which handles the Window creations. The next important one is a data member, m_pMainWnd (of CWinApp) which holds the pointer to the window.

    Let's write an example and try to understand it.
    Follow these steps to create an application with minimal usage wizard. The wizard will be used just to create a simple workspace for this MFC Tutorial.

Step1:
    Create a new project of type Win32 application. In the second screen of the wizard, select the first option. Do not allow the wizard to add any files. 
Step 2: 
    After the project is created, click on Menu -->Project --> Add to Project -->New and select a .cpp file and give a name 
to it.
Step 3:
    Copy and paste the code below. 


//MFC1.CPP - MFC Tutorial Part 1 from codersource.net

#include <afxwin.h>

class MFC_Tutorial_Window :public CFrameWnd
{
public:
    MFC_Tutorial_Window()
    {
         Create(NULL,"MFC Tutorial Part 1 CoderSource Window");
    }
};

class MyApp :public CWinApp
{
   MFC_Tutorial_Window *wnd; 
public:
   BOOL InitInstance()
   {
        wnd = new MFC_Tutorial_Window();
        m_pMainWnd = wnd;
        m_pMainWnd->ShowWindow(1);
        return 1;
     }
};

MyApp theApp;

//End of program MFC Tutorial Part 1


Step 4:
   Now compile the application. It will generate linker errors. The reason is because the MFC libraries are not yet linked. 
   Do the last step of this MFC Tutorial.
Step 5:
Select Menu -->Project --> Settings and General tab. On the first combo box "Microsoft Foundation classes" and select "Use MFC in shared dll".
   Now compile and run the program.

   This MFC Tutorial example creates a simple Window with a title called "MFC Tutorial Part 1 CoderSource Window". The window has no contents, menu or any other controls. 
   The next part of this MFC Tutorial series will handle the left out topics one by one.

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值