?? 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">
<tr>
<td width="377" bgcolor="#15397D" height="20"><font
size="5"></font> </td>
<td width="238" bgcolor="#000000" height="20">
<p
align="right"><a href="../../vc.htm"><span style="text-decoration: none"><font
color="#FFFFFF">電腦報Visual C++網絡教程</font></span></a></p>
</td>
</tr>
</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>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -