user966939
user966939

Reputation: 726

Setting client area size (after creation)?

I want to adjust the height of the client area after creation of the main window. I use CW_USEDEFAULT when creating it and I simply want to resize that to the nearest height which is divisible by a fixed number. I've tried processing WM_SIZE, but it doesn't seem to work like I want it to, and I'm not really sure SetWindowPos is the appropriate way to resize the client area.. ?

LRESULT CALLBACK mainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message) {
      case WM_SIZE:
        if (HIWORD(lParam) % 15) {
          SetWindowPos(hWnd, NULL, 0, 0, LOWORD(lParam), HIWORD(lParam) / 15 * 15, SWP_NOMOVE);
        }
        return DefWindowProc(hWnd, message, wParam, lParam);
        break;

      case WM_DESTROY:
        PostQuitMessage(0);
        break;

      default:
        return DefWindowProc(hWnd, message, wParam, lParam);
        break;
    }

    return 0;
}

The result is a zero-height client area for some reason:

Test

Another couple of approaches I've tried:

Any ideas?

Upvotes: 1

Views: 2449

Answers (2)

user966939
user966939

Reputation: 726

I realized my if-statement wasn't working as intended, since, when SetWindowPos is used to update the main window, it actually sets the full window height, whereas HIWORD(lParam) is only the client area height. In my case (Windows 7), the client area and full window height simply aren't both divisible by 15 at any point, so it will essentially keep looping. So here's my solution:

int initSize = 0; /* global */

/* ... */

case WM_SIZE:
    if (!initSize && HIWORD(lParam) % 15) {
        RECT r;
        GetClientRect(hWnd, &r);
        r.bottom = r.top + (HIWORD(lParam) / 15 * 15);
        AdjustWindowRect(&r, GetWindowLong(hWnd, GWL_STYLE), TRUE);
        SetWindowPos(hWnd, NULL, 0, 0, r.right - r.left, r.bottom - r.top, SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOMOVE);
        initSize = 1;
    }
    return DefWindowProc(hWnd, message, wParam, lParam);
    break;

Upvotes: 0

Paul Ogilvie
Paul Ogilvie

Reputation: 25286

I use the following, but not from the WM_SIZE message, just from a function:

r= <a rect with the size you want>;
/* Compute the size of the window rectangle based on the given
 * client rectangle size and the window style, then size the
 * client window.
 */
AdjustWindowRect (&r, GetWindowLong(hWnd, GWL_STYLE), FALSE);
SetWindowPos (hWnd, 0,
          r.left, r.top,
          r.right  - r.left,
          r.bottom - r.top,
          SWP_NOZORDER | SWP_NOACTIVATE);

UpdateWindow(hWnd);                         

Upvotes: 1

Related Questions