I have a simple program with a button and an entry field, and I'm wondering how can I organize them (like they were in a table) so that they don't overlap? I'm using the windows api. I tried searching but I'm not sure what to call this.

Dani AI

Generated

pointed toward manual positioning; that works for a tiny number of controls but becomes fragile once the window resizes or more controls are added. A more robust pattern is to either use a dialog template (resource editor) or implement a small layout routine that positions child controls from the parent client rectangle and runs on WM_CREATE and WM_SIZE.

Example: compute a grid and move controls on WM_SIZE. The routine uses GetClientRect and SetWindowPos/MoveWindow to place each control.

void LayoutControls(HWND hwndParent)
{
    RECT rc;
    GetClientRect(hwndParent, &rc);
    int padding = 8, cols = 2;
    int cellW = (rc.right - rc.left - padding*(cols+1)) / cols;
    int cellH = 24;
    HWND hEdit = GetDlgItem(hwndParent, IDC_EDIT1);
    HWND hBtn  = GetDlgItem(hwndParent, IDC_BUTTON1);
    SetWindowPos(hEdit, NULL, padding, padding, cellW, cellH, SWP_NOZORDER);
    SetWindowPos(hBtn,  NULL, padding+cellW+padding, padding, cellW, cellH, SWP_NOZORDER);
}

For many controls, use the BeginDeferWindowPos/DeferWindowPos/EndDeferWindowPos trio to move them in a single batch and reduce flicker:

HDWP hdwp = BeginDeferWindowPos(2);
hdwp = DeferWindowPos(hdwp, hEdit, NULL, x1, y1, w1, h1, SWP_NOZORDER);
hdwp = DeferWindowPos(hdwp, hBtn,  NULL, x2, y2, w2, h2, SWP_NOZORDER);
EndDeferWindowPos(hdwp);

Use dialog units and MapDialogRect to keep layouts consistent across fonts/DPI, or supply a DPI-aware manifest for modern scaling. Controls must be created as child windows (WS_CHILD | WS_VISIBLE) with the main window as parent. For reference, see the Win32 docs for MapDialogRect and DeferWindowPos.

This approach keeps layout predictable, avoids overlapping when resized, and scales better than hand-tweaking coordinates. can use the dialog editor if the UI will stay static, or the layout routine above if dynamic resizing is needed.

Recommended Answers

All 2 Replies

Assuming your using CreateWindowEx, change the value of y (vertical position of window) before the next window is created.

Thanks Tight_Coder_Ex.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.