📄 chap7_5.htm
字号:
<html><head><meta http-equiv="Content-Type"content="text/html; charset=gb_2312-80"><meta name="GENERATOR" content="Microsoft FrontPage Express 2.0"><title>7.5不使用串行化的文档视结构程序</title><link rel="stylesheet" href="../../../cpcw.css"></head><body link="#3973DE" alink="#3973DE" background="../../bg.gif"><div align="center"><center><table border="0" cellpadding="0" cellspacing="0" width="85%"> <tr bgcolor="#FFFFFF"> <td> <div align="center"> <center> <table border="0" cellpadding="0" cellspacing="0" width="615" height="20"> </table> </center> </div> <p align="center"><b><font color="#FF3333" face="Times New Roman">7.5</font><font color="#FF3333">不使用串行化的文档视结构程序</font></b></p> <p><font > 在</font><font face="Times New Roman">MFC</font><font >例子中有一个</font><font face="Times New Roman">DIBLOOK</font><font >(见</font><font face="Times New Roman">SAMPLES\MFC\GENERAL\DIBLOOK</font><font >目录),它是一个位图显示程序,演示了在不使用串行化的情况下实现文档的输入输出功能。有关位图、调色板的使用在第十一章有详细介绍,这里只讨论与文档视结构相关的内容。我们先看</font><font face="Times New Roman">DIBLOOK</font><font >的文档声明和定义。</font></p> <p><font ><b>清单</b></font><font face="Times New Roman"><b>7-16 CDibDoc</b></font><font ><b>的类声明文件</b></font></p> <p><font face="Times New Roman">// dibdoc.h : interface of the CDibDoc class</font></p> <p><font face="Times New Roman">#include "dibapi.h"</font></p> <p><font face="Times New Roman">class CDibDoc : public CDocument</font></p> <p><font face="Times New Roman">{</font></p> <p><font face="Times New Roman">protected: // create from serialization only</font></p> <p><font face="Times New Roman">CDibDoc();</font></p> <p><font face="Times New Roman">DECLARE_DYNCREATE(CDibDoc)</font></p> <p><font face="Times New Roman">// Attributes</font></p> <p><font face="Times New Roman">public:</font></p> <p><font face="Times New Roman"><b>HDIB GetHDIB() const</b></font></p> <p><font face="Times New Roman"><b>{ return m_hDIB; }</b></font></p> <p><font face="Times New Roman"><b>CPalette* GetDocPalette() const</b></font></p> <p><font face="Times New Roman"><b>{ return m_palDIB; }</b></font></p> <p><font face="Times New Roman"><b>CSize GetDocSize() const</b></font></p> <p><font face="Times New Roman"><b>{ return m_sizeDoc; }</b></font></p> <p><font face="Times New Roman"><b>// Operations</b></font></p> <p><font face="Times New Roman"><b>public:</b></font></p> <p><font face="Times New Roman"><b>void ReplaceHDIB(HDIB hDIB);</b></font></p> <p><font face="Times New Roman"><b>void InitDIBData();</b></font></p> <p><font face="Times New Roman">// Implementation</font></p> <p><font face="Times New Roman">protected:</font></p> <p><font face="Times New Roman">virtual ~CDibDoc();</font></p> <p><font face="Times New Roman">virtual BOOL OnSaveDocument(LPCTSTR lpszPathName);</font></p> <p><font face="Times New Roman">virtual BOOL OnOpenDocument(LPCTSTR lpszPathName);</font></p> <p><font face="Times New Roman">protected:</font></p> <p><font face="Times New Roman">HDIB m_hDIB;</font></p> <p><font face="Times New Roman">CPalette* m_palDIB;</font></p> <p><font face="Times New Roman">CSize m_sizeDoc;</font></p> <p><font face="Times New Roman">#ifdef _DEBUG</font></p> <p><font face="Times New Roman">virtual void AssertValid() const;</font></p> <p><font face="Times New Roman">virtual void Dump(CDumpContext& dc) const;</font></p> <p><font face="Times New Roman">#endif</font></p> <p><font face="Times New Roman">protected:</font></p> <p><font face="Times New Roman">virtual BOOL OnNewDocument();</font></p> <p><font face="Times New Roman">// Generated message map functions</font></p> <p><font face="Times New Roman">protected:</font></p> <p><font face="Times New Roman">//{{AFX_MSG(CDibDoc)</font></p> <p><font face="Times New Roman">//}}AFX_MSG</font></p> <p><font face="Times New Roman">DECLARE_MESSAGE_MAP()</font></p> <p><font face="Times New Roman">};</font></p> <p><font face="Times New Roman">/////////////////////////////////////////////////////////////////////////////</font></p> <p><font ><b>清单</b></font><font face="Times New Roman"><b>7-17 CDibDoc</b></font><font ><b>类的实现文件</b></font></p> <p><font face="Times New Roman">// dibdoc.cpp : implementation of the CDibDoc class</font></p> <p><font face="Times New Roman">#include "stdafx.h"</font></p> <p><font face="Times New Roman">#include "diblook.h"</font></p> <p><font face="Times New Roman">#include <limits.h></font></p> <p><font face="Times New Roman">#include "dibdoc.h"</font></p> <p><font face="Times New Roman">#ifdef _DEBUG</font></p> <p><font face="Times New Roman">#undef THIS_FILE</font></p> <p><font face="Times New Roman">static char BASED_CODE THIS_FILE[] = __FILE__;</font></p> <p><font face="Times New Roman">#endif</font></p> <p><font face="Times New Roman">/////////////////////////////////////////////////////////////////////////////</font></p> <p><font face="Times New Roman">// CDibDoc</font></p> <p><font face="Times New Roman">IMPLEMENT_DYNCREATE(CDibDoc, CDocument)</font></p> <p><font face="Times New Roman">BEGIN_MESSAGE_MAP(CDibDoc, CDocument)</font></p> <p><font face="Times New Roman">//{{AFX_MSG_MAP(CDibDoc)</font></p> <p><font face="Times New Roman">//}}AFX_MSG_MAP</font></p> <p><font face="Times New Roman">END_MESSAGE_MAP()</font></p> <p><font face="Times New Roman">/////////////////////////////////////////////////////////////////////////////</font></p> <p><font face="Times New Roman">// CDibDoc construction/destruction</font></p> <p><font face="Times New Roman">CDibDoc::CDibDoc()</font></p> <p><font face="Times New Roman">{</font></p> <p><font face="Times New Roman"><b>//</b></font><font ><b>初始化文档的</b></font><font face="Times New Roman"><b>DIB</b></font><font ><b>句柄和调色板</b></font></p> <p><font face="Times New Roman"><b>m_hDIB = NULL;</b></font></p> <p><font face="Times New Roman"><b>m_palDIB = NULL;</b></font></p> <p><font face="Times New Roman"><b>m_sizeDoc = CSize(1,1); // dummy value to make CScrollView happy</b></font></p> <p><font face="Times New Roman">}</font></p> <p><font face="Times New Roman">CDibDoc::~CDibDoc()</font></p> <p><font face="Times New Roman">{</font></p> <p><font face="Times New Roman"><b>if (m_hDIB != NULL)</b></font></p> <p><font face="Times New Roman"><b>{</b></font></p> <p><font face="Times New Roman"><b>::GlobalFree((HGLOBAL) m_hDIB);</b></font></p> <p><font face="Times New Roman"><b>}</b></font></p> <p><font face="Times New Roman"><b>if (m_palDIB != NULL)</b></font></p> <p><font face="Times New Roman"><b>{</b></font></p> <p><font face="Times New Roman"><b>delete m_palDIB;</b></font></p> <p><font face="Times New Roman"><b>}</b></font></p> <p><font face="Times New Roman">}</font></p> <p><font face="Times New Roman">BOOL CDibDoc::OnNewDocument()</font></p> <p><font face="Times New Roman">{</font></p> <p><font face="Times New Roman">if (!CDocument::OnNewDocument())</font></p> <p><font face="Times New Roman">return FALSE;</font></p> <p><font face="Times New Roman">return TRUE;</font></p> <p><font face="Times New Roman">}</font></p> <p><font face="Times New Roman"><b>void CDibDoc::InitDIBData()</b></font></p> <p><font face="Times New Roman"><b>{</b></font></p> <p><font face="Times New Roman"><b>if (m_palDIB != NULL)</b></font></p> <p><font face="Times New Roman"><b>{</b></font></p> <p><font face="Times New Roman"><b>delete m_palDIB;</b></font></p> <p><font face="Times New Roman"><b>m_palDIB = NULL;</b></font></p> <p><font face="Times New Roman"><b>}</b></font></p> <p><font face="Times New Roman"><b>if (m_hDIB == NULL)</b></font></p> <p><font face="Times New Roman"><b>{</b></font></p> <p><font face="Times New Roman"><b>return;</b></font></p> <p><font face="Times New Roman"><b>}</b></font></p> <p><font face="Times New Roman"><b>// Set up document size</b></font></p> <p><font face="Times New Roman"><b>LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB);</b></font></p> <p><font face="Times New Roman"><b>if (::DIBWidth(lpDIB) > INT_MAX ||::DIBHeight(lpDIB) > INT_MAX)</b></font></p> <p><font face="Times New Roman"><b>{</b></font></p> <p><font face="Times New Roman"><b>::GlobalUnlock((HGLOBAL) m_hDIB);</b></font></p> <p><font face="Times New Roman"><b>::GlobalFree((HGLOBAL) m_hDIB);</b></font></p> <p><font face="Times New Roman"><b>m_hDIB = NULL;</b></font></p> <p><font face="Times New Roman"><b>CString strMsg;</b></font></p> <p><font face="Times New Roman"><b>strMsg.LoadString(IDS_DIB_TOO_BIG);</b></font></p> <p><font face="Times New Roman"><b>MessageBox(NULL, strMsg, NULL, MB_ICONINFORMATION | MB_OK);</b></font></p> <p><font face="Times New Roman"><b>return;</b></font></p> <p><font face="Times New Roman"><b>}</b></font></p> <p><font face="Times New Roman"><b>m_sizeDoc = CSize((int) ::DIBWidth(lpDIB), (int) ::DIBHeight(lpDIB));</b></font></p> <p><font face="Times New Roman"><b>::GlobalUnlock((HGLOBAL) m_hDIB);</b></font></p> <p><font face="Times New Roman"><b>// Create copy of palette</b></font></p> <p><font face="Times New Roman"><b>m_palDIB = new CPalette;</b></font></p> <p><font face="Times New Roman"><b>if (m_palDIB == NULL)</b></font></p> <p><font face="Times New Roman"><b>{</b></font></p> <p><font face="Times New Roman"><b>// we must be really low on memory</b></font></p> <p><font face="Times New Roman"><b>::GlobalFree((HGLOBAL) m_hDIB);</b></font></p> <p><font face="Times New Roman"><b>m_hDIB = NULL;</b></font></p> <p><font face="Times New Roman"><b>return;</b></font></p> <p><font face="Times New Roman"><b>}</b></font></p> <p><font face="Times New Roman"><b>if (::CreateDIBPalette(m_hDIB, m_palDIB) == NULL)</b></font></p> <p><font face="Times New Roman"><b>{</b></font></p> <p><font face="Times New Roman"><b>// DIB may not have a palette</b></font></p> <p><font face="Times New Roman"><b>delete m_palDIB;</b></font></p> <p><font face="Times New Roman"><b>m_palDIB = NULL;</b></font></p> <p><font face="Times New Roman"><b>return;</b></font></p> <p><font face="Times New Roman"><b>}</b></font></p> <p><font face="Times New Roman"><b>}</b></font></p> <p><font face="Times New Roman">BOOL CDibDoc::OnOpenDocument(LPCTSTR lpszPathName)</font></p> <p><font face="Times New Roman">{</font></p> <p><font face="Times New Roman"><b>CFile file;</b></font></p> <p><font face="Times New Roman"><b>CFileException fe;</b></font></p> <p><font face="Times New Roman"><b>if (!file.Open(lpszPathName, CFile::modeRead | CFile::shareDenyWrite, &fe))</b></font></p> <p><font face="Times New Roman"><b>{</b></font></p> <p><font face="Times New Roman"><b>ReportSaveLoadException(lpszPathName, &fe,</b></font></p> <p><font face="Times New Roman"><b>FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);</b></font></p> <p><font face="Times New Roman"><b>return FALSE;</b></font></p> <p><font face="Times New Roman"><b>}</b></font></p> <p><font face="Times New Roman"><b>DeleteContents();</b></font></p> <p><font face="Times New Roman"><b>BeginWaitCursor();</b></font></p> <p><font face="Times New Roman"><b>// replace calls to Serialize with ReadDIBFile function</b></font></p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -