当前位置:文档之家› vc 6.0打造单文档界面与多文档界面的编程

vc 6.0打造单文档界面与多文档界面的编程

vc++6.0打造单文档界面与多文档界面的编程VC++6中,可以利用MFC向导分别创建单文档和多文档界面的应用程序。

有了这两种类型的应用程序,您就可以实现简单的记事本,写字本和WORD的功能了。

以下介绍一个在文档中输出一段文字,点击菜单项,输入文字内容按OK按钮后可以更改输出的文字。

1.首先,点击MFC向导,选择多文档视图类型,新建一个名为MdiDemo的工程。

2.新建一个对话框。

上面放置一个Static控件和一个Edit控件,按CTRL+W,将Edit控件改成m_Caption.3.新建一个类与对话框对应,如CDlgDemo类。

4.在菜单中,增加一项Demo,并增加菜单项Test.按CTRL+W,并双击后,在消息的上面选择Doc,下面选择Command,双击就产生了对应的事件处理函数。

5.在MdiDemoDoc.cpp类中输入以下:// DlgDemo.cpp : implementation file//#include "stdafx.h"#include "MdiDemo.h"#include "DlgDemo.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CDlgDemo dialogCDlgDemo::CDlgDemo(CWnd* pParent /*=NULL*/): CDialog(CDlgDemo::IDD, pParent){//{{AFX_DATA_INIT(CDlgDemo)m_Caption = _T("");//}}AFX_DATA_INIT}void CDlgDemo::DoDataExchange(CDataExchange* pDX) {CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CDlgDemo)DDX_Text(pDX, IDC_EDIT1, m_Caption);//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CDlgDemo, CDialog)//{{AFX_MSG_MAP(CDlgDemo)// NOTE: the ClassWizard will add message map macros here//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CDlgDemo message handlers6.在MdiDemoView.cpp中输入以下内容:// MdiDemoView.cpp : implementation of the CMdiDemoView class//#include "stdafx.h"#include "MdiDemo.h"#include "MdiDemoDoc.h"#include "MdiDemoView.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CMdiDemoViewIMPLEMENT_DYNCREATE(CMdiDemoView, CView) BEGIN_MESSAGE_MAP(CMdiDemoView, CView)//{{AFX_MSG_MAP(CMdiDemoView)// NOTE - the ClassWizard will add and remove mapping macros here.// DO NOT EDIT what you see in these blocks of generated code!//}}AFX_MSG_MAP// Standard printing commandsON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT,CView::OnFilePrint)ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CMdiDemoView construction/destruction CMdiDemoView::CMdiDemoView(){// TODO: add construction code here}CMdiDemoView::~CMdiDemoView(){}BOOLCMdiDemoView::PreCreateWindow(CREATESTRUCT&a mp; cs){// TODO: Modify the Window class or styles here by modifying// the CREATESTRUCT csreturn CView::PreCreateWindow(cs);}/////////////////////////////////////////////////////////////////////////////// CMdiDemoView drawingvoid CMdiDemoView::OnDraw(CDC* pDC){CMdiDemoDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);// TODO: add draw code for native data hereCRect crect;GetClientRect(&crect);CSize sizeClient=crect.Size();CString str=pDoc->strCaption;CSize sizeTextExtent=pDC->GetTextExtent(str);pDC->TextOut((sizeClient.cx-sizeTextExtent.cx)/2,(size Client.cy-sizeTextExtent.cy)/2,str);}/////////////////////////////////////////////////////////////////////////////// CMdiDemoView printingBOOL CMdiDemoView::OnPreparePrinting(CPrintInfo* pInfo){// default preparationreturn DoPreparePrinting(pInfo);}void CMdiDemoView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/){// TODO: add extra initialization before printing}void CMdiDemoView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/){// TODO: add cleanup after printing}/////////////////////////////////////////////////////////////////////////////// CMdiDemoView diagnostics#ifdef _DEBUGvoid CMdiDemoView::AssertValid() const{CView::AssertValid();}void CMdiDemoView::Dump(CDumpContext& dc) const{CView::Dump(dc);}CMdiDemoDoc* CMdiDemoView::GetDocument() //non-debug version is inline{ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMdiDemoDoc)));return (CMdiDemoDoc*)m_pDocument;}#endif //_DEBUG///////////////////////////////////////////////////////////////////////////// // CMdiDemoView message handlers6。

按“CTRL+F5”即可运行。

相关主题