#include <windows.h> #include "resource.h"
#define XMOV 8 #define YMOV 8 #define XTILE 48 #define YTILE 48
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); HINSTANCE g_hInst; LPSTR lpszClass = L"Win32 api";
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance , LPSTR lpszCmdParam, int nCmdShow) { HWND hWnd; MSG Message; WNDCLASS WndClass; g_hInst = hInstance;
WndClass.cbClsExtra = 0; WndClass.cbWndExtra = 0; WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); WndClass.hCursor = LoadCursor(NULL, IDC_ARROW); WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); WndClass.hInstance = hInstance; WndClass.lpfnWndProc = (WNDPROC)WndProc; WndClass.lpszClassName = lpszClass; WndClass.lpszMenuName = NULL; WndClass.style = CS_HREDRAW | CS_VREDRAW; RegisterClass(&WndClass);
hWnd = CreateWindow(lpszClass, lpszClass, WS_CAPTION|WS_MINIMIZEBOX|WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, (700+36), (480+36), NULL, (HMENU)NULL, hInstance, NULL); ShowWindow(hWnd, nCmdShow);
while (GetMessage(&Message, 0, 0, 0)) { TranslateMessage(&Message); DispatchMessage(&Message); } return Message.wParam; }
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam) { HDC hdc; static HDC MemDC;
PAINTSTRUCT ps; static int ixPos = 0; static int iyPos = 0;
static int ixPos_Max; static int iyPos_Max; static RECT rt; RECT RtArea;
static HBITMAP myBitmap; static HBITMAP oldBitmap; static HBITMAP backBitmap;
static HBITMAP frontBitmap; static HBITMAP leftBitmap; static HBITMAP rightBitmap; static HBITMAP rearBitmap;
int iCntX; int iCntY;
switch (iMessage) { case WM_CREATE: hdc = GetDC(hWnd);
MemDC = CreateCompatibleDC(hdc); frontBitmap = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BITMAP3)); leftBitmap = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BITMAP5)); rightBitmap = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BITMAP6)); rearBitmap = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BITMAP7)); backBitmap = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BITMAP4)); oldBitmap = (HBITMAP)SelectObject(MemDC, frontBitmap); GetClientRect(hWnd, &rt); ixPos_Max = rt.right; iyPos_Max = rt.bottom;
myBitmap = frontBitmap;
ReleaseDC(hWnd, hdc); return 0;
case WM_DESTROY: SelectObject(MemDC, oldBitmap); DeleteObject(frontBitmap); DeleteObject(leftBitmap); DeleteObject(rightBitmap); DeleteObject(rearBitmap); DeleteObject(backBitmap); DeleteDC(MemDC); PostQuitMessage(0); return 0;
case WM_PAINT: hdc = BeginPaint(hWnd, &ps); SelectObject(MemDC, backBitmap); for (iCntY = 0; iCntY < 10; iCntY++) { for (iCntX = 0; iCntX < 15; iCntX++) { BitBlt(hdc, (iCntX*48), (iCntY*48), 48, 48, MemDC, 0, 0, SRCCOPY); } } SelectObject(MemDC, myBitmap); BitBlt(hdc, ixPos, iyPos, 48, 48, MemDC,0, 0, SRCCOPY); EndPaint(hWnd, &ps); return 0; case WM_KEYDOWN: RtArea.top = iyPos; RtArea.bottom = iyPos + YTILE; RtArea.left = ixPos; RtArea.right = ixPos + XTILE;
switch (wParam) { case VK_LEFT: ixPos = ixPos - XMOV; RtArea.left = iyPos - XMOV; if (ixPos < 0) { ixPos = 0; RtArea.left = 0; } myBitmap = leftBitmap; break;
case VK_RIGHT: ixPos = ixPos + XMOV; if (ixPos > ixPos_Max - XTILE) { ixPos = ixPos_Max - XTILE; } myBitmap = rightBitmap;
RtArea.right = +XMOV; break;
case VK_UP: iyPos = iyPos - YMOV; if (iyPos < 0) { iyPos = 0; } myBitmap = rearBitmap; break;
case VK_DOWN: iyPos = iyPos + YMOV; if (iyPos > iyPos_Max - YTILE) { iyPos = iyPos_Max - YTILE; } myBitmap = frontBitmap; break; } InvalidateRect(hWnd, &RtArea, TRUE); return 0; } return(DefWindowProc(hWnd, iMessage, wParam, lParam)); }
|