Show toolbar
顯示具有 Doc/View 標籤的文章。 顯示所有文章
顯示具有 Doc/View 標籤的文章。 顯示所有文章

2013年8月5日 星期一

Create Static Splitter Window using MFC

標題:使用MFC建立靜態分割視窗
VC++ (SplitterWindow.cpp):
範例結果:


動態教學:
Open
直接下載:
http://docs.google.com/uc?id=0B8HdyDgIiLuHREdOUmVuSUtoZzA

說明:
在MFC中建立分割視窗,左視窗為CFrameWnd類別可設計表單,右視窗為CView類別可進行繪圖,注意須建立『IDD_FORMVIEW』之Dialog資源。

2013年4月26日 星期五

Create Document/View using MFC

標題:MFC之Document/View架構
VC++ (DocView.cpp):
#include <afxwin.h>
#include "DocView.h"

class DocBoxy : public CDocument //Document
{
    //在此撰寫Document內容
    DECLARE_DYNCREATE(DocBoxy) //宣告run-time類別
};

IMPLEMENT_DYNCREATE(DocBoxy, CDocument) //宣告DocBoxy為run-time類別

class FrameBoxy : public CFrameWnd //Frame
{
public:
    afx_msg void OnExit() //宣告關閉檔案事件
    {
        if(MessageBox( TEXT("Are you sure you want to close this window?"), TEXT("DocViewBoxy"), MB_OKCANCEL | MB_ICONQUESTION ) == 1 )
        {
            DestroyWindow();
        }
    }

    //在此撰寫Frame內容
    DECLARE_DYNCREATE(FrameBoxy) //宣告run-time類別
    DECLARE_MESSAGE_MAP() //宣告訊息映射表
};

//宣告FrameBoxy為run-time類別
IMPLEMENT_DYNCREATE(FrameBoxy, CFrameWnd)

//建立訊息映射表
BEGIN_MESSAGE_MAP(FrameBoxy, CFrameWnd)
    ON_COMMAND(ID_FILE_EXIT, OnExit)
END_MESSAGE_MAP()

class ViewBoxy : public CView //View
{
public:
    void OnDraw(CDC * aDC) //必須覆寫的虛擬函數
    {
        for(int i=0;i<10;i++)
        {
            aDC->TextOut(20 * i, 20 * i, L"HelloBoxy!!");
        }
    }
    //在此撰寫View內容
    DECLARE_DYNCREATE(ViewBoxy) //宣告run-time類別
};

IMPLEMENT_DYNCREATE(ViewBoxy, CView) //宣告ViewBoxy為run-time類別

class AppBoxy : public CWinApp
{
public:
    BOOL InitInstance() //程式進入點
    {
        CDocument *doc;
        CSingleDocTemplate* DocTemplate;

        DocTemplate = new CSingleDocTemplate(
            IDR_MENU,
            RUNTIME_CLASS(DocBoxy),   //Document
            RUNTIME_CLASS(FrameBoxy), //Frame
            RUNTIME_CLASS(ViewBoxy)   //View
        );

        AddDocTemplate(DocTemplate);
        doc = DocTemplate->CreateNewDocument();

        m_pMainWnd = DocTemplate->CreateNewFrame( doc, NULL );
        DocTemplate->InitialUpdateFrame ( (CFrameWnd*)m_pMainWnd, doc );
        m_pMainWnd->ShowWindow(SW_SHOW);

        return true;
    }
};

AppBoxy appboxy; //建立應用程式物件
範例結果:


說明:
在Win32模式下建立MFC之Document/View架構之範例程式碼, 須建立『DocView.rc』資源檔及自動產生『DocView.h』並加入Menu指定ID為『IDR_MENU』。