Visual c++ 如何将控件移动到MFC窗体的中间?

Visual c++ 如何将控件移动到MFC窗体的中间?,visual-c++,mfc,Visual C++,Mfc,在VC++6.0 MFC项目中,如何将控件移动到窗体中间 假设我添加了一个EditBox(m_editcontrol),如何将该控件移动到表单的中间 CRect rectParent; m_editcontrol.GetParent()->GetClientRect(rectParent); CRect rectControl; m_editcontrol.GetWindowRect(rectControl); m_editcontrol.MoveWindow((rectParent.Wi

在VC++6.0 MFC项目中,如何将控件移动到窗体中间

假设我添加了一个EditBox(m_editcontrol),如何将该控件移动到表单的中间

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());
控件的位置在父窗口的客户端区域内,因此首先需要获得父窗口的宽度和高度。然后,我们得到控件的宽度和高度。父窗口的中间是宽度和高度之差除以2。MoveWindow用于将窗口移动到所需位置


控件的位置在父窗口的客户端区域内,因此首先需要获得父窗口的宽度和高度。然后,我们得到控件的宽度和高度。父窗口的中间是宽度和高度之差除以2。MoveWindow用于将窗口移动到所需位置。

在VC6资源编辑器中,您可以选择该控件,并执行Ctrl+F9进行垂直居中,执行Ctrl+Shift+F9进行水平居中


通过编程,您可以使用MoveWindow Win32 API将控件定位到任何需要的位置。

在VC6资源编辑器中,您可以选择该控件,并按Ctrl+F9进行垂直居中,按Ctrl+Shift+F9进行水平居中

CRect rectParent;
m_pParentWnd->GetClientRect(rectParent);
m_pParentWnd->ClientToScreen(rectParent);
CRect rectControl;
GetWindowRect(rectControl);
MoveWindow(rectParent.left + (rectParent.Width()-rectControl.Width())/2, rectParent.top + (rectParent.Height()-rectControl.Height())/2,
    rectControl.Width(), rectControl.Height());
通过编程,您可以使用MoveWindow Win32 API将控件定位到您想要的任何位置

CRect rectParent;
m_pParentWnd->GetClientRect(rectParent);
m_pParentWnd->ClientToScreen(rectParent);
CRect rectControl;
GetWindowRect(rectControl);
MoveWindow(rectParent.left + (rectParent.Width()-rectControl.Width())/2, rectParent.top + (rectParent.Height()-rectControl.Height())/2,
    rectControl.Width(), rectControl.Height());