alex555
alex555

Reputation: 1786

How to move window in MFC?

I have a group box where I placed a CListCtrl with a custom height

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); });      

Now I want to place a picture control below the CListCtrl, but all the functions with CRect confuse me. All them place the control somewhere but not where I want.

//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);

Can somebody help me with this crazy mfc stuff?

Upvotes: 2

Views: 6557

Answers (1)

ScottMcP-MVP
ScottMcP-MVP

Reputation: 10425

The left and top members returned by GetClientRect are always zero. So calling m_FeatureList.GetClientRect tells you nothing about where the control is located. You have to call m_FeatureList.GetWindowRect and then convert the result to get its position relative to the parent dialog.

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

Upvotes: 6

Related Questions