📄 right18-2.htm
字号:
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>第 18 章</title>
<base target="rbottom">
<style>
<!--
.右标题 { font-size: 10pt; color: #000080; text-indent: 0; margin: 0 }
.右内容 { font-size: 10pt; text-align: left; text-indent: 0; line-height: 100%; margin:
0 }
-->
</style>
</head>
<body>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><span style="position: absolute; left: 4; top: -8"><img border="0" src="1.gif" width="63" height="70"></span></p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left">
<span style="background-color: #CCFF99">第 18 章</span><span style="background-color: #CCFF99">
</span>><span style="background-color: #CCFF99"> 第 2 节 </span>><span style="background-color: #CCFF99">
18.2 MFC中与GDI有关的类</span></p>
<hr color="#008000" size="1">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font LANG="ZH-CN">为了支持</font>GDI<font LANG="ZH-CN">绘图,</font>MFC<font LANG="ZH-CN">提供了两种重要的类:设备描述表类,用于设置绘图属性和绘制图形;绘图对象类,封装了各种</font>GDI<font LANG="ZH-CN">绘图对象,包括画笔、刷子、字体、位图、调色板和区域。</font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">1.
<font LANG="ZH-CN">设备描述表类</font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font LANG="ZH-CN">设备描述表类包括</font>CDC<font LANG="ZH-CN">和它的派生类</font>CClientDC<font LANG="ZH-CN">、</font>CPaintDC<font LANG="ZH-CN">、</font>CWindowDC<font LANG="ZH-CN">、</font>CMetaFileDC<font LANG="ZH-CN">。</font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 5; margin-bottom: 0"><font LANG="ZH-CN" size="2">(1)
CDC类</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">CDC<font LANG="ZH-CN">是设备描述表类的基类,除了一般的窗口显示外,还用于基于桌面的全屏幕绘制和非屏幕显示的打印机输出。</font>CDC<font LANG="ZH-CN">类封装了所有图形输出函数,包括矢量、光栅和文本输出。</font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 5; margin-bottom: 0"><font LANG="ZH-CN" size="2">(2)
CClientDC类</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">CClientDC<font LANG="ZH-CN">(客户区设备描述表)用于客户区的输出,它在构造函数中封装了</font>GetDC(
)<font LANG="ZH-CN">,在析构函数中封装了</font>ReleaseDC( )<font LANG="ZH-CN">函数。一般在响应非窗口重画消息(如键盘输入时绘制文本、鼠标绘图)绘图时要用到它。一般用法如下:</font></font></p>
<blockquote>
<blockquote>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">CClientDC
dc(this); //this<font LANG="ZH-CN">一般指向本窗口或当前活动视图</font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">dc.TextOut(10,10,str,str.GetLength(
));</font></p>
</blockquote>
</blockquote>
<p ALIGN="justify" style="line-height: 100%; margin-top: 5; margin-bottom: 0"><font LANG="ZH-CN" size="2">(3)
CPaintDC</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">CPaintDC<font LANG="ZH-CN">用于响应窗口重绘消息(</font>WM_PAINT<font LANG="ZH-CN">)时的绘图输出。</font>CPaintDC<font LANG="ZH-CN">在构造函数中调用</font>BeginPaint(
)<font LANG="ZH-CN">取得设备描述表,在析构函数中调用</font>EndPaint(
)<font LANG="ZH-CN">释放设备描述表。</font>EndPaint( )<font LANG="ZH-CN">除了释放设备描述表外,还负责从消息队列中清除</font>WM_PAINT<font LANG="ZH-CN">消息。因此,在处理窗口重画时,必须使用</font>CPaintDC<font LANG="ZH-CN">,否则</font>WM_PAINT<font LANG="ZH-CN">消息无法从消息队列中清除,将引起不断的窗口重画。</font>CPaintDC<font LANG="ZH-CN">也只能用在</font>WM_PAINT<font LANG="ZH-CN">消息处理之中。</font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 5; margin-bottom: 0"><font LANG="ZH-CN" size="2">(4)
CWindowDC</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">CWindowDC<font LANG="ZH-CN">用于窗口客户区和非客户区(包括窗口边框、标题栏、控制按钮等)的绘制。除非要自己绘制窗口边框和按钮(如一些</font>CD<font LANG="ZH-CN">播放程序等),否则一般不用它。</font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 5; margin-bottom: 0"><font LANG="ZH-CN" size="2">(5)
CMetaFileDC</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">CMetaFileDC<font LANG="ZH-CN">专门用于图元文件的绘制。图元文件记录一组</font>GDI<font LANG="ZH-CN">命令,可以通过这一组</font>GDI<font LANG="ZH-CN">命令重建图形输出。使用</font>CMetaFileDC<font LANG="ZH-CN">时,所有的图形输出命令会自动记录到一个与</font>CMetaFileDC<font LANG="ZH-CN">相关的图元文件中。</font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">2.
<font LANG="ZH-CN">图形对象类</font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font LANG="ZH-CN">图形对象类包括</font>CGdiObject<font LANG="ZH-CN">、画笔、刷子、字体、位图、调色板、区域等。</font>CGdiObject<font LANG="ZH-CN">是图形对象类的基类,但该类不能直接为应用程序所使用。要使用</font>GDI<font LANG="ZH-CN">对象,必须使用它的派生类:画笔、刷子、字体、位图、区域等。</font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font LANG="ZH-CN" size="2" color="#FF9966">使用图形对象要注意两点:</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font LANG="ZH-CN">①</font>
<font LANG="ZH-CN">同其他</font>MFC<font LANG="ZH-CN">对象一样,</font>GDI<font LANG="ZH-CN">对象的创建也要分为两步:第一步,是定义一个</font>GDI<font LANG="ZH-CN">绘图对象类的实例;第二步调用该对象的创建方法真正创建对象。</font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font LANG="ZH-CN">②</font>
<font LANG="ZH-CN">创建图形对象之后,要使用该对象,首先要调用</font>CDC::SelectObject(
)<font LANG="ZH-CN">将它选入到设备描述表中,同时保存原来的设置到一个</font>GDI<font LANG="ZH-CN">对象指针,比如说</font>pOldObject<font LANG="ZH-CN">中。在使用完后,再用</font>SelectObject(pOldObject)<font LANG="ZH-CN">恢复原来的设置。但是,如果该设备描述表是用户自己创建的,则不必恢复原来设置,因为框架会在该设备描述表生存期结束时删除该设备描述表,同时也就删除了原来存放于该设备描述表中的绘图对象设置。</font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font LANG="ZH-CN" size="2">图形对象类:</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">(1)
画笔</font><font size="2"><font LANG="ZH-CN">类 </font><font lang="ZH-CN">
</font>CPen<font LANG="ZH-CN"></font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">(2)
画刷</font><font size="2"><font LANG="ZH-CN">类</font><font lang="ZH-CN">
</font>CBrush<font LANG="ZH-CN"></font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">(3)
字体</font><font size="2"><font LANG="ZH-CN">类 </font>CFont</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">(4)
位图</font><font size="2"><font LANG="ZH-CN">类</font><font lang="ZH-CN">
</font>CBitmap<font LANG="ZH-CN"></font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">(5)
调色板</font><font size="2"><font LANG="ZH-CN">类 </font>CPalette<font LANG="ZH-CN"></font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">(6)
区域</font><font size="2"><font LANG="ZH-CN">类 </font>CRgn<font LANG="ZH-CN"></font></font></p>
<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left"> </p>
<hr size="1" color="#008000">
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> <span style="position: absolute; left: 36; top: 872"><a href="right18-1.htm" target="_self"><img border="0" src="rightd1.gif" width="113" height="70"></a></span>
<span style="position: absolute; left: 502; top: 875"><a href="right18-3.htm" target="_self"><img border="0" src="rightd2.gif" width="124" height="63"></a></span></p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -