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

📄 teach_road_24.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.4 在窗口中绘制设备相关位图,图标,设备无关位图</big></p>

<table border=0 align=center width=80%>
<tr><td>
<p>在Windows中可以将预先准备好的图像复制到显示区域中,这种内存拷贝执行起来是非常快的。在Windows中提供了两种使用图形拷贝的方法:通过设备相关位图(DDB)和设备无关位图(DIB)。</p>
<p>DDB可以用MFC中的CBitmap来表示,而DDB一般是存储在资源文件中,在加载时只需要通过资源ID号就可以将图形装入。BOOL CBitmap::LoadBitmap( UINT nIDResource )可以装入指定DDB,但是在绘制时必须借助另一个和当前绘图DC兼容的内存DC来进行。通过CDC::BitBlt( int x, int y, int nWidth, int nHeight, CDC* pSrcDC, int xSrc, int ySrc, DWORD dwRop )绘制图形,同时指定光栅操作的类型。BitBlt可以将源DC中位图复制到目的DC中,其中前四个参数为目的区域的坐标,接下来是源DC指针,然后是源DC中的起始坐标,由于BitBlt为等比例复制,所以不需要再次指定长宽,(StretchBlt可以进行缩放)最后一个参数为光栅操作的类型,可取以下值:
<ul type=disc>
<li>
<b>BLACKNESS</b> 输出区域为黑色&nbsp;&nbsp;&nbsp;Turns all output black.<br><br></li>
<li>
<b>DSTINVERT</b> 反色输出区域&nbsp;&nbsp;&nbsp;Inverts the destination bitmap.<br><br></li>
<li>
<b>MERGECOPY</b> 在源和目的间使用AND操作&nbsp;&nbsp;&nbsp;Combines the pattern and the source bitmap using the Boolean AND operator.<br><br></li>
<li>
<b>MERGEPAINT</b> 在反色后的目的和源间使用OR操作&nbsp;&nbsp;&nbsp;Combines the inverted source bitmap with the destination bitmap using the Boolean OR operator.<br><br></li>
<li>
<b>NOTSRCCOPY</b> 将反色后的源拷贝到目的区&nbsp;&nbsp;&nbsp;Copies the inverted source bitmap to the destination.<br><br></li>
<li>
<b>PATINVERT</b> 源和目的间进行XOR操作&nbsp;&nbsp;&nbsp;Combines the destination bitmap with the pattern using the Boolean XOR operator.<br><br></li>
<li>
<b>SRCAND</b> 源和目的间进行AND操作&nbsp;&nbsp;&nbsp;Combines pixels of the destination and source bitmaps using the Boolean AND operator.<br><br></li>
<li>
<b>SRCCOPY</b> 复制源到目的区&nbsp;&nbsp;&nbsp;Copies the source bitmap to the destination bitmap.<br><br></li>
<li>
<b>SRCINVERT</b> 源和目的间进行XOR操作&nbsp;&nbsp;&nbsp;Combines pixels of the destination and source bitmaps using the Boolean XOR operator.<br><br></li>
<li>
<b>SRCPAINT</b> 源和目的间进行OR操作&nbsp;&nbsp;&nbsp;Combines pixels of the destination and source bitmaps using the Boolean OR operator.<br><br></li>
<li>
<b>WHITENESS</b> 输出区域为白色&nbsp;&nbsp;&nbsp;Turns all output white.</li>
</ul>
下面用代码演示这种方法:<pre>
CYourView::OnDraw(CDC* pDC)
{
	CDC memDC;//定义一个兼容DC
	memDC.CreateCompatibleDC(pDC);//创建DC
	CBitmap bmpDraw;
	bmpDraw.LoadBitmap(ID_BMP);//装入DDB
	CBitmap* pbmpOld=memDC.SelectObject(&bmpDraw);//保存原有DDB,并选入新DDB入DC
	pDC->BitBlt(0,0,20,20,&memDC,0,0,SRCCOPY);//将源DC中(0,0,20,20)复制到目的DC(0,0,20,20)
	pDC->BitBlt(20,20,40,40,&memDC,0,0,SRCAND);//将源DC中(0,0,20,20)和目的DC(20,20,40,40)中区域进行AND操作
	memDC.SelectObject(pbmpOld);//选入原DDB
}
</pre>
</p>
<p>(图标并不是一个GDI对象,所以不需要选入DC)在MFC中没有一个专门的图标类,因为图标的操作比较简单,使用HICON CWinApp::LoadIcon( UINT nIDResource )或是HICON CWinApp::LoadStandardIcon( LPCTSTR lpszIconName )  装入后就可以利用BOOL CDC::DrawIcon( int x, int y, HICON hIcon )绘制。由于在图标中可以指定透明区域,所以在某些需要使用非规则图形而且面积不大的时候使用图标会比较简单。下面给出简单的代码:
<pre>
OnDraw(CDC* pDC)
{
	HICON hIcon1=AfxGetApp()->LoadIcon(IDI_I1);
	HICON hIcon2=AfxGetApp()->LoadIcon(IDI_I2);
	pDC->DrawIcon(0,0,hIcon1);
	pDC->DrawIcon(0,40,hIcon2);
	DestroyIcon(hIcon1);
	DestroyIcon(hIcon2);
}
</pre></p>
<p>同样在MFC也没有提供一个DIB的类,所以在使用DIB位图时我们需要自己读取位图文件中的头信息,并读入数据,并利用API函数StretchDIBits绘制。位图文件以BITMAPFILEHEADER结构开始,然后是BITMAPINFOHEADER结构和调色版信息和数据,其实位图格式是图形格式中最简单的一种,而且也是Windows可以理解的一种。我不详细讲解DIB位图的结构,提供一个CDib类供大家使用,这个类包含了基本的功能如:Load,Save,Draw。<a href=24_dib.zip>DownLoad CDib 4K</a></p>
<p></p>
<p></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 + -