酷代码 AI
菜单

用c++写一个图形界面程序,有七个球,可以点击球然后按上下左右键移动球,七个球的颜色分别为红、橙、黄、绿、青、蓝、紫。不含第三方内容,球移动后原位置的球立即消失。不含控制台(黑框),也不含任何错误

以下是一个使用 Windows API 编写的 C++ 图形界面程序,该程序会显示七个颜色分别为红、橙、黄、绿、青、蓝、紫的球,你可以点击球然后使用上下左右键移动球,球移动后原位置的球会立即消失,且不显示控制台窗口。 ```cpp #include <windows.h> // 定义球的结构体 struct Ball { int x, y; int radius; COLORREF color; bool selected; }; // 全局变量 const int NUM_BALLS = 7; Ball balls[NUM_BALLS]; Ball* selectedBall = nullptr; // 初始化球的位置和颜色 void initBalls() { int radius = 20; int startX = 50; int startY = 50; COLORREF colors[NUM_BALLS] = { RGB(255, 0, 0), RGB(255, 165, 0), RGB(255, 255, 0), RGB(0, 128, 0), RGB(0, 255, 255), RGB(0, 0, 255), RGB(128, 0, 128) }; for (int i = 0; i < NUM_BALLS; ++i) { balls[i].x = startX + i * (radius * 2 + 20); balls[i].y = startY; balls[i].radius = radius; balls[i].color = colors[i]; balls[i].selected = false; } } // 绘制球 void drawBalls(HDC hdc) { for (int i = 0; i < NUM_BALLS; ++i) { HBRUSH hBrush = CreateSolidBrush(balls[i].color); SelectObject(hdc, hBrush); Ellipse(hdc, balls[i].x - balls[i].radius, balls[i].y - balls[i].radius, balls[i].x + balls[i].radius, balls[i].y + balls[i].radius); DeleteObject(hBrush); } } // 检查点是否在球内 bool isPointInBall(int x, int y, const Ball& ball) { int dx = x - ball.x; int dy = y - ball.y; return dx * dx + dy * dy <= ball.radius * ball.radius; } // 窗口过程函数 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_CREATE: initBalls(); break; case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); drawBalls(hdc); EndPaint(hwnd, &ps); break; } case WM_LBUTTONDOWN: { int x = LOWORD(lParam); int y = HIWORD(lParam); selectedBall = nullptr; for (int i = 0; i < NUM_BALLS; ++i) { if (isPointInBall(x, y, balls[i])) { selectedBall = &balls[i]; balls[i].selected = true; } else { balls[i].selected = false; } } InvalidateRect(hwnd, nullptr, TRUE); break; } case WM_KEYDOWN: { if (selectedBall != nullptr) { int step = 5; switch (wParam) { case VK_UP: selectedBall->y -= step; break; case VK_DOWN: selectedBall->y += step; break; case VK_LEFT: selectedBall->x -= step; break; case VK_RIGHT: selectedBall->x += step; break; } InvalidateRect(hwnd, nullptr, TRUE); } break; } case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; } // 主函数 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // 定义窗口类 WNDCLASS wc = { 0 }; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.lpszClassName = L"BallWindowClass"; // 注册窗口类 RegisterClass(&wc); // 创建窗口 HWND hwnd = CreateWindow(wc.lpszClassName, L"Ball Mover", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, nullptr, nullptr, hInstance, nullptr); // 显示窗口 ShowWindow(hwnd, nCmdShow); // 消息循环 MSG msg; while (GetMessage(&msg, nullptr, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } ``` 代码说明: 1. Ball 结构体:用于存储球的位置、半径、颜色和是否被选中的状态。 2. initBalls 函数:初始化七个球的位置和颜色。 3. drawBalls 函数:在窗口中绘制所有的球。 4. isPointInBall 函数:检查一个点是否在球内。 5. WndProc 函数:窗口过程函数,处理各种窗口消息,包括创建窗口、绘制窗口、鼠标点击和键盘按键事件。 6. WinMain 函数:程序的入口点,负责注册窗口类、创建窗口、显示窗口和处理消息循环。 编译和运行: 将上述代码保存为一个 `.cpp` 文件,使用支持 Windows API 的 C++ 编译器(如 Visual Studio)进行编译和运行。程序运行后会弹出一个窗口,显示七个不同颜色的球,你可以点击球然后使用上下左右键移动它。 [2026-01-23 08:37:03 | AI问答 | 1500点数解答]

服务商
相关提问
发财导航,免费问AI
实用工具查看更多