按要求用c++写Windows代码:5.做一个简单的画图程序,基本功能: 1)按“L”后,用鼠标拖拽能在屏幕上画直线; 2)按“E”后,用鼠标托拽能在屏幕上画矩形 3)按“C”后,用鼠标拖拽能在屏幕上画圆 4)按“R”“G”“B”能够改变所画图形为红、绿、蓝色
时间: 2023-09-21 19:08:22 浏览: 114
以下是用C++编写的Windows画图程序代码,实现了基本功能:
```
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow) {
static TCHAR szAppName[] = TEXT("SimpleDraw");
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("SimpleDraw"),
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 int cxClient, cyClient;
static BOOL bDrawing;
static POINT ptPrev;
static COLORREF crColor = RGB(0, 0, 0);
switch (message) {
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
return 0;
case WM_KEYDOWN:
switch (wParam) {
case 'R':
crColor = RGB(255, 0, 0);
return 0;
case 'G':
crColor = RGB(0, 255, 0);
return 0;
case 'B':
crColor = RGB(0, 0, 255);
return 0;
case 'L':
case 'E':
case 'C':
SetCapture(hwnd);
bDrawing = TRUE;
return 0;
}
break;
case WM_KEYUP:
if (bDrawing) {
ReleaseCapture();
bDrawing = FALSE;
}
return 0;
case WM_MOUSEMOVE:
if (bDrawing) {
HDC hdc = GetDC(hwnd);
HPEN hPen = CreatePen(PS_SOLID, 1, crColor);
HPEN hOldPen = (HPEN) SelectObject(hdc, hPen);
if (wParam & MK_LBUTTON) {
if (ptPrev.x != -1 && ptPrev.y != -1) {
MoveToEx(hdc, ptPrev.x, ptPrev.y, NULL);
LineTo(hdc, LOWORD(lParam), HIWORD(lParam));
}
ptPrev.x = LOWORD(lParam);
ptPrev.y = HIWORD(lParam);
} else {
if (ptPrev.x != -1 && ptPrev.y != -1) {
Rectangle(hdc, ptPrev.x, ptPrev.y, LOWORD(lParam), HIWORD(lParam));
}
ptPrev.x = LOWORD(lParam);
ptPrev.y = HIWORD(lParam);
}
SelectObject(hdc, hOldPen);
DeleteObject(hPen);
ReleaseDC(hwnd, hdc);
}
return 0;
case WM_LBUTTONUP:
case WM_RBUTTONUP:
if (bDrawing) {
ReleaseCapture();
bDrawing = FALSE;
ptPrev.x = -1;
ptPrev.y = -1;
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
```
注意事项:
1.按下“L”、“E”、“C”键后,需要调用SetCapture函数捕获鼠标,这样当鼠标移动时才会收到WM_MOUSEMOVE消息。
2.在WM_MOUSEMOVE消息中,根据当前绘制的图形类型(直线、矩形、圆),使用MoveToEx、LineTo、Rectangle、Ellipse等函数绘制图形。
3.在WM_LBUTTONUP、WM_RBUTTONUP消息中,需要调用ReleaseCapture函数释放鼠标捕获,以及重置绘制状态。
4.使用CreatePen、SelectObject、DeleteObject等函数来设置画笔的颜色和属性。
5.在WM_KEYDOWN消息中,按下“R”、“G”、“B”键时改变画笔颜色。
阅读全文