How do I move commands in the middle of an MFC form?

advertisements

How to move controls to the middle of the form in a VC++ 6.0 MFC project?

Say I added an EditBox(m_editcontrol), how to move this control to the middle of the form?


CRect rectParent;
m_editcontrol.GetParent()->GetClientRect(rectParent);
CRect rectControl;
m_editcontrol.GetWindowRect(rectControl);
m_editcontrol.MoveWindow((rectParent.Width()-rectControl.Width())/2, (rectParent.Height()-rectControl.Height())/2, rectControl.Width(), rectControl.Height());

The position of a control is within the client area of the parent, so first we need to get the width and height of the parent window. Then, we get the width and height of the control. The middle of the parent window is the difference of the widths and heights, divided by 2. MoveWindow is used to move a window to the desired position.