#include <windows.h>
#include "resource.h"
const char g_szClassName[] = "myWindowClass";
const int ID_TIMER = 2;
const int GIRL_MOVE_DELTA = 15;
const int MAN_MOVE_DELTA = 14;
typedef struct _IMAGEINFO
{
int width;
int height;
int x;
int y;
int dx;
int dy;
}IMAGEINFO;
IMAGEINFO g_girlInfo;
IMAGEINFO g_manInfo;
HBITMAP g_hbmGirl = NULL;
HBITMAP g_hbmMan = NULL;
HBITMAP g_hgmMask = NULL;
HBITMAP g_hmmMask = NULL;
HBITMAP CreateBitmapMask(HBITMAP hbmColour, COLORREF crTransparent)
{
HDC hdcMem, hdcMem2;
HBITMAP hbmMask;
BITMAP bm;
GetObject(hbmColour, sizeof(BITMAP), &bm);
hbmMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL);
hdcMem = CreateCompatibleDC(0);
hdcMem2 = CreateCompatibleDC(0);
SelectObject(hdcMem, hbmColour);
SelectObject(hdcMem2, hbmMask);
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 hbmMask;
}
void DrawMan(HDC hdc, RECT* prc)
{
HDC hdcBuffer = CreateCompatibleDC(hdc);
HBITMAP hbmBuffer = CreateCompatibleBitmap(hdc, prc->right, prc->bottom);
HBITMAP hbmOldBuffer = (HBITMAP)SelectObject(hdcBuffer, hbmBuffer);
HDC hdcMem = CreateCompatibleDC(hdc);
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, g_hmmMask);
FillRect(hdcBuffer, prc, (HBRUSH)GetStockObject(WHITE_BRUSH));
BitBlt(hdcBuffer, g_manInfo.x, g_manInfo.y, g_manInfo.width, g_manInfo.height, hdcMem, 0, 0, SRCAND);
SelectObject(hdcMem, g_hbmMan);
BitBlt(hdcBuffer, g_manInfo.x, g_manInfo.y, g_manInfo.width, g_manInfo.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);
}
void DrawGirl(HDC hdc, RECT* prc)
{
HDC hdcBuffer = CreateCompatibleDC(hdc);
HBITMAP hbmBuffer = CreateCompatibleBitmap(hdc, prc->right, prc->bottom);
HBITMAP hbmOldBuffer = (HBITMAP)SelectObject(hdcBuffer, hbmBuffer);
HDC hdcMem = CreateCompatibleDC(hdc);
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, g_hgmMask);
FillRect(hdcBuffer, prc, (HBRUSH)GetStockObject(WHITE_BRUSH));
BitBlt(hdcBuffer, g_girlInfo.x, g_girlInfo.y, g_girlInfo.width, g_girlInfo.height, hdcMem, 0, 0, SRCAND);
SelectObject(hdcMem, g_hbmGirl);
BitBlt(hdcBuffer, g_girlInfo.x, g_girlInfo.y, g_girlInfo.width, g_girlInfo.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);
}
void UpdateMan(RECT* prc)
{
g_manInfo.x += g_manInfo.dx;
g_manInfo.y += g_manInfo.dy;
if(g_manInfo.x < 0)
{
g_manInfo.x = 0;
g_manInfo.dx = MAN_MOVE_DELTA;
}
else if(g_manInfo.x + g_manInfo.width > prc->right)
{
g_manInfo.x = prc->right - g_manInfo.width;
g_manInfo.dx = -MAN_MOVE_DELTA;
}
if(g_manInfo.y < 0)
{
g_manInfo.y = 0;
g_manInfo.dy = MAN_MOVE_DELTA;
}
else if(g_manInfo.y + g_manInfo.height > prc->bottom)
{
g_manInfo.y = prc->bottom - g_manInfo.height;
g_manInfo.dy = -MAN_MOVE_DELTA;
}
}
void UpdateGirl(RECT* prc)
{
g_girlInfo.x += g_girlInfo.dx;
g_girlInfo.y += g_girlInfo.dy;
if(g_girlInfo.x < 0)
{
g_girlInfo.x = 0;
g_girlInfo.dx = GIRL_MOVE_DELTA;
}
else if(g_girlInfo.x + g_girlInfo.width > prc->right)
{
g_girlInfo.x = prc->right - g_girlInfo.width;
g_girlInfo.dx = -GIRL_MOVE_DELTA;
}
if(g_girlInfo.y < 0)
{
g_girlInfo.y = 0;
g_girlInfo.dy = GIRL_MOVE_DELTA;
}
else if(g_girlInfo.y + g_girlInfo.height > prc->bottom)
{
g_girlInfo.y = prc->bottom - g_girlInfo.height;
g_girlInfo.dy = -GIRL_MOVE_DELTA;
}
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
{
UINT ret;
BITMAP bm, bn;
g_hbmGirl = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_GIRL));
if(g_hbmGirl == NULL)
MessageBox(hwnd, "Could not load IDB_Girl!", "Error", MB_OK | MB_ICONEXCLAMATION);
g_hbmMan = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_MAN));
if(g_hbmMan == NULL)
MessageBox(hwnd, "Could not load IDB_Man!", "Error", MB_OK | MB_ICONEXCLAMATION);
g_hgmMask = CreateBitmapMask(g_hbmGirl, RGB(0, 0, 0));
if(g_hgmMask == NULL)
MessageBox(hwnd, "Could not create mask for Girl!", "Error", MB_OK | MB_ICONEXCLAMATION);
g_hmmMask = CreateBitmapMask(g_hbmMan, RGB(0, 0, 0));
if(g_hmmMask == NULL)
MessageBox(hwnd, "Could not create mask for Man!", "Error", MB_OK | MB_ICONEXCLAMATION);
GetObject(g_hbmGirl, sizeof(bm), &bm);
ZeroMemory(&g_girlInfo, sizeof(g_girlInfo));
g_girlInfo.width = bm.bmWidth;
g_girlInfo.height = bm.bmHeight;
g_girlInfo.dx = GIRL_MOVE_DELTA;
g_girlInfo.dy = GIRL_MOVE_DELTA;
GetObject(g_hbmMan, sizeof(bn), &bn);
ZeroMemory(&g_manInfo, sizeof(g_manInfo));
g_manInfo.width = bn.bmWidth;
g_manInfo.height = bn.bmHeight;
g_manInfo.dx = MAN_MOVE_DELTA;
g_manInfo.dy = MAN_MOVE_DELTA;
ret = SetTimer(hwnd, ID_TIMER, 50, NULL);
if(ret == 0)
MessageBox(hwnd, "Could not SetTimer()!", "Error", MB_OK | MB_ICONEXCLAMATION);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_PAINT:
{
RECT rcClientGirl, rcClientMan;
PAINTSTRUCT psGirl, psMan;
HDC hdc = BeginPaint(hwnd, &psGirl);
GetClientRect(hwnd, &rcClientGirl);
DrawGirl(hdc, &rcClientGirl);
EndPaint(hwnd, &psGirl);
hdc= BeginPaint(hwnd, &psMan);
GetClientRect(hwnd, &rcClientMan);
DrawMan(hdc, &rcClientMan);
EndPaint(hwnd, &psMan);
}
break;
case WM_TIMER:
{
RECT rcClientGirl, rcClientMan;
HDC hdc = GetDC(hwnd);
GetClientRect(hwnd, &rcClientGirl);
UpdateGirl(&rcClientGirl);
DrawGirl(hdc, &rcClientGirl);
ReleaseDC(hwnd, hdc);
hdc = GetDC(hwnd);
GetClientRect(hwnd, &rcClientMan);
UpdateMan(&rcClientMan);
DrawMan(hdc, &rcClientMan);
ReleaseDC(hwnd, hdc);
}
break;
case WM_DESTROY:
KillTimer(hwnd, ID_TIMER);
DeleteObject(g_hbmGirl);
DeleteObject(g_hgmMask);
DeleteObject(g_hbmMan);
DeleteObject(g_hmmMask);
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;
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;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"Girl, where are you?...",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}