⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 teach_road_21.htm

📁 vc++设计宝典教程2
💻 HTM
字号:
<html><head><meta http-equiv="Content-Type"content="text/html; charset=gb_2312-80"><title>VC Road</title><style type="text/css" MEDIA="screen"><!--a {text-decoration: underline;}a:hover {color:red; text-decoration:underline}small {color: navy; font-size: 12px ; line-height:16px;}h1 {color: navy; font-size: 12px ; line-height:16px;}

big {color: navy; font-size: 22px ; line-height:26px;}
td {color: navy; font-size: 12px ; line-height:16px;bgcolor="#FFCFA3";}--></style></head>
<body background="../img/ground.gif">
<!-- 耐特付费广告代码开始 不得修改. -->
<center><script language="javascript">
<!--
  var date = new Date();
  var ra = date.getTime() % 1000;
  var ua = document.URL;
  document.write("<iframe src='http://www.china-free.com/cgi-bin/ad/random.cgi?id=560' width=468 height=60 scrolling=no marginwidth=0 marginheight=0 frameborder=0 vspace=0 hspace=0 >");
  document.write("<a href='http://www.china-free.com/cgi-bin/ad/random.cgi?id=560' target='_blank'>");
  document.write("<img src='http://www.china-free.com/cgi-bin/ad/random.cgi?job=go&id=560' width=468 height=60 border=0></a>");
  document.write("</iframe>");
//-->
</script>
<noscript>
<a target="_blank" href="http://www.china-free.com/cgi-bin/ad/random.cgi?id=560">
<img src="http://www.china-free.com/cgi-bin/ad/random.cgi?job=go&id=560" width="468" height="60" border="0"></a>
</noscript>
<br><a HREF="http://www.china-free.com/ad" target="_blank"><img SRC="http://www.china-free.com/ad/bd.gif" BORDER="0"  ></a></center>
<!-- 耐特付费广告代码结尾. -->

<p align=center><big>2.1 和GUI有关的各种对象</big></p>

<table border=0 align=center width=80%>
<tr><td>
<p>在Windows中有各种GUI对象(不要和C++对象混淆),当你在进行绘图就需要利用这些对象。而各种对象都拥有各种属性,下面分别讲述各种GUI对象和拥有的属性。</p>
<p>字体对象CFont用于输出文字时选用不同风格和大小的字体。可选择的风格包括:是否为斜体,是否为粗体,字体名称,是否有下划线等。<font color=red>颜色和背景色不属于字体的属性。</font>关于如何创建和使用字体在<a href=teach_road_22.htm>2.2 在窗口中输出文字</a>中会详细讲解。</p>
<p>刷子CBrush对象决定填充区域时所采用的颜色或模板。对于一个固定色的刷子来讲它的属性为颜色,是否采用网格和网格的类型如水平的,垂直的,交叉的等。你也可以利用8*8的位图来创建一个自定义模板的刷子,在使用这种刷子填充时系统会利用位图逐步填充区域。关于如何创建和使用刷子在<a href=teach_road_23.htm>2.3 使用刷子,笔进行绘图</a>中会详细讲解。</p>
<p>画笔CPen对象在画点和画线时有用。它的属性包括颜色,宽度,线的风格,如虚线,实线,点划线等。关于如何创建和使用画笔在<a href=teach_road_23.htm>2.3 使用刷子,笔进行绘图</a>中会详细讲解。</p>
<p>位图CBitmap对象可以包含一幅图像,可以保存在资源中。关于如何使用位图在<a href=teach_road_24.htm>2.4 在窗口中绘制设备相关位图,图标,设备无关位图</a>中会详细讲解。</p>
<p>还有一种特殊的GUI对象是多边形,利用多边形可以很好的限制作图区域或是改变窗口外型。关于如何创建和使用多边形在<a href=teach_road_26.htm>2.6 多边形和剪贴区域</a>中会详细讲解。</p>
<p>在Windows中使用GUI对象必须遵守一定的规则。首先需要创建一个合法的对象,不同的对象创建方法不同。然后需要将该GUI对象选入DC中,同时保存DC中原来的GUI对象。<font color=red>如果选入一个非法的对象将会引起异常</font>。在使用完后应该恢复原来的对象,这一点特别重要,<font color=red>如果保存一个临时对象在DC中,而在临时对象被销毁后可能引起异常。</font>有一点必须注意,<font color=red>每一个对象在重新创建前必须销毁</font>,下面的代码演示了这一种安全的使用方法:
<pre>
OnDraw(CDC* pDC)
{
	CPen pen1,pen2;
	pen1.CreatePen(PS_SOLID,2,RGB(128,128,128));//创建对象
	pen2.CreatePen(PS_SOLID,2,RGB(128,128,0));//创建对象
	CPen* pPenOld=(CPen*)pDC->SelectObject(&pen1);//选择对象进DC
	drawWithPen1...
	(CPen*)pDC->SelectObject(&pen2);//选择对象进DC
	drawWithPen2...
	pen1.DeleteObject();//再次创建前先销毁
	pen1.CreatePen(PS_SOLID,2,RGB(0,0,0));//再次创建对象
	(CPen*)pDC->SelectObject(&pen1);//选择对象进DC
	drawWithPen1...
	pDC->SelectObject(pOldPen);//恢复
}
</pre></p>
<p>此外系统中还拥有一些库存GUI对象,你可以利用CDC::SelectStockObject(SelectStockObject( int nIndex )选入这些对象,它们包括一些固定颜色的刷子,画笔和一些基本字体。
<ul>
<li>
<b>BLACK_BRUSH</b>&nbsp;&nbsp;&nbsp;Black brush.<br><br></li>
<li>
<b>DKGRAY_BRUSH</b>&nbsp;&nbsp;&nbsp;Dark gray brush.<br><br></li>
<li>
<b>GRAY_BRUSH</b>&nbsp;&nbsp;&nbsp;Gray brush.<br><br></li>
<li>
<b>HOLLOW_BRUSH</b>&nbsp;&nbsp;&nbsp;Hollow brush.<br><br></li>
<li>
<b>LTGRAY_BRUSH</b>&nbsp;&nbsp;&nbsp;Light gray brush.<br><br></li>
<li>
<b>NULL_BRUSH</b>&nbsp;&nbsp;&nbsp;Null brush.<br><br></li>
<li>
<b>WHITE_BRUSH</b>&nbsp;&nbsp;&nbsp;White brush.<br><br></li>
<li>
<b>BLACK_PEN</b>&nbsp;&nbsp;&nbsp;Black pen.<br><br></li>
<li>
<b>NULL_PEN</b>&nbsp;&nbsp;&nbsp;Null pen.<br><br></li>
<li>
<b>WHITE_PEN</b>&nbsp;&nbsp;&nbsp;White pen.<br><br></li>
<li>
<b>ANSI_FIXED_FONT</b>&nbsp;&nbsp;&nbsp;ANSI fixed system font.<br><br></li>
<li>
<b>ANSI_VAR_FONT</b>&nbsp;&nbsp;&nbsp;ANSI variable system font.<br><br></li>
<li>
<b>DEVICE_DEFAULT_FONT</b>&nbsp;&nbsp;&nbsp;Device-dependent font.<br><br></li>
<li>
<b>OEM_FIXED_FONT</b>&nbsp;&nbsp;&nbsp;OEM-dependent fixed font.<br><br></li>
<li>
<b>SYSTEM_FONT</b>&nbsp;&nbsp;&nbsp;The system font. By default, Windows uses the system font to draw menus, dialog-box controls, and other text. In Windows versions 3.0 and later, the system font is proportional width; earlier versions of Windows use a fixed-width system font.<br><br></li>
<li>
<b>SYSTEM_FIXED_FONT</b>&nbsp;&nbsp;&nbsp;The fixed-width system font used in Windows prior to version 3.0. This object is available for compatibility with earlier versions of Windows.<br><br></li>
<li>
<b>DEFAULT_PALETTE</b>&nbsp;&nbsp;&nbsp;Default color palette. This palette consists of the 20 static colors in the system palette.</li>
</ul>这些对象留在DC中是安全的,所以你可以利用选入库存对象来作为恢复DC中GUI对象。</p>
<p>大家可能都注意到了绘图时都需要一个DC对象,DC(Device Context设备环境)对象是一个抽象的作图环境,可能是对应屏幕,也可能是对应打印机或其它。这个环境是设备无关的,所以你在对不同的设备输出时只需要使用不同的设备环境就行了,而作图方式可以完全不变。这也就是Windows耀眼的一点设备无关性。如同你将对一幅画使用照相机或复印机将会产生不同的输出,而不需要对画进行任何调整。DC的使用会穿插在本章中进行介绍。</p>
<p align=center><a href=teach_first.htm#charpter2>返回</a></p>
</td></tr>
</table>
<p align=center><small>版权所有 闻怡洋 <a href=http://vchelp.163.net/>http://vchelp.163.net/</a></small></p>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -