tags:

views:

324

answers:

2

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?

+2  A: 
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.

Mark Ransom
+1  A: 

In the VC6 resource editor you can select the control and do Ctrl+F9 for vertical centering and Ctrl+Shift+F9 for horizontal centering.

Programatically you can use MoveWindow Win32 API to position the control wherever you want.

Canopus