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

📄 teach_road_24.htm

📁 最佳VC++入门电子书
💻 HTM
字号:

<!-- LANGUAGE='JavaScript'>write_body();<-->
<!-- LANGUAGE='JavaScript'>write_bar();<-->


<table width=98% cellspacing="0" cellpadding="0" align=center><!--整体框架-->
<tr><td>

<table border=0 width="100%" cellspacing="0" cellpadding="2"><!--标记放置区域-->
<tr>
	<td width="30%" align="center" bgcolor="#8E8E8E" valign=middle><img src=../../img/brand_200_60.gif width=200 height=60 alt="LOGO1"></td>
	<td width="70%" align="center" bgcolor="#8E8E8E" valign=middle><!-- LANGUAGE='JavaScript'>write_ban();<--></td>
</tr>
<tr>
	<td colspan="2" bgcolor="#939393" align=center><font color=white>您当前位置</font> <a href=../../index.htm><font color=white>首页</font></a> <a href=../index.htm><font color=white>开发教程</font></a> <a href=index.htm><font color=white><font class=engul>Visual C++/MFC</font>入门教程</font></a> <font color=white>2.4 在窗口中绘制设备相关位图,图标,设备无关位图<!-- LANGUAGE='JavaScript'>write_command();<--></font></td>
</tr>
</table><!--标记放置区域 END-->

<table border=0 width=100% cellspacing="0" cellpadding="0">
<tr>
<td><!--begin-->

<br>
<p align=center><big>2.4 在窗口中绘制设备相关位图,图标,设备无关位图</big></p>

<table border=0 align=center width=100%>
<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=index.htm#charpter2>返回</a></p>
</td></tr>
</table>
<p align=center><small>版权所有 闻怡洋 <a href=http://www.vchelp.net/>http://www.vchelp.net/</a></small></p>


</td><!--end-->
</tr>
</table>

</td></tr></table><!--整体框架 END-->




<!-- LANGUAGE='JavaScript'>write_tail();<-->


⌨️ 快捷键说明

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