对话框窗口重新初始化问题

时间:2018-08-25 13:22:46

标签: c++ winapi dialog radio-button createwindow

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

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

1 个答案:

答案 0 :(得分:1)

每次您从WM_COMMANDSETTINGS_BUTTON_SAVE以外的任何控件收到SETTINGS_BUTTON_CANCEL消息时,您的窗口过程都会遇到WM_CREATE情况并调用AddControlsToSettingsDialog 。您正在创建越来越多的子窗口,它们相互堆叠。

此外,SettingsDialogProcedure被声明为返回LRESULT,但是它可能会到达右括号而不会遇到return语句。这表现出不确定的行为。