The easiest way is to handle the WM_SIZE message, which is called after the window receives its new size, but before it is repainted. You simply compare the new width to your desired width. If it is identical, do nothing. If it differs, call the SetWindowPos() API with the new height (received in the WM_SIZE message) and your desired width.
For example, the following uses the Message Crackers in
<windowsx.h> to parse the message:
static void onSize(HWND hwnd, UINT , int cx, int cy)
{
BOOL resize = FALSE;
if (cx > wintext_max_width)
{
resize = TRUE;
cx = wintext_max_width;
}
if (cy > wintext_max_height)
{
resize = TRUE;
cy = wintext_max_height;
}
if (resize)
SetWindowPos(hwnd,
GetNextWindow(hwnd, GW_HWNDPREV),
0, 0,
cx, cy,
SWP_NOMOVE);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_SIZE:
return HANDLE_WM_SIZE(hwnd, wParam, lParam, onSize);
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
}