Code:
#include <windows.h>
#include "resource.h"
const char g_szClassName[] = "myWindowClass";
//Dollguy info
typedef struct _DOLLGUY
{
int width;
int height;
int x;
int y;
}DOLLGUY;
//background
HBITMAP g_bgplain = NULL;
//Doll guy
HBITMAP g_chdoll = NULL;
//Doll guy Mask
HBITMAP g_mskdoll = NULL;
//somethign about the struct
DOLLGUY g_dollinfo;
//Create Mask for Doll
HBITMAP CreateBitmapMask(HBITMAP hbmColour, COLORREF crTransparent)
{
HDC hdcMem, hdcMem2;
HBITMAP mskdoll;
BITMAP bm;
GetObject(hbmColour, sizeof(BITMAP), &bm);
mskdoll = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL);
hdcMem = CreateCompatibleDC(0);
hdcMem2 = CreateCompatibleDC(0);
SelectObject(hdcMem, hbmColour);
SelectObject(hdcMem2, mskdoll);
SetBkColor(hdcMem, crTransparent);
BitBlt(hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem2, 0, 0, SRCINVERT);
DeleteDC(hdcMem);
DeleteDC(hdcMem2);
return mskdoll;
}
//Draw the doll on update
void DrawDoll(HDC hdc, RECT* prc)
{
HDC hdcBuffer = CreateCompatibleDC(hdc);
HBITMAP hbmBuffer = CreateCompatibleBitmap(hdc, prc->right, prc->bottom);
HBITMAP hbmOldBuffer = SelectObject(hdcBuffer, hbmBuffer);
HDC hdcMem = CreateCompatibleDC(hdc);
HBITMAP hbmOld = SelectObject(hdcMem, g_mskdoll);
FillRect(hdcBuffer, prc, GetStockObject(WHITE_BRUSH));
BitBlt(hdcBuffer, g_dollinfo.x, g_dollinfo.y, g_dollinfo.width, g_dollinfo.height, hdcMem, 0, 0, SRCAND);
SelectObject(hdcMem, g_chdoll);
BitBlt(hdcBuffer, g_dollinfo.x, g_dollinfo.y, g_dollinfo.width, g_dollinfo.height, hdcMem, 0, 0, SRCPAINT);
BitBlt(hdc, 0, 0, prc->right, prc->bottom, hdcBuffer, 0, 0, SRCCOPY);
SelectObject(hdcMem, hbmOld);
DeleteDC(hdcMem);
SelectObject(hdcBuffer, hbmOldBuffer);
DeleteDC(hdcBuffer);
DeleteObject(hbmBuffer);
}
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
//set background
g_bgplain = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(101));
if(g_bgplain == NULL)
MessageBox(hwnd, "Could not load Background!", "Error", MB_OK | MB_ICONEXCLAMATION);
//set doll char.
g_chdoll = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(102));
if(g_chdoll == NULL)
MessageBox(hwnd, "Could not load Item: doll!", "Error", MB_OK | MB_ICONEXCLAMATION);
//Create Mask(Doll)
g_mskdoll = CreateBitmapMask(g_chdoll, RGB(0, 0, 0));
if(g_mskdoll == NULL)
MessageBox(hwnd, "Could not create mask!", "Error", MB_OK | MB_ICONEXCLAMATION);
break;
case WM_PAINT:
{
//<start drawing background>
BITMAP bm;
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
HDC hdcMem = CreateCompatibleDC(hdc);
HBITMAP hbmOld = SelectObject(hdcMem, g_bgplain);
GetObject(g_bgplain, sizeof(bm), &bm);
BitBlt(hdc, 0, 0, 330, 191, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, hbmOld);
DeleteDC(hdcMem);
EndPaint(hwnd, &ps);
//<end drawing background>
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
DeleteObject(g_bgplain);
DeleteObject(g_chdoll);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"Move left + right",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 330, 191,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}