mfc建立一個沒有文檔類的視圖程式

來源:互聯網
上載者:User
mfc程式最主要的是文檔視圖的架構,現在不要文檔如何建立呢? 雖然網上的資料一大把,但是自己

再寫一個備忘錄。

 

#include <afxwin.h>         // MFC 核心組件和標準組件

#include <afxext.h>         // MFC 擴充

// =======================================視圖類===========================================

class CMyView : public CView

{

     // 建構函式聲明為protected,就是視只能由架構內部建立

protected:

     CMyView()

     {

     }

     // 可以動態建立

     DECLARE_DYNCREATE(CMyView)

     // 聲明訊息映射

     DECLARE_MESSAGE_MAP()

     // 自訂背景擦除

     afx_msg BOOL OnEraseBkgnd(CDC *pDC)

     {

         RECT rect;

         this->GetClientRect(&rect);

         COLORREF color = RGB(200,200,220);

         CBrush brush(color);

         pDC->FillRect(&rect, &brush);

         return TRUE;

     }

public:

     // 必須實現此函數,這是CView類的純虛函數,CView是抽象類別。

     virtual void OnDraw(CDC *pDC)

     {

         pDC->SetBkMode(TRANSPARENT);

         pDC->TextOutW(100,100,CString("This is my view"));

     }

     // 解構函式是public的

     ~CMyView()

     {

     }

};

// 實現動態建立能力

IMPLEMENT_DYNCREATE(CMyView, CView)

// 實現訊息映射

BEGIN_MESSAGE_MAP(CMyView, CView)

     ON_WM_ERASEBKGND()

END_MESSAGE_MAP()

// =======================================主架構視窗類別===========================================

class CMainFrame : public CFrameWnd

{

protected:

     // 支援動態建立

     DECLARE_DYNCREATE(CMainFrame)

     // 支援訊息映射

     DECLARE_MESSAGE_MAP()

     // 視

     CMyView *m_pMainView;

     // OnCreate方法,在視窗建立完成時才被調用,由WM_CREATE訊息引起被調用

     afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct)

     {

         // 首先完成基類的方法

         if (CFrameWnd::OnCreate(lpCreateStruct) == -1)

              return -1;

         // 建立視圖,必須聲明一個上下文視圖結構體

         CCreateContext ccx;

         ccx.m_pNewViewClass = RUNTIME_CLASS(CMyView);

         ccx.m_pCurrentFrame = this;

         ccx.m_pCurrentDoc = NULL;

         ccx.m_pNewDocTemplate = NULL;

         ccx.m_pLastView = NULL;

         // 使用結構體建立一個視圖

         m_pMainView = DYNAMIC_DOWNCAST( CMyView, this->CreateView(&ccx) );

         // 如果建立失敗

         if ( !m_pMainView )

         {

              TRACE0("Creation of view failed");

         }

         // 主架構重新布局

         RecalcLayout();

         // 顯示視圖

         m_pMainView->ShowWindow(SW_SHOW);

         m_pMainView->UpdateWindow();

         return 0;

     }

public:

     CMainFrame()

     {

         m_pMainView = NULL;

     }

     ~CMainFrame()

     {

     }

};

//實現動態建立能力

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

//實現訊息映射

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)

     ON_WM_CREATE()

END_MESSAGE_MAP()

// =======================================主應用程式類===========================================

class CMyApp : public CWinApp

{

public:

     CMyApp()

     {

     }

     virtual BOOL InitInstance()

     {

         CWinApp::InitInstance();

         CMainFrame *pFrame = new CMainFrame();

         pFrame->Create(NULL,(LPCTSTR)_T("hello"));

         this->m_pMainWnd = pFrame;

         this->m_pMainWnd->ShowWindow(SW_SHOW);

         this->m_pMainWnd->UpdateWindow();

         return TRUE;

     }

};

CMyApp theApp;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.