📄 vcc61.htm
字号:
<html>
<head>
<title>c++系列</title>
<meta content="text/html; charset=gb2312" http-equiv=Content-Type>
</head>
<p align="center"><script src="../../1.js"></script></a>
<body bgcolor="#ffffff" leftmargin="5" topmargin="1" marginheight="5" marginwidth="5">
<div align=center>
<table border=0 cellpadding=0 cellspacing=0 width=680 align="center">
<tbody>
<tr>
<td width=200 height="59">
</tr>
</tbody>
</table>
<table border=1 bordercolordark=#ffffff bordercolorlight=#ffffff cellpadding=0
cellspacing=0 width=685 align="center" height="70">
<tbody>
<tr>
<td bgcolor=#F9D23C height=14>
<div align=center class=H1><font color="#FFFFFF">在窗口中绘制设备相关位图,图标,设备无关位图</font></div>
</td>
</tr>
<tr valign=top>
<td class=H1 height=51>
<p align="left"><span class="unnamed1">在Windows中可以将预先准备好的图像复制到显示区域中,这种内存拷贝执行起来是非常快的。在Windows中提供了两种使用图形拷贝的方法:通过设备相关位图(DDB)和设备无关位图(DIB)。</span></p>
<p class="ourfont1"><span class="unnamed1">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可以进行缩放)最后一个参数为光栅操作的类型,可取以下值:
</span>
<ul type=disc>
<li class="ourfont1"> <b>BLACKNESS</b> 输出区域为黑色 Turns
all output black.<br>
<br>
</span>
<li class="ourfont1"> <b>DSTINVERT</b> 反色输出区域 Inverts
the destination bitmap.<br>
<br>
</span>
<li class="ourfont1"> <b>MERGECOPY</b> 在源和目的间使用AND操作 Combines
the pattern and the source bitmap using the Boolean AND operator.<br>
<br>
</span>
<li class="ourfont1"> <b>MERGEPAINT</b> 在反色后的目的和源间使用OR操作 Combines
the inverted source bitmap with the destination bitmap using the Boolean
OR operator.<br>
<br>
</span>
<li class="ourfont1"> <b>NOTSRCCOPY</b> 将反色后的源拷贝到目的区 Copies
the inverted source bitmap to the destination.<br>
<br>
</span>
<li class="ourfont1"> <b>PATINVERT</b> 源和目的间进行XOR操作 Combines
the destination bitmap with the pattern using the Boolean XOR operator.<br>
<br>
</span>
<li class="ourfont1"> <b>SRCAND</b> 源和目的间进行AND操作 Combines
pixels of the destination and source bitmaps using the Boolean AND
operator.<br>
<br>
</span>
<li class="ourfont1"> <b>SRCCOPY</b> 复制源到目的区 Copies
the source bitmap to the destination bitmap.<br>
<br>
</span>
<li class="ourfont1"> <b>SRCINVERT</b> 源和目的间进行XOR操作 Combines
pixels of the destination and source bitmaps using the Boolean XOR
operator.<br>
<br>
</span>
<li class="ourfont1"> <b>SRCPAINT</b> 源和目的间进行OR操作 Combines
pixels of the destination and source bitmaps using the Boolean OR
operator.<br>
<br>
</span>
<li class="ourfont1"> <b>WHITENESS</b> 输出区域为白色 Turns
all output white. </span></li>
</ul>
<span class="unnamed1">下面用代码演示这种方法: </span>
<pre class="ourfont1"><span class="unnamed1">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
}
</span></pre>
<p></p>
<p class="ourfont1"> (图标并不是一个GDI对象,所以不需要选入DC)在MFC中没有一个专门的图标类,因为图标的操作比较简单,使用HICON
CWinApp::LoadIcon( UINT nIDResource )或是HICON CWinApp::LoadStandardIcon(
LPCTSTR lpszIconName ) 装入后就可以利用BOOL CDC::DrawIcon( int x, int y, HICON
hIcon )绘制。由于在图标中可以指定透明区域,所以在某些需要使用非规则图形而且面积不大的时候使用图标会比较简单。下面给出简单的代码:
</span>
<pre class="ourfont1"><span class="unnamed1">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);
}
</span></pre>
<p></p>
<p class="ourfont1"><span class="unnamed1">同样在MFC也没有提供一个DIB的类,所以在使用DIB位图时我们需要自己读取位图文件中的头信息,
并读入数据,并利用API函数StretchDIBits绘制。位图文件以BITMAPFILEHEADER结构开始,然后是BITMAPINFOHEADER
结构和调色版信息和数据,其实位图格式是图形格式中最简单的一种,而且也是Windows可以理解的一种。我不详细 讲解DIB位图的结构,提供一个CDib类供大家使用,这个类包含了基本的功能如:Load,Save,Draw。</span></p>
<div align="left"> </div>
</td>
</tr>
</tbody>
</table>
</div>
<p align="center"><script src="../../2.js"></script></a>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -