《C语言实例教程(PDF格式)》第100章


void CKeyView::OnChar(UINT nChar; UINT nRepCnt; UINT nFlags)

CKeyDoc *pDoc=GetDocument();
pDoc…》data_string+=nChar;
// 。。。
CView::OnChar(nChar; nRepCnt; nFlags);

下面接下来的一步是把这个数据显示在屏幕上 (这是视窗的任务), 
此时最简单的办法是用类CClientDC来生成一个对应于视窗中的用户 
区的新的设备描述表,使用该设备描述表要比使用别的更一般的设备 
描述表容易。为此,需要把一个指向该对象的指针传给CClientDC 的 
构造函数,这可以通过关键字this完成。即:
void CKeyView::OnChar(UINT nChar; UINT nRepCnt; UINT nFlags)

// TODO: Add your message handler code here and/or call default
CKeyDoc *pDoc=GetDocument();
pDoc…》data_string+=nChar;
// 。。。
CView::OnChar(nChar; nRepCnt; nFlags);
CClientDC dc(this);
// 。。。

现在已经有了一个附加在视窗上的设备描述表,可以象以前一样使用 
TextOut 在它里面打印,打印pDoc…》data_string的代码如下:
void CKeyView::OnChar(UINT nChar; UINT nRepCnt; UINT nFlags)

…………………………………………………………Page 464……………………………………………………………
// TODO: Add your message handler code here and/or call default
CKeyDoc *pDoc=GetDocument();
pDoc…》data_string+=nChar;
// 。。。
CView::OnChar(nChar; nRepCnt; nFlags);
CClientDC dc(this);
dc。TextOut(0;0;pDoc…》data_string;Pdoc…》data_string。GetLength );
// 。。。

以上即为OnChar的全部代码。现在当键入串时,它将显示在屏幕上, 
读取键击的程序取得了成功。图8。13为一个运行画面。
l 注意:
l 别忘了在CKeyDoc。h中加入数据data_string的声明:
l CString data_string;
图8。13 生成一个视
l 注意:
l 在程序中,很明显的还有很大的缺陷:
…………………………………………………………Page 465……………………………………………………………
1。 虽然我们的程序是为单文档服务的,在多文档程序中,还 
需要在一个文档发生改变时通知其所有视图更新自己的数 
据,则可以通过下列的函数调用 UpdateAllViews来完成。当 
然,对于我们现在的单文档程序还没有涉及 到这一点。
2。 在视窗中的输入在窗口失去再重新得到焦点时,数据不能 
被正确显示。这应该在窗口重绘函数OnDraw中考虑。
3。 如果按通常的字处理程序来看,该程序至少还需要一个适 
宜的插入符。这点我们在以后的程序中会作出事例。
4。 视的滚动及缩放处理我们放到多文档界面中再作详细解 
释。下面,我们列出将上述问题加以解决后的函数OnChar和 
重绘函数OnDraw:
l void CKeyView::OnChar(UINT nChar; UINT nRepCnt; UINT nFlags)
l {
l // TODO: Add your message handler code here and/or call default
l CKeyDoc *pDoc=GetDocument();
l pDoc…》data_string+=nChar;
l CView::OnChar(nChar; nRepCnt; nFlags);
l CClientDC dc(this);
l dc。TextOut(0;0;pDoc…》data_string;pDoc…》data_string。GetLength());
l pDoc…》UpdateAllViews(this;0l;0);
l }
l
l void CKeyView::OnDraw(CDC* pDC)
l {
l CKeyDoc* pDoc = GetDocument();
l ASSERT_VALID(pDoc);
…………………………………………………………Page 466……………………………………………………………
l 
l // TODO: add draw code for native data here
l pDC…》TextOut(0;0;pDoc…》data_string;pDoc…》data_string。GetLength());
l }
在本节的末尾,我们给出一个较长一些的例子。它使用了有关视的一 
些典型方法,能打开VC程序的源代码,同时,其中也涉及到一些文件 
操作,但由于我们使用的是标准的文件处理对话框,应该不会对读者 
的阅读带来很大困难。但即使暂时不能全部看懂也没有关系,在本章 
的随后的几节里,我们会作尽量详细的解释。同时,我们也只是在其 
中简单的使用了视图的滚动处理。更详细的解释,我们认为留到多文 
档程序中讲解会更容易接受。
下面给出程序的一部分运行画面 (图8。14到图8。16)。
程序源代码 (由于篇幅原因,对程序代码没有作详细解释):
// filelist。h : main header file for the FILELIST application
//
#ifndef __AFXWIN_H__
#error include "stdafx。h" before including this file for PCH
#endif
#include 〃resource。h〃 // main symbols
/////////////////////////////////////////////////////////////////////////////
// DApp:
// See filelist。cpp for the implementation of this class
//
…………………………………………………………Page 467……………………………………………………………
图8。14 打开文件
图8。15 对程序选择字体
class DApp : public CWinApp

public:
DApp();
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(DApp)
public:
…………………………………………………………Page 468……………………………………………………………
virtual BOOL InitInstance();
//}}AFX_VIRTUAL
图8。16 字体已被改变
// Implementation
//{{AFX_MSG(DApp)
afx_msg void OnAppAbout();
// NOTE the ClassWizard will add and remove member functions here。
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
// filelist。cpp: This program opens and displays text files。
//
#include 〃stdafx。h〃
#include 〃filelist。h〃
#include 〃mainfrm。h〃
#ifdef _DEBUG
…………………………………………………………Page 469……………………………………………………………
#undef THIS_FILE
static char BASED_CODE THIS_FILE'' = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// DApp
BEGIN_MESSAGE_MAP(DApp; CWinApp)
//{{AFX_MSG_MAP(DApp)
ON_MAND(ID_APP_ABOUT; OnAppAbout)
小说推荐
返回首页返回目录