图7.1 最初的Example工程界面 |
//{{NO_DEPENDENCIES}} // Microsoft Visual C++ generated include file. // Used by EXAMPLE.RC // #define IDD_ABOUTBOX 100 #define IDR_MAINFRAME 128 //#define IDR_EXAMPLTYPE 129 #define IDR_TEXTTYPE 10001 #define IDR_BMPTYPE 10002 … #endif STRINGTABLE PRELOAD DISCARDABLE BEGIN IDR_MAINFRAME "Example" IDR_EXAMPLTYPE "\nExampl\nExampl\n\n\nExample.Document\nExampl Document" IDR_TEXTTYPE "\nTEXT\nTEXT\nExampl 文件 (*.txt)\n.txt\nTEXT\nTEXT Document" IDR_BMPTYPE "\nBMP\nBMP\nExampl 文件 (*.bmp)\n.bmp\nBMP\nBMP Document" END |
BOOL CExampleApp::InitInstance() { … CMultiDocTemplate* pDocTemplate; pDocTemplate = new CMultiDocTemplate( IDR_TEXTTYPE, //对应文本文件的字符串 RUNTIME_CLASS(CExampleDoc), RUNTIME_CLASS(CChildFrame), // custom MDI child frame RUNTIME_CLASS(CExampleView)); AddDocTemplate(pDocTemplate); … } |
class CExampleDoc : public CDocument { … CString m_Text; //在文档类中定义成员变量用于存储TXT文件中的字符串 … } //重载文档类的Serialize,读取字符串到m_Text中 void CExampleDoc::Serialize(CArchive& ar) { if (ar.IsStoring()) { // TODO: add storing code here } else { // TODO: add loading code here ar.ReadString(m_Text); } } //重载视图类的OnDraw函数,显示文档中的字符串 ///////////////////////////////////////////////////////////////////////////// // CExampleView drawing void CExampleView::OnDraw(CDC* pDC) { CExampleDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here pDC->TextOut(0,0,pDoc->m_Text); } |
图7.2 打开TXT文件的界面 |
#include "BMPDocument.h" #include "BMPView.h" |
pDocTemplate = new CMultiDocTemplate( //IDR_EXAMPLTYPE, IDR_BMPTYPE, RUNTIME_CLASS(CBMPDocument), RUNTIME_CLASS(CChildFrame), // custom MDI child frame RUNTIME_CLASS(CBMPView)); AddDocTemplate(pDocTemplate); |
图7.3 包含多个文档模板后的"新建" |
图7.4 包含多个文档模板后的"打开" |
class CBMPView : public CView { … CBMPDocument* GetDocument(); //头文件中声明 … } //重载CBMPView::GetDocument函数 CBMPDocument* CBMPView::GetDocument() { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CBMPDocument))); return (CBMPDocument*)m_pDocument; } |
void CBMPView::OnDraw(CDC* pDC) { CBMPDocument* pDoc = GetDocument(); // TODO: add draw code here CDib dib; dib.Load(pDoc->GetPathName()); dib.SetPalette(pDC); dib.Draw(pDC); } |
图7.5 打开位图的界面 |
图7.6 同时打开位图和文本的界面 |
class CBMPDocument : public CDocument { … public: unsigned char bmpBit[MAX_BITMAP]; } void CBMPDocument::Serialize(CArchive& ar) { if (ar.IsStoring()) { // TODO: add storing code here } else { // TODO: add loading code here CFile *file = ar.GetFile(); for(int i=0;i<file->GetLength();i++) { ar >> bmpBit[i]; } } } |
class CBMPChildFrame : public CMDIChildWnd { … public: CSplitterWnd m_wndSplitter; //定义拆分 … } |
BOOL CBMPChildFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext *pContext) { // TODO: Add your specialized code here and/or call the base class CRect rect; GetClientRect(&rect); m_wndSplitter.CreateStatic(this, 1, 2); m_wndSplitter.CreateView(0, 0, pContext->m_pNewViewClass, CSize(rect.right /2, 0), pContext); m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CBMPDataView), CSize(0, 0),pContext); m_wndSplitter.SetActivePane(0, 0); return true; } |
void CBMPDataView::OnDraw(CDC* pDC) { CBMPDocument* pDoc = GetDocument(); // TODO: add draw code here CString str; char tmp[3]; for(int i=0;i<20;i++)//假设只显示20行,每行20个字符 { str = ""; for (int j =0;j<20;j++) { memset(tmp,0,4); itoa(pDoc->bmpBit[10*i+j],tmp,16); str+=CString(tmp)+" "; } pDC->TextOut(0,20*i,str); } } |
图7.7 用两种视图来显示位图的界面 图7.8 "多视图+多文档"的界面 |
原文地址:http://blog.csdn.net/luguifang2011/article/details/42060991