环境:win10+vs2017+mfc
1,位图的加载
CBitmap * m_Bitmap=new CBitmap;
m_BitMAP->m_hObject=LoadImage(NULL,"**.BMP",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
2,位图的运算
CClientDc dc(this);
dc.BitBlt(dc.BitBlt(x, 300, 63,154, m_Dc,63,0, SRCAND);//颜色按位与
***SRCPAINT;//颜色按位或
***SRCCOPY;//颜色覆盖
最基本肯定是少不了CMyApp和CMyWnd(上一篇博客已经说到)
其余的则是与位图操作相关,绘图相关的
主要是CDC和CBitmap
CDC:
MSDN说明:
The CDC object provides member functions for working with a device context, such as a display or printer, as well as members for working with a display context associated with the client area of a window.
大意是说CDC提供成员函数处理设备环境,比如显示和打印,也是处理窗口的客户区域显示,这里贴图主要是使用到后者,对客户区域的显示
CBitmap:大意是说,构建与位图相关联的对象,相当于java中的File对象,构建操作文件的对象
To use a CBitmap object, construct the object, attach a bitmap handle to it with one of the initialization member functions, and then call the object's member functions.
#include
class CMyWnd :public CFrameWnd {
private:
CDC * mdc;
CBitmap *mbmp,*mbmpBack;
public:
CMyWnd() {
Create(0, TEXT("FIRST"));
//在窗口加载之后加载图片
CClientDC dc(this);
mdc = new CDC;
//创建适配dc
mdc->CreateCompatibleDC(&dc);
mbmp = new CBitmap;
mbmp->m_hObject = LoadImage(0, TEXT("crimer2.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
//加载背景位图
mbmpBack = new CBitmap;
mbmpBack->m_hObject = LoadImage(0, TEXT("bground.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
}
DECLARE_MESSAGE_MAP()
afx_msg void OnPaint();
};
class CMyApp :public CWinApp
{
BOOL InitInstance();
};
BOOL CMyApp::InitInstance() {
CMyWnd *pf = new CMyWnd;
//pf->Create(0, "SENCOND");
pf->ShowWindow(m_nCmdShow);
pf->UpdateWindow();
m_pMainWnd = pf;
return TRUE;
}
BEGIN_MESSAGE_MAP(CMyWnd, CFrameWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()
void CMyWnd::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用 CFrameWnd::OnPaint()
//选取位图(选取背景位图)
mdc->SelectObject(mbmpBack);
//绘制位图 参数说明: 依次为绘制的位图左上角点 在窗口的位置、位图大小、mdc、图片截取的左上角 点坐标,0,0表示全部截取
//最后一个参数是位图与窗口背景的运算规则,SRCOPY就是覆盖,使用图片完全覆盖背景窗口
//SRCAND 是与运算,位与,如果窗口背景是白色(全1),那么位图除了白色部分,其余皆显示为黑色
//SRCPAINT是或运算,按位或,如果窗口背景是黑色(全0),那么位图是什么颜色,就会显示什么颜色
dc.BitBlt(0,0, 786, 481, mdc, 0, 0, SRCCOPY);//保留此行,注释掉一下三行,修改最后一个参数可以看到效果
//选取另一张位图
mdc->SelectObject(mbmp);
dc.BitBlt(0, 0, 63,154, mdc,63,0, SRCAND);
dc.BitBlt(0, 0, 63, 154, mdc,0, 0,SRCPAINT);
}
CMyApp TheApp;
mdc->SelectObject(mbmp);//从内存中选取位图
dc.BitBlt(0, 0, 63,154, mdc,63,0, SRCAND);//开始贴图
BOOL BitBlt( int x, int y, int nWidth, int nHeight, CDC* pSrcDC, int xSrc, int ySrc, DWORD dwRop );
x
Specifies the logical x-coordinate of the upper-left corner of the destination rectangle.
y
Specifies the logical y-coordinate of the upper-left corner of the destination rectangle.
nWidth//从src上面截取下来的图的大小
Specifies the width (in logical units) of the destination rectangle and source bitmap.
nHeight
Specifies the height (in logical units) of the destination rectangle and source bitmap.
pSrcDC//被粘贴的图片src
Pointer to a CDC object that identifies the device context from which the bitmap will be copied. It must be NULL if dwRop specifies a raster operation that does not include a source.
xSrc//在原图片上开始截取的起点,左上角那个点的坐标
Specifies the logical x-coordinate of the upper-left corner of the source bitmap.
ySrc
Specifies the logical y-coordinate of the upper-left corner of the source bitmap.
dwRop//被粘贴的图与背景之间的运算,与,或,覆盖
Specifies the raster operation to be performed. Raster-operation codes define how the GDI combines colors in output operations that involve a current brush, a possible source bitmap, and a destination bitmap. See BitBlt in the Windows SDK for a list of the raster-operation codes for dwRop and their descriptions
图片必须是bmp格式,如果是png等其他格式,不要重命名,这样是不行的,应该使用windws的画图工具另存为 bmp文件
放在工程目录下的 与工程目录同名的文件下,也就是存放cpp文件的那个目录
比如我的是:
如果是在cpp文件的上一级目录,那么加载位图时,位图路径是这样的 "../bground.bmp"
测试将位图移动到上一级目录:
测试效果:(未改代码)
加上相对路径:../
mdc = new CDC;
//创建适配dc
mdc->CreateCompatibleDC(&dc);
mbmp = new CBitmap;
mbmp->m_hObject = LoadImage(0, TEXT("../crimer2.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
//加载背景位图
mbmpBack = new CBitmap;
mbmpBack->m_hObject = LoadImage(0, TEXT("../bground.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
否则只是不能显示图片,但是不会报错,c++这点有点坑,和java有点不一样
用到的素材截图:
背景:746*481
小偷:126x368