#include <windows.h>
#include <commdlg.h>
#include <vector>
#include <string>
// 菜单命令标识符
#define IDM_OPEN 1001
#define IDM_SAVE 1002
#define IDM_EXIT 1003
#define IDM_ABOUT 1004
// 控件ID定义
#define IDC_INPUT_EDIT 105
#define IDC_DISPLAY_EDIT 106
#define IDC_LOCATION_LIST 107
#define IDC_CHECKBOX 108
#define IDC_SUBMIT_BUTTON 109
// 广东地名列表
const std::vector<std::wstring> guangdongCities = {
L"广州市", L"深圳市", L"珠海市", L"汕头市", L"佛山市",
L"韶关市", L"湛江市", L"肇庆市", L"江门市", L"茂名市",
L"惠州市", L"梅州市", L"汕尾市", L"河源市", L"阳江市",
L"清远市", L"东莞市", L"中山市", L"潮州市", L"揭阳市", L"云浮市"
};
// 窗口过程函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE: {
// 创建文本标签
CreateWindow(L"STATIC", L"输入内容:", WS_VISIBLE | WS_CHILD,
20, 20, 80, 20, hWnd, NULL, NULL, NULL);
CreateWindow(L"STATIC", L"选择地点:", WS_VISIBLE | WS_CHILD,
20, 60, 80, 20, hWnd, NULL, NULL, NULL);
CreateWindow(L"STATIC", L"输出结果:", WS_VISIBLE | WS_CHILD,
250, 20, 80, 20, hWnd, NULL, NULL, NULL);
// 创建文本输入框
CreateWindow(L"EDIT", L"", WS_CHILD | WS_VISIBLE | WS_BORDER,
100, 20, 120, 25, hWnd, (HMENU)IDC_INPUT_EDIT, NULL, NULL);
// 创建列表框(含广东地名)
HWND hList = CreateWindow(L"LISTBOX", L"", WS_CHILD | WS_VISIBLE | WS_BORDER |
LBS_STANDARD | LBS_HASSTRINGS | WS_VSCROLL,
20, 85, 200, 150, hWnd, (HMENU)IDC_LOCATION_LIST, NULL, NULL);
// 填充广东地名
for (const auto& city : guangdongCities) {
SendMessage(hList, LB_ADDSTRING, 0, (LPARAM)city.c_str());
}
SendMessage(hList, LB_SETCURSEL, 0, 0); // 默认选中第一项
// 创建复选框
CreateWindow(L"BUTTON", L"包含详细信息", WS_CHILD | WS_VISIBLE | BS_CHECKBOX,
20, 250, 150, 25, hWnd, (HMENU)IDC_CHECKBOX, NULL, NULL);
// 创建提交按钮
CreateWindow(L"BUTTON", L"显示选择结果", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
20, 285, 120, 30, hWnd, (HMENU)IDC_SUBMIT_BUTTON, NULL, NULL);
// 创建文本框(显示结果)
CreateWindow(L"EDIT", L"选择结果将显示在这里",
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE | WS_VSCROLL | ES_READONLY,
250, 40, 300, 280, hWnd, (HMENU)IDC_DISPLAY_EDIT, NULL, NULL);
break;
}
case WM_COMMAND: {
if (LOWORD(wParam) == IDC_SUBMIT_BUTTON) {
// 获取输入框内容
wchar_t inputText[256] = { 0 };
GetWindowText(GetDlgItem(hWnd, IDC_INPUT_EDIT), inputText, 256);
// 获取列表选中项
wchar_t selectedCity[256] = { 0 };
int selIndex = SendMessage(GetDlgItem(hWnd, IDC_LOCATION_LIST), LB_GETCURSEL, 0, 0);
if (selIndex != LB_ERR) {
SendMessage(GetDlgItem(hWnd, IDC_LOCATION_LIST),
LB_GETTEXT, selIndex, (LPARAM)selectedCity);
}
// 获取复选框状态
bool isChecked = (SendMessage(GetDlgItem(hWnd, IDC_CHECKBOX),
BM_GETCHECK, 0, 0) == BST_CHECKED);
// 组合结果显示
wchar_t result[1024] = { 0 };
if (isChecked) {
wsprintf(result,
L"您输入的内容是: %s\n\n您选择的地点是: %s\n\n当前时间: %s\n操作状态: 成功完成",
inputText,
selectedCity,
__TIMEW__);
}
else {
wsprintf(result,
L"输入内容: %s\n选择城市: %s",
inputText,
selectedCity);
}
SetWindowText(GetDlgItem(hWnd, IDC_DISPLAY_EDIT), result);
}
else if (LOWORD(wParam) == IDM_OPEN) {
// 创建打开文件对话框
OPENFILENAMEW ofn;
wchar_t szFile[260] = { 0 };
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWnd;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = L"文本文件 (*.txt)\0*.txt\0所有文件 (*.*)\0*.*\0";
ofn.nFilterIndex = 1;
ofn.lpstrTitle = L"选择要打开的文件";
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (GetOpenFileNameW(&ofn)) {
MessageBoxW(hWnd, szFile, L"已选择文件", MB_OK | MB_ICONINFORMATION);
}
break;
}
else if (LOWORD(wParam) == IDM_SAVE) {
// 创建保存文件对话框
OPENFILENAMEW ofn;
wchar_t szFile[260] = { 0 };
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWnd;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = L"文本文件 (*.txt)\0*.txt\0所有文件 (*.*)\0*.*\0";
ofn.nFilterIndex = 1;
ofn.lpstrDefExt = L"txt";
ofn.lpstrTitle = L"保存文件";
ofn.Flags = OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
if (GetSaveFileNameW(&ofn)) {
wchar_t message[300];
swprintf(message, 300, L"文件已保存到:\n%s", szFile);
MessageBoxW(hWnd, message, L"保存成功", MB_OK | MB_ICONINFORMATION);
}
break;
}
else if (LOWORD(wParam) == IDM_EXIT) {
PostQuitMessage(0);
break;
}
else if (LOWORD(wParam) == IDM_ABOUT) {
MessageBoxW(hWnd, L"广东城市选择器\n版本 2.0\n添加了城市选择功能",
L"关于", MB_OK | MB_ICONINFORMATION);
break;
}
break;
}
case WM_SIZE: {
// 调整文本框大小以适应窗口
RECT rect;
GetClientRect(hWnd, &rect);
MoveWindow(GetDlgItem(hWnd, IDC_DISPLAY_EDIT),
rect.right - 320, 40,
300, rect.bottom - 60, TRUE);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProcW(hWnd, msg, wParam, lParam);
}
return 0;
}
// 创建主菜单
HMENU CreateMainMenu() {
HMENU hMenu = CreateMenu();
HMENU hFileMenu = CreatePopupMenu();
HMENU hHelpMenu = CreatePopupMenu();
// 文件菜单
AppendMenuW(hFileMenu, MF_STRING, IDM_OPEN, L"打开(&O)");
AppendMenuW(hFileMenu, MF_STRING, IDM_SAVE, L"保存(&S)");
AppendMenuW(hFileMenu, MF_SEPARATOR, 0, NULL);
AppendMenuW(hFileMenu, MF_STRING, IDM_EXIT, L"退出(&X)");
// 帮助菜单
AppendMenuW(hHelpMenu, MF_STRING, IDM_ABOUT, L"关于(&A)");
// 主菜单栏
AppendMenuW(hMenu, MF_POPUP, (UINT_PTR)hFileMenu, L"文件(&F)");
AppendMenuW(hMenu, MF_POPUP, (UINT_PTR)hHelpMenu, L"帮助(&H)");
return hMenu;
}
// 程序入口点
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) {
// 注册窗口类
const wchar_t CLASS_NAME[] = L"CitySelectorClass";
WNDCLASSW wc = { 0 };
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
if (!RegisterClassW(&wc)) {
MessageBoxW(NULL, L"窗口类注册失败!", L"错误", MB_ICONERROR);
return 1;
}
// 创建菜单
HMENU hMenu = CreateMainMenu();
// 创建窗口
HWND hWnd = CreateWindowExW(
0,
CLASS_NAME,
L"广东城市选择器",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
800, 500,
NULL,
hMenu, // 附加菜单
hInstance,
NULL
);
if (hWnd == NULL) {
MessageBoxW(NULL, L"窗口创建失败!", L"错误", MB_ICONERROR);
return 1;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// 消息循环
MSG msg;
while (GetMessageW(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return (int)msg.wParam;
}
提示“__TIMEW__”: 未声明的标识符