Reputation: 420
I need to provide a client area within a window of a suitable size for rendering from a video camera. My problem is that when using AdjustWindowRect() and SetWindowPos() with what I believe to be the correct arguments I'm still left with a window that's slightly too small.
Take the code below:
//nWidth = 640, nHeight = 480.
RECT rcClient = { 0, 0, nWidth, nHeight };
AdjustWindowRect(&rcClient, WS_CAPTION | WS_SYSMENU | WS_THICKFRAME, FALSE);
//rcClient now equals 0, 0, 648, 488. Which doesn't sound much bigger to me given there's a caption and frame.
SetWindowPos(hCamWnd, 0, 0, 0, rcClient.right, rcClient.bottom, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
GetClientRect(hCamWnd, &rcClient);
//rcClient now equals 0, 0, 632, 450. Not the 640 x 480 I'm after. Why???
The window creation code (it's created before I know the camera dimensions so is resized later). Obviously some code here from elsewhere, but I'm guessing it's just the style that's likely to be relevant.
hCamWnd = CreateWindow(
wc.lpszClassName,
_T("Inspection Camera"),
WS_CAPTION | WS_SYSMENU | WS_THICKFRAME,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
appGlobal::AppFrame().GetSafeHwnd(),
NULL,
GetModuleHandle(NULL),
NULL
);
The code rendering the image to screen is practically unchanged from the MSDN example at https://msdn.microsoft.com/en-us/library/windows/desktop/dd743690%28v=vs.85%29.aspx, which luckily resizes and letter boxes within the client area to maintain aspect ratio, so all I'm left with is a bit of an unwanted border, but it'd be nice to get the client area right ...
I've tried a few different window styles but it doesn't seem to change anything.
Cheers for any help.
Upvotes: 2
Views: 892
Reputation: 37122
If the window already exists you can use use the difference between GetWindowRect
and GetClientRect
to calculate the size of the non-client area, and then work backwards from that:
// get size of window and the client area
RECT rc, rcClient;
GetWindowRect(hCamWnd, &rc);
GetClientRect(hCamWnd, &rcClient);
// calculate size of non-client area
int xExtra = rc.right - rc.left - rcClient.right;
int yExtra = rc.bottom - rc.top - rcClient.bottom;
// now resize based on desired client size
SetWindowPos(hCamWnd, 0, 0, 0, nWidth + xExtra, nHeight + yExtra, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
Upvotes: 3