0

我有以下问题。在我的 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

你能帮我解决这个问题吗?

4

1 回答 1

1

每次您WM_COMMAND从除SETTINGS_BUTTON_SAVEor之外的任何控件收到消息时SETTINGS_BUTTON_CANCEL,您的窗口过程都会转到WM_CREATEcase 和调用AddControlsToSettingsDialog。您正在创建越来越多的子窗口,它们相互堆叠。

此外,SettingsDialogProcedure被声明为 return LRESULT,但它可能会在没有遇到return语句的情况下到达右大括号。这表现出未定义的行为。

于 2018-08-25T13:29:06.713 回答