/*----------------------------------------
BRICKS1.C -- LoadBitmap Demonstration
(c) Charles Petzold, 1998
----------------------------------------*/
#include <windows.h>
#include "resource.h"
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName [] = TEXT ("Bricks1") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, TEXT ("LoadBitmap Demo"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HBITMAP hBitmap ;
static int cxClient, cyClient, cxSource, cySource ;
BITMAP bitmap ;
HDC hdc, hdcMem ;
HINSTANCE hInstance ;
int x, y , mx, my, nx, ny;
PAINTSTRUCT ps ;
const int nStartX = 100; // 背景图坐标启始点
const int nStartY = 100;
switch (message)
{
case WM_CREATE:
hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
hBitmap = LoadBitmap (hInstance, MAKEINTRESOURCE (IDM_BITMAP)) ;
GetObject (hBitmap, sizeof (BITMAP), &bitmap) ;
cxSource = bitmap.bmWidth ;
cySource = (bitmap.bmHeight)/2 ;
break;
case WM_SIZE:
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
break;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
hdcMem = CreateCompatibleDC (hdc) ;
SelectObject (hdcMem, hBitmap) ;
for (y = nStartY ; y < cyClient - nStartY ; y += cySource)
{
for (x = nStartX ; x < cxClient - nStartX ; x += cxSource)
{
BitBlt (hdc, x, y, cxSource, cySource, hdcMem, 0, 0, SRCCOPY) ;
}
}
DeleteDC (hdcMem) ;
EndPaint (hwnd, &ps) ;
break;
case WM_LBUTTONDOWN:
hdc = GetDC(hwnd);
mx = LOWORD(lParam);
my = HIWORD(lParam);
// 判断鼠标坐标是否在怎个图块内,不在跳出
if(mx <nStartX || my < nStartY)
break;
// 左边和下边效准
x = cxClient - nStartX +cxSource - (cxClient - 2 * nStartX) % cxSource -1;
y = cyClient - nStartY +cySource - (cyClient - 2 * nStartY) % cySource -1;
if(mx >= x || my >= y)
break;
hdcMem = CreateCompatibleDC (hdc) ;
SelectObject (hdcMem, hBitmap) ;
// 找到单个图块的左上角坐标
x = nStartX + (mx - nStartX) / cxSource * cxSource;
y = nStartY + (my - nStartY) / cySource * cySource;
BitBlt (hdc, x, y, cxSource, cySource, hdcMem, 0, cySource, SRCCOPY) ;
DeleteDC (hdcMem) ;
ReleaseDC(hwnd, hdc);
break;
case WM_DESTROY:
DeleteObject (hBitmap) ;
PostQuitMessage (0) ;
break;
}
return DefWindowProc (hwnd, message, wParam, lParam);
}