1. 程式人生 > >(二)MFC學習之貼圖

(二)MFC學習之貼圖

環境: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 <afxwin.h>

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

 

效果:

注意事項:

1,關於格式

圖片必須是bmp格式,如果是png等其他格式,不要重新命名,這樣是不行的,應該使用windws的畫圖工具另存為 bmp檔案

2,關於圖片路徑

放在工程目錄下的 與工程目錄同名的檔案下,也就是存放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