// MFCDoc.cpp : Defines the entry point for the application.
//
CDocTemplate
#include "stdafx.h"
#include "resource.h"
class CMyDocument : public CDocument
{
public:
CMyDocument( );
//我们加入了很多的消息映射 都是映射一个命令 可是它会执行那个呢
//下面也相当于是WM_COMMAND命令顺序
//1.CDocView 2.COocument 3.CFrameWnd 4.CWinApp 如果再查不到就丢弃
//WM_COMMAND 发送到程序的顶层窗口FrameWnd
//FrameWnd获取当前活动视图将命令发送到Vew
//在View中进行消息处理然后派发给CDocument
//CDocument进行消息处理返回FrameWnd ,FrameWnd 自己进行消息处理
//派发到CWinApp进行消息处理
DECLARE_DYNAMIC( CMyDocument )
DECLARE_MESSAGE_MAP( );
public:
afx_msg void OnTest( );
public:
CString m_strText;
};
IMPLEMENT_DYNAMIC( CMyDocument, CDocument )
BEGIN_MESSAGE_MAP( CMyDocument, CDocument )
ON_COMMAND( ID_TEST, OnTest )
END_MESSAGE_MAP( )
CMyDocument::CMyDocument( )
{
m_strText = "Hello Document";
}
void CMyDocument::OnTest( )
{
AfxMessageBox( "CMyDocument::OnTest" );
}
class CDocView : public CEditView
{
public:
CDocView( );
virtual void OnInitialUpdate( );
DECLARE_DYNCREATE( CDocView )
DECLARE_MESSAGE_MAP( )
public:
afx_msg void OnTest( );
};
IMPLEMENT_DYNCREATE( CDocView, CEditView )
BEGIN_MESSAGE_MAP( CDocView, CEditView )
ON_COMMAND( ID_TEST, OnTest )
END_MESSAGE_MAP( )
CDocView::CDocView( )
{
}
void CDocView::OnInitialUpdate( )
{
//获取当前的文档
CDocument * pDoc = GetDocument( );
if( FALSE == pDoc->IsKindOf(
RUNTIME_CLASS(CMyDocument) ) )
{
return;
}
//显示字符串
CMyDocument * pMyDoc =
(CMyDocument *)pDoc;
SetWindowText( pMyDoc->m_strText );
}
void CDocView::OnTest( )
{
AfxMessageBox( "CDocView::OnTest" );
}
class CDocFrame : public CFrameWnd
{
DECLARE_MESSAGE_MAP( )
public:
afx_msg void OnTest( );
};
BEGIN_MESSAGE_MAP( CDocFrame, CFrameWnd )
ON_COMMAND( ID_TEST, OnTest )
END_MESSAGE_MAP( )
void CDocFrame::OnTest( )
{
AfxMessageBox( "CDocFrame::OnTest" );
}
class CDocApp : public CWinApp
{
public:
virtual BOOL InitInstance( );
DECLARE_MESSAGE_MAP( )
public:
afx_msg void OnTest( );
};
BEGIN_MESSAGE_MAP( CDocApp, CWinApp )
ON_COMMAND( ID_TEST, OnTest )
END_MESSAGE_MAP( )
void CDocApp::OnTest( )
{
AfxMessageBox( "CDocApp::OnTest" );
}
CDocApp theApp;
BOOL CDocApp::InitInstance( )
{
CDocFrame * pWnd = new CDocFrame( );
CCreateContext context;
context.m_pNewViewClass =
RUNTIME_CLASS( CDocView );
//增加文档类的支持
context.m_pCurrentDoc =
new CMyDocument( );
pWnd->LoadFrame( IDR_MAINFRM,
WS_OVERLAPPEDWINDOW, NULL,
&context );
m_pMainWnd = pWnd;
m_pMainWnd->ShowWindow( SW_SHOW );
m_pMainWnd->UpdateWindow( );
//更新当前所有View
pWnd->InitialUpdateFrame( NULL, TRUE );
return TRUE;
}
程序连接:http://pan.baidu.com/s/1i3olm3V
MFC加载CDocView 类似于文本编辑器使用14.5.5
最新推荐文章于 2024-06-04 09:52:47 发布