4

I'm using MFC for Visual Studio 2003. I have an Edit Control with ID IDC_COMMENT_EDIT. In the following code, after my first call to GetClientRect, I don't expect the value of rc to change.

CWnd* pWnd = GetDlgItem(IDC_COMMENT_EDIT);
if (pWnd != NULL)
{
  RECT rc;
  pWnd->GetClientRect(&rc);
  pWnd->MoveWindow(&rc, TRUE);
  pWnd->GetClientRect(&rc);
}

rc.top and rc.left are 0 all the way through, as expected. However:
After the first call to GetClientRect, I get rc.bottom == 52, and rc.right == 575.
After the second call to GetClientRect, I get rc.bottom == 48, and rc.right == 571.

Does anyone have any idea what is going on?

des4maisons
  • 1,791
  • 4
  • 20
  • 23

2 Answers2

7

Your call to MoveWindow is resizing. You need to use GetWindowRect instead of GetClientRect.

The client rect only includes the client area which is the non windows elements (such as border) of the window.

Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
  • thanks, I couldn't figure out why my text box kept shrinking! – des4maisons Dec 16 '09 at 14:59
  • No problem, common problem. That's the problem with naming a function that both moves and resizes MoveWindow! – Brian R. Bondy Dec 16 '09 at 15:38
  • A better name would be something like AdjustWindowRect – Brian R. Bondy Dec 16 '09 at 15:39
  • Or they could have provided a function to let you move your window without resizing it... resize and moving seem kind of orthogonal to me. – des4maisons Dec 18 '09 at 00:46
  • There's no problem with the name of the function, which moves the sides of the window. It's obvious that it does that because it's given a rect, not a point. The only problem is (was) the OP's use of the wrong function to get the sides of the window, and it would still be a problem if the function only changed the position, because the position of the client rect isn't the position of the window. "rc.top and rc.left are 0 all the way through" is true because the top left of the client rect is always (0,0) in client coordinates, and the OP should not have expected it. – Jim Balter Oct 22 '17 at 07:45
4

The client rect doesn't include the window borders, but MoveRect expects a rectangle that includes the borders. Use GetWindowRect instead.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622