zapshe
zapshe

Reputation: 278

How to resize/reposition controls of a Window, when the window size changes

When using any program, resizing the window makes the controls change location to fit on the window. How do you do this using C++? I've seen a lot of C# examples, but C++ ones were no where.

Upvotes: 1

Views: 1107

Answers (1)

Zeus
Zeus

Reputation: 3890

You can use SetWindowPos API in WM_SIZE message:

I create a sample and use the following code:

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HWND hButton;
    switch (message)
    {
    case WM_CREATE:
        hButton = CreateWindow(TEXT("Button"), TEXT("OK"), WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 50, 50, 50, 50, hwnd, NULL, NULL, NULL);
        return 0;
    case WM_SIZE:
    {
        int cxClient = LOWORD(lParam);
        int cyClient = HIWORD(lParam);
        SetWindowPos(hButton, NULL, cxClient / 3, cyClient / 4, cxClient / 5, cyClient / 2, SWP_SHOWWINDOW);
        return 0;
    }
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}

The created button will be resized according to my resize window:

enter image description here

Upvotes: 4

Related Questions