【发布时间】:2018-08-25 13:22:46
【问题描述】:
我有以下问题。在我的 c++ 项目中,我想创建一个对话框窗口来输入要使用的应用程序的设置。
单击菜单时会创建窗口:application -> robot settings。
我通过以下方式创建了窗口:
void Robot_Settings::CreateSettingsWindow(HWND hWnd, RECT& windowSize)
{
GetWindowRect(hWnd, &windowSize);
HWND hSDlg = CreateWindowW(
L"SettingsDialogClass",
NULL,
WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU | WS_VISIBLE | WS_CLIPSIBLINGS,
(windowSize.right - SETTINGS_WINDOW_SIZE) / 2,
(windowSize.bottom - SETTINGS_WINDOW_SIZE) / 2,
SETTINGS_WINDOW_SIZE,
SETTINGS_WINDOW_SIZE,
hWnd,
NULL,
NULL,
NULL
);
}
然后我有窗口过程:
LRESULT CALLBACK Robot_Settings::SettingsDialogProcedure(HWND hSDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_COMMAND:
switch (wParam)
{
case SETTINGS_BUTTON_SAVE:
collectInputs(hSDlg);
break;
case SETTINGS_BUTTON_CANCEL:
DestroyWindow(hSDlg);
break;
}
case WM_CREATE:
AddControlsToSettingsDialog(hSDlg);
break;
case WM_DESTROY:
DestroyWindow(hSDlg);
break;
default:
return DefWindowProcW(hSDlg, msg, wParam, lParam);
}
}
我在WM_CREATE 上添加内部控件:
void Robot_Settings::AddControlsToSettingsDialog(HWND hSDlg)
{
// Create Text info about amount input
CreateWindowW(
L"Static",
L"Input the amount of robots you want to get simulated:",
WS_VISIBLE | WS_CHILD,
(int)(SETTINGS_WINDOW_SIZE * 0.15),
(int)(SETTINGS_WINDOW_SIZE * 0.2),
(int)(SETTINGS_WINDOW_SIZE / 2),
(int)(SETTINGS_WINDOW_SIZE / 2),
hSDlg,
NULL,
NULL,
NULL
);
// Create Text info about formation input
CreateWindowW(
L"Static",
L"Input the formation of th robots in this format -> \"x-x-x-x\" where x is the amount of robots you want to have in each row:",
WS_VISIBLE | WS_CHILD,
(int)(SETTINGS_WINDOW_SIZE * 0.15),
(int)(SETTINGS_WINDOW_SIZE * 0.4),
(int)(SETTINGS_WINDOW_SIZE / 2),
(int)(SETTINGS_WINDOW_SIZE / 2),
hSDlg,
NULL,
NULL,
NULL
);
// Create Text info about speed input
CreateWindowW(
L"Static",
L"Input the speed calculated in squares per second at which you want the robots to move:",
WS_VISIBLE | WS_CHILD,
(int)(SETTINGS_WINDOW_SIZE * 0.15),
(int)(SETTINGS_WINDOW_SIZE * 0.6),
(int)(SETTINGS_WINDOW_SIZE / 2),
(int)(SETTINGS_WINDOW_SIZE / 2),
hSDlg,
NULL,
NULL,
NULL
);
// Create TextField for amount input
hAmount = CreateWindowW(
L"Edit",
L"",
WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP,
(int)(SETTINGS_WINDOW_SIZE * 0.7),
(int)(SETTINGS_WINDOW_SIZE * 0.2),
100,
20,
hSDlg,
NULL,
NULL,
NULL
);
//formation radio buttons
CreateWindowW(
L"BUTTON",
L"Formation",
WS_VISIBLE | WS_CHILD | BS_GROUPBOX,
(int)(SETTINGS_WINDOW_SIZE * 0.7),
(int)(SETTINGS_WINDOW_SIZE * 0.35),
120,
120,
hSDlg,
NULL,
NULL,
NULL);
hTriangle = CreateWindowW(
L"BUTTON",
L"Triangle",
WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP,
(int)(SETTINGS_WINDOW_SIZE * 0.72),
(int)(SETTINGS_WINDOW_SIZE * 0.4),
100,
20,
hSDlg,
(HMENU) ID_RADIO_TRIANGLE,
NULL,
NULL);
hRectangle = CreateWindowW(
L"BUTTON",
L"Rectangle",
WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP,
(int)(SETTINGS_WINDOW_SIZE * 0.72),
(int)(SETTINGS_WINDOW_SIZE * 0.45),
100,
20,
hSDlg,
(HMENU) ID_RADIO_RECTANGLE,
NULL,
NULL);
hRhombus = CreateWindowW(
L"BUTTON",
L"Rhombus",
WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP,
(int)(SETTINGS_WINDOW_SIZE * 0.72),
(int)(SETTINGS_WINDOW_SIZE * 0.5),
100,
20,
hSDlg,
(HMENU) ID_RADIO_RHOMBUS,
NULL,
NULL);
// Create TextField for speed input
hSpeed = CreateWindowW(
L"Edit",
L"",
WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP,
(int)(SETTINGS_WINDOW_SIZE * 0.7),
(int)(SETTINGS_WINDOW_SIZE * 0.6),
100,
20,
hSDlg,
NULL,
NULL,
NULL
);
// Create button Cancel
CreateWindowW(
L"Button",
L"Cancel",
WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP,
(int)(SETTINGS_WINDOW_SIZE * 0.7),
(int)(SETTINGS_WINDOW_SIZE * 0.8),
100,
40,
hSDlg,
(HMENU)SETTINGS_BUTTON_CANCEL,
NULL,
NULL
);
// Create button Save
CreateWindowW(
L"Button",
L"Save",
WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP,
(int)(SETTINGS_WINDOW_SIZE * 0.7 - 110),
(int)(SETTINGS_WINDOW_SIZE * 0.8),
100,
40,
hSDlg,
(HMENU)SETTINGS_BUTTON_SAVE,
NULL,
NULL
);
}
所以,我的问题是,每次我尝试输入任何文本或单击单选按钮时,窗口似乎都会重新初始化或重新绘制(无法弄清楚发生了什么,但无论如何这不是正常行为)。如果您选中 rectangle 单选按钮,窗口将关闭。
您可以在以下位置找到整个项目: https://github.com/JamesHawkJ/cpp/tree/master/WycieczkaRobotow
你能帮我解决这个问题吗?
【问题讨论】:
-
SettingsDialogProcedure表现出未定义的行为,通过到达右大括号而不执行return语句。 -
另外,
WM_COMMAND的处理由WM_CREATE负责。这可能不是你的意思。 -
另外,与您的问题无关,因为您想创建一个对话框,您应该调用CreateDialogW 系列函数之一(请参阅dialog boxes)。这允许您使用资源 (*.rc) 脚本创作对话框,并免费获得键盘导航。
标签: c++ winapi dialog radio-button createwindow