GDI透明动画(多背景移动

/*程序功能:显示连续动态前景图案,
透明处理,
从而产生透明动画效果
  */
#include "stdafx.h"

//声明全局变量
//bg为背景图,dra为恐龙连续跑动对应的屏蔽图
HINSTANCE hInst;
HBITMAP dra,bg;
HDC		hdc,mdc,bufdc;//窗口hdc,内存mdc, 选区位图bufdc
HWND	hWnd;
DWORD	tPre,tNow;
int		num,x,y;

//ㄧΑ
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( msg.message!=WM_QUIT )
    {
        if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) )
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
		else
		{
			tNow = GetTickCount();
			if(tNow-tPre >= 100)
				MyPaint(hdc);
		}
    }

	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)
{
	char filename[20] = "";
	HBITMAP bmp;
	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,640,480,true);
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	hdc = GetDC(hWnd);
	mdc = CreateCompatibleDC(hdc);
	bufdc = CreateCompatibleDC(hdc);
	bmp = CreateCompatibleBitmap(hdc,640,480);

	SelectObject(mdc,bmp);
	//加载前景图和背景图
	dra = (HBITMAP)LoadImage(NULL,"dra.bmp",IMAGE_BITMAP,760,198,LR_LOADFROMFILE);
	bg = (HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,640,480,LR_LOADFROMFILE);

	num = 0;    //显示图号
	x = 640;	//贴图起始坐标x
	y = 360;    //贴图起始坐标y

	MyPaint(hdc);

	return TRUE;
}

//****自定义绘图函数*********************************
// 1.恐龙图透明处理
// 2.更新贴图坐标
void MyPaint(HDC hdc)
{
	if(num == 8)
		num = 0;

	//mdc贴上背景图
	SelectObject(bufdc,bg);
	BitBlt(mdc,0,0,640,480,bufdc,0,0,SRCCOPY);

	//mdc上透明处理
	SelectObject(bufdc,dra);
	BitBlt(mdc,x,y,95,99,bufdc,num*95,99,SRCAND);//与运算
	BitBlt(mdc,x,y,95,99,bufdc,num*95,0,SRCPAINT);//或运算

	//将最后画面显示在窗口上
	BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY);

	tPre = GetTickCount();     //记录此次绘图时间
	num++;

	x-=20;					   //计算下次贴图坐标
	if(x<=-95)
		x = 640;
}

//****癟矪瞶ㄧΑ***********************************
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case WM_DESTROY:					//
			DeleteDC(mdc);
			DeleteDC(bufdc);
			DeleteObject(dra);
			DeleteObject(bg);
			ReleaseDC(hWnd,hdc);
			PostQuitMessage(0);
			break;
		default:							//
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}


相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页