用WIN API创建一个带edit文本框的窗口程序,并且edit文本框大小跟随窗口变化,窗口下部有"submit"和"quit"按钮,附上"submit"和"quit"按钮的单击回调, 其中单击"quit"按钮退出程序,改变窗口大小时需要重绘,所有字符串加L修饰,字符串类型要保持正确
时间: 2023-07-06 08:38:58 浏览: 157
好的,我可以为你提供一个简单的示例程序,该程序创建一个带有自适应大小编辑框和“提交”、“退出”按钮的窗口,并且有对应的回调函数。
首先,我们需要包含一些头文件:
```c++
#include <Windows.h>
#include <wchar.h>
```
然后,我们需要定义一些常量:
```c++
#define WINDOW_CLASS_NAME L"MyWindowClass"
#define WINDOW_TITLE L"My Window"
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
#define BUTTON_WIDTH 100
#define BUTTON_HEIGHT 30
#define EDIT_WIDTH 400
#define EDIT_HEIGHT 200
#define EDIT_PADDING 10
#define ID_EDIT 100
#define ID_SUBMIT 101
#define ID_QUIT 102
```
接下来,我们需要定义一些全局变量:
```c++
HWND g_hEdit;
HWND g_hSubmit;
HWND g_hQuit;
```
然后,我们需要定义窗口过程函数:
```c++
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
// 创建编辑框
g_hEdit = CreateWindowExW(
0,
L"EDIT",
L"",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
EDIT_PADDING,
EDIT_PADDING,
EDIT_WIDTH,
EDIT_HEIGHT,
hwnd,
(HMENU)ID_EDIT,
GetModuleHandleW(NULL),
NULL
);
// 创建提交按钮
g_hSubmit = CreateWindowExW(
0,
L"BUTTON",
L"Submit",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
WINDOW_WIDTH / 2 - BUTTON_WIDTH - 10,
WINDOW_HEIGHT - BUTTON_HEIGHT - 10,
BUTTON_WIDTH,
BUTTON_HEIGHT,
hwnd,
(HMENU)ID_SUBMIT,
GetModuleHandleW(NULL),
NULL
);
// 创建退出按钮
g_hQuit = CreateWindowExW(
0,
L"BUTTON",
L"Quit",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
WINDOW_WIDTH / 2 + 10,
WINDOW_HEIGHT - BUTTON_HEIGHT - 10,
BUTTON_WIDTH,
BUTTON_HEIGHT,
hwnd,
(HMENU)ID_QUIT,
GetModuleHandleW(NULL),
NULL
);
break;
case WM_SIZE:
// 重新布局窗口控件
MoveWindow(g_hEdit, EDIT_PADDING, EDIT_PADDING, LOWORD(lParam) - 2 * EDIT_PADDING, HIWORD(lParam) - 3 * BUTTON_HEIGHT - 4 * EDIT_PADDING, TRUE);
MoveWindow(g_hSubmit, LOWORD(lParam) / 2 - BUTTON_WIDTH - 10, HIWORD(lParam) - BUTTON_HEIGHT - 10, BUTTON_WIDTH, BUTTON_HEIGHT, TRUE);
MoveWindow(g_hQuit, LOWORD(lParam) / 2 + 10, HIWORD(lParam) - BUTTON_HEIGHT - 10, BUTTON_WIDTH, BUTTON_HEIGHT, TRUE);
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case ID_SUBMIT:
// 提交按钮被单击
// 在这里添加提交按钮的单击回调代码
break;
case ID_QUIT:
// 退出按钮被单击
PostQuitMessage(0);
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}
return 0;
}
```
最后,我们需要定义WinMain函数:
```c++
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
// 注册窗口类
WNDCLASSEXW wcex = { 0 };
wcex.cbSize = sizeof(WNDCLASSEXW);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WindowProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = WINDOW_CLASS_NAME;
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassExW(&wcex))
{
return 0;
}
// 创建窗口
HWND hwnd = CreateWindowExW(
0,
WINDOW_CLASS_NAME,
WINDOW_TITLE,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
WINDOW_WIDTH, WINDOW_HEIGHT,
NULL,
NULL,
hInstance,
NULL
);
if (!hwnd)
{
return 0;
}
// 显示窗口
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// 消息循环
MSG msg;
while (GetMessageW(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return (int)msg.wParam;
}
```
现在,我们就完成了一个简单的带有自适应大小编辑框和“提交”、“退出”按钮的窗口程序。你可以在提交按钮的单击回调函数中添加你自己的代码。
阅读全文