按要求写c++代码:在窗口用户区显示红色字体“Welcome to Zhejiang University”.要求如下: 1) 始终(包括窗口放大或缩小)显示在窗口用户区中央,颜色红色,字体默认(5分) 2)按下键盘向左方向键,整个字符串向左移动,当移到最左端时,停止移动,跳出消息框“已到左边界!”(7分) 3)按下鼠标右键,同时拖动鼠标,字符串将字符串跟随鼠标光标移动,并在放开鼠标时,停留在鼠标放开时的位置(请注意,字符串的位置为第一字符的位置)(9分) 4)按住键盘Shift键,跳出模式对话框,对话框中有一编辑框,如果用户在编辑框中输入字符串,并按对话框中“确定”按钮,则主窗口用户区字符串“Welcome to Zhejiang University”将被所输入的字符串替代。
时间: 2024-02-26 18:52:16 浏览: 113
以下是实现上述要求的 C++ 代码,使用 Win32 API 编写:
```c++
#include <Windows.h>
#include <tchar.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
// Register Window Class
const TCHAR szClassName[] = _T("MyWindowClass");
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = szClassName;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
if (!RegisterClass(&wc)) {
MessageBox(NULL, _T("Register Window Class Failed!"), _T("Error"), MB_ICONERROR | MB_OK);
return 0;
}
// Create Window
HWND hWnd = CreateWindow(szClassName, _T("My Window"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL);
if (!hWnd) {
MessageBox(NULL, _T("Create Window Failed!"), _T("Error"), MB_ICONERROR | MB_OK);
return 0;
}
// Show Window
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// Message Loop
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
static TCHAR szText[] = _T("Welcome to Zhejiang University");
static int cxClient, cyClient;
static int xText, yText;
static bool bShiftDown = false;
static TCHAR szBuffer[256];
switch (message) {
case WM_CREATE:
SetTimer(hWnd, 1, 50, NULL);
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
xText = (cxClient - lstrlen(szText) * 8) / 2; // 8 is the width of a character
yText = cyClient / 2;
return 0;
case WM_PAINT:
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
SetTextColor(hdc, RGB(255, 0, 0));
TextOut(hdc, xText, yText, szText, lstrlen(szText));
EndPaint(hWnd, &ps);
return 0;
case WM_TIMER:
if (!bShiftDown) {
xText -= 5;
if (xText < 0) {
xText = 0;
KillTimer(hWnd, 1);
MessageBox(hWnd, _T("已到左边界!"), _T("提示"), MB_ICONINFORMATION | MB_OK);
SetTimer(hWnd, 1, 50, NULL);
}
InvalidateRect(hWnd, NULL, TRUE);
}
return 0;
case WM_RBUTTONDOWN:
SetCapture(hWnd);
xText = LOWORD(lParam);
yText = HIWORD(lParam);
SetCursor(LoadCursor(NULL, IDC_SIZEALL));
return 0;
case WM_MOUSEMOVE:
if (wParam & MK_RBUTTON) {
xText += LOWORD(lParam) - xText;
yText += HIWORD(lParam) - yText;
InvalidateRect(hWnd, NULL, TRUE);
}
return 0;
case WM_RBUTTONUP:
ReleaseCapture();
SetCursor(LoadCursor(NULL, IDC_ARROW));
return 0;
case WM_KEYDOWN:
if (wParam == VK_SHIFT) {
bShiftDown = true;
DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), hWnd, NULL);
bShiftDown = false;
return 0;
}
return 0;
case WM_CLOSE:
if (MessageBox(hWnd, _T("确定要退出程序吗?"), _T("退出"), MB_ICONQUESTION | MB_YESNO) == IDYES) {
DestroyWindow(hWnd);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK) {
GetDlgItemText((HWND)lParam, IDC_EDIT1, szBuffer, 256);
lstrcpy(szText, szBuffer);
InvalidateRect(hWnd, NULL, TRUE);
}
return 0;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
```
其中,还需要添加一个对话框资源 IDD_DIALOG1 和一个编辑框资源 IDC_EDIT1。这里只展示代码部分,完整代码可以在我的 GitHub 仓库中查看:https://github.com/jerrita/Win32-Tutorials/blob/master/Tutorial%2005%20-%20Display%20Text%20and%20Handle%20Input/Tutorial05.cpp
阅读全文