VC6运行鼠标控制旋转的正方形 ,掌握程序鼠标交互方法,尝试为其添加键盘与菜单控制,实 现同样功能
时间: 2023-12-09 16:02:46 浏览: 133
Unity 鼠标控制场景 旋转 放大 缩小 平移等功能 可自行修改源代码
5星 · 资源好评率100%
以下是一个VC6运行鼠标控制旋转正方形的示例程序,其中包含了鼠标交互的代码:
```cpp
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static char szAppName[] = "RotateSquare";
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, "This program requires Windows NT!", szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName, "Rotate Square",
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 POINT ptCenter;
static POINT ptSquare[] = { {-50,-50},{50,-50},{50,50},{-50,50},{-50,-50} };
static int iAngle;
HDC hdc;
int i;
PAINTSTRUCT ps;
switch (message)
{
case WM_CREATE:
ptCenter.x = (int)(0.5 * cxClient);
ptCenter.y = (int)(0.5 * cyClient);
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
ptCenter.x = (int)(0.5 * cxClient);
ptCenter.y = (int)(0.5 * cyClient);
return 0;
case WM_KEYDOWN:
switch (wParam)
{
case VK_LEFT:
iAngle -= 5;
InvalidateRect(hwnd, NULL, TRUE);
break;
case VK_RIGHT:
iAngle += 5;
InvalidateRect(hwnd, NULL, TRUE);
break;
}
return 0;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDM_EXIT:
DestroyWindow(hwnd);
break;
case IDM_ABOUT:
MessageBox(hwnd, "Rotate Square\n\n(c) 2021 YourName\n\nhttps://www.example.com", "About", MB_ICONINFORMATION);
break;
}
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
SelectObject(hdc, GetStockObject(BLACK_PEN));
for (i = 0; i < 4; i++)
{
ptSquare[i].x = (int)(50 * cos(iAngle + i * 3.14159 / 2) + ptCenter.x);
ptSquare[i].y = (int)(-50 * sin(iAngle + i * 3.14159 / 2) + ptCenter.y);
}
Polyline(hdc, ptSquare, 5);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
```
这个程序可以通过鼠标控制正方形的旋转,按下左右箭头键可以使正方形向左或向右旋转,同时可以添加菜单控制。在代码中,菜单控制的实现使用了 WM_COMMAND 消息,在菜单项被点击时触发相应的操作。
注意,在使用菜单控制时,需要在窗口的 WndProc 函数中处理 WM_COMMAND 消息,根据菜单项的 ID 进行相应的操作。在示例程序中,我们添加了名为 IDM_EXIT 和 IDM_ABOUT 的两个菜单项,分别用于退出程序和显示关于对话框。我们可以在程序的 WndProc 函数中添加以下代码来处理这两个菜单项的点击事件:
```cpp
switch (LOWORD(wParam))
{
case IDM_EXIT:
DestroyWindow(hwnd);
break;
case IDM_ABOUT:
MessageBox(hwnd, "Rotate Square\n\n(c) 2021 YourName\n\nhttps://www.example.com", "About", MB_ICONINFORMATION);
break;
}
```
阅读全文