Visual C ++ Game Programming transparent basis of picture processing

A transparent processing step

1. FIG mask pattern and background and do arithmetic, black is 0, 1 is white;

2. FIG foreground and background operation, or for, the processed bitmap affixed to the DC

Second, the implementation code


#include "stdafx.h"

HINSTANCE hInst;
HBITMAP bg,dra;//定义背景图句柄bg,前景图句柄dra
HDC     mdc;//声明内存DC--mdc来暂存位图

ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
void				MyPaint(HDC hdc);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
	MSG msg;//用来表示消息的结构体变量

	MyRegisterClass(hInstance);

	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	
	return msg.wParam;
}

ATOM MyRegisterClass(HINSTANCE hInstance)//向系统注册窗口类型,要产生一个什么样的窗口
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX); 
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= NULL;
	wcex.hCursor		= NULL;
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= "canvas";
	wcex.hIconSm		= NULL;

	return RegisterClassEx(&wcex);
}


BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HWND hWnd;
	HDC hdc;

	hInst = hInstance;

	hWnd = CreateWindow("canvas", "绘图窗口" , WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

	if (!hWnd)
	{
		return FALSE;
	}

	MoveWindow(hWnd,10,10,600,450,true);
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	hdc = GetDC(hWnd);//获取窗口DC
	mdc = CreateCompatibleDC(hdc);//建立与窗口DC兼容的内存DC--mdc

	bg = (HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,600,400,LR_LOADFROMFILE); 
	dra = (HBITMAP)LoadImage(NULL,"dra.bmp",IMAGE_BITMAP,600,568,LR_LOADFROMFILE); 

	MyPaint(hdc);
	ReleaseDC(hWnd,hdc);

	return TRUE;
}


void MyPaint(HDC hdc)
{
	SelectObject(mdc,bg);//将bg存到mdc中
	BitBlt(hdc,0,0,600,400,mdc,0,0,SRCCOPY);//这里指整个窗口,外部窗口;如果底下有白边说明创建窗口的高度太大或者bg的高度太小

	SelectObject(mdc,dra);//把dra存到mdc中
	BitBlt(hdc,0,116,600,284,mdc,0,284,SRCAND);//屏蔽图与背景图作and运算,黑0白1
	BitBlt(hdc,0,116,600,284,mdc,0,0,SRCPAINT);//前景图与背景图作OR运算
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message)
	{
		case WM_PAINT:					
			hdc = BeginPaint(hWnd, &ps);
			MyPaint(hdc);
			EndPaint(hWnd, &ps);
			break;
		case WM_DESTROY:				
			DeleteDC(mdc);
			DeleteObject(bg);
			DeleteObject(dra);
			PostQuitMessage(0);
			break;
		default:						
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

Third, the effect

Guess you like

Origin blog.csdn.net/Sruggle/article/details/90611994