如何在MFC中移动窗口?

时间:2013-11-12 14:07:36

标签: c++ mfc

我有一个分组框,我在其中放置了一个CListCtrl的自定义高度

m_FeatureList.GetClientRect(&rect);
nColInterval = rect.Width()/2;

m_FeatureList.InsertColumn(0, _T("ID"), LVCFMT_LEFT, nColInterval);
m_FeatureList.InsertColumn(1, _T("Class"), LVCFMT_RIGHT, nColInterval);
m_FeatureList.ModifyStyle( LVS_OWNERDRAWFIXED, 0, 0 );
m_FeatureList.SetExtendedStyle(m_CoilList.GetExtendedStyle() | LVS_EX_GRIDLINES);
...
int a, b;
m_FeatureList.GetItemSpacing(true, &a, &b);

// data is a vector containing item text
m_FeatureList.MoveWindow(listRect.left, listRect.top, listRect.Width(), b*data.size()+4);

int i = 0;
std::for_each(data.begin(), data.end(), [&](CString& p) { AddDefectListItem(i++,p); });      

现在我想在CListCtrl下方放置一个图片控件,但CRect的所有功能都让我感到困惑。他们把控制放在某个地方,但不是我想要的地方。

//m_FeatureList.GetClientRect(&listRect);
//m_FeatureList.ClientToScreen(&listRect);
m_FeatureList.ScreenToClient(&listRect);

// Oh my god, which coordinates do I need???
m_image.MoveWindow(listRect.left, listRect.bottom+3,listRect.Width(), 20);

有人可以帮我解决这个疯狂的mfc问题吗?

1 个答案:

答案 0 :(得分:5)

GetClientRect返回的左侧和顶层成员始终为零。因此,调用m_FeatureList.GetClientRect不会告诉您控件的位置。您必须调用m_FeatureList.GetWindowRect,然后转换结果以获取其相对于父对话框的位置。

CRect listRect;
m_FeatureList.GetWindowRect(&listRect);
ScreenToClient(&listRect);
listRect.top = listRect.bottom +3;
listRect.bottom = listRect.top + 20;
m_image.MoveWindow(&listRect);