MFC第一篇

 1.1  MFC基本認識

Afx.h   //MFC 基本類框架app

Afxwin.h //MFC 程序框架框架

CwinApp //程序對象函數

CFrame //程序框架this

InitInstance();//初始化函數spa

//消息入口對象

DECLARE_MESSAGE_MAP()(在CFrame中定義)繼承

//消息處理開始it

BEGIN_MESSAGE_MAP(CMainWindow,CFrameWnd)io

    //繪畫class

    ON_WM_PAINT()

    //消息結束

 END_MESSAGE_MAP()

1.2  MFC建立基本步驟

(1)     繼承CwinApp  定義繪製窗口函數OnPaint

(2)     繼承CFrame 定義初始化函數 Initistance

(3)     實現CwinApp構造函數建立窗口(Create(NULL,title))

(4)     實現OnPaint函數繪製程序GUI

(5)     實現初始化函數Initinstance (將CwinApp保存到m_pMainWind中負責沒法運行)

 

1.3 實戰1 第一個MFC程序

 

App.h  

#ifndef   _AFXDLL

#define   _AFXDLL

#endif

#include <afx.h>

#include <afxwin.h>

class SelfApp:public CWinApp

{

public :

    //初始化函數

    virtual BOOL InitInstance();

 

};

 

class CMainWindow :public CFrameWnd

{

public:

    CMainWindow();

protected:

    afx_msg void OnPaint();

    //消息入口

    DECLARE_MESSAGE_MAP()

};

Appi.cpp

#include "app.h"

SelfApp app;

CMainWindow  *  mainwnd;

//實現函數及構造函數

BOOL  SelfApp::InitInstance()

{

    mainwnd  =  new CMainWindow;

    mainwnd->ShowWindow(m_nCmdShow);

    mainwnd->UpdateWindow();

 

    m_pMainWnd = mainwnd;

    return TRUE;

}

//消息處理開始

BEGIN_MESSAGE_MAP(CMainWindow,CFrameWnd)

    //繪畫

    ON_WM_PAINT()

    //消息結束

    END_MESSAGE_MAP()

    CMainWindow::CMainWindow()

    {

        //建立窗口

        Create(NULL,TEXT("The Application"));

    }

    void CMainWindow::OnPaint()

    {    //繪製筆

        CPaintDC dc(this);

        CRect rect;

        GetClientRect(&rect);

       //繪製文本

        dc.DrawText(__T("This is MFC application"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);

    }

相關文章
相關標籤/搜索