鼠标绘图
程序代码:
#include <windows.h> #define ID_TIME 1 LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow) { TCHAR Title[] = "按下鼠标左键进行绘图,按下右键清屏"; TCHAR ClsName[] = "szCls"; HWND hwnd; WNDCLASS wc; MSG msg; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH); wc.hCursor = LoadCursor (hInstance, IDC_ARROW); wc.hIcon = LoadIcon (hInstance, IDI_APPLICATION); wc.hInstance = hInstance; wc.lpfnWndProc = WndProc; wc.lpszClassName = ClsName; wc.lpszMenuName = NULL; wc.style = CS_HREDRAW | CS_VREDRAW; if ( !RegisterClass (&wc) ) { MessageBox (NULL, TEXT ("RegisterError"), TEXT ("ERROR"), MB_OK | MB_ICONINFORMATION); return -1; } hwnd = CreateWindow ( ClsName, Title, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); ShowWindow (hwnd, SW_SHOW); while ( GetMessage (&msg, NULL, 0, 0) > 0 ) { TranslateMessage (&msg); DispatchMessage (&msg); } return msg.wParam; } LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; static int cur_x, cur_y; switch (message) { case WM_CREATE: { } break; case WM_MOUSEMOVE: { cur_x = (int) LOWORD(lParam); cur_y = (int) HIWORD(lParam); } break; case WM_LBUTTONDOWN: { SetTimer (hwnd, ID_TIME, 1, NULL); } break; case WM_LBUTTONUP: { KillTimer (hwnd, ID_TIME); } break; case WM_TIMER: { hdc = GetDC (hwnd); SetPixel (hdc, cur_x, cur_y, RGB (255, 0, 0)); ReleaseDC (hwnd, hdc); } break; case WM_RBUTTONDOWN: { InvalidateRect (hwnd, NULL, TRUE); } break; case WM_CLOSE: { PostQuitMessage (0); } break; default: return DefWindowProc (hwnd, message, wParam, lParam); } return 0; }
鼠绘.rar
(12.15 KB)