《windows程序设计》学习_3.1:画出雷区,左键的使用

时间:2024-05-28 10:37:08
 #include<windows.h>
#include "resource.h" LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstace, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("Stick1");
HWND hwnd;
MSG msg;
WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = ;
wndclass.cbWndExtra = ;
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 ("The Program requires Windows NT!"), szAppName, MB_ICONERROR);
return ;
} hwnd = CreateWindow (szAppName,
TEXT ("Stick 1"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL); ShowWindow(hwnd , iCmdShow);
UpdateWindow(hwnd); while(GetMessage(&msg, NULL, , ))
{
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;
PAINTSTRUCT ps; switch(message)
{
case WM_CREATE:
hInstance = ((LPCREATESTRUCT) lParam) ->hInstance; hBitmap = LoadBitmap(hInstance,MAKEINTRESOURCE(IDM_BITMAP)/*TEXT("bitmap1")*/);
GetObject(hBitmap,sizeof(BITMAP),&bitmap); cxSource = bitmap.bmWidth;
cySource = bitmap.bmHeight/; return ;
case WM_SIZE:
cxClient = LOWORD (lParam);
cyClient = HIWORD (lParam);
return ;
case WM_PAINT:
hdc = BeginPaint(hwnd , &ps); hdcMem = CreateCompatibleDC(hdc);
SelectObject(hdcMem,hBitmap); for(y = ; y < cyClient - ; y += cySource)
{
for(x = ; x < cxClient - ; x += cxSource)
{
BitBlt(hdc,x,y,cxSource,cySource,hdcMem,,,SRCCOPY);
}
}
DeleteDC(hdcMem);
EndPaint(hwnd, &ps);
return ;
case WM_LBUTTONDOWN:
hdc = GetDC(hwnd); mx = LOWORD(lParam);
my = HIWORD(lParam); hdcMem = CreateCompatibleDC(hdc);
SelectObject(hdcMem, hBitmap); BitBlt(hdc, mx, my, cxSource, cySource, hdcMem, , cySource, SRCCOPY); DeleteDC(hdcMem);
ReleaseDC(hwnd, hdc);
return ;
case WM_DESTROY:
DeleteObject(hBitmap);
PostQuitMessage();
return ;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}

《windows程序设计》学习_3.1:画出雷区,左键的使用