控制元件大小隨視窗變化的方法(MFC)
形式1:用於在單文件視窗中,或者是手動建立的listctrl
void CTrade_MISView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
if (m_ListCtrl) //m_ListCtrl是listctrl的例項物件
{
CRect rect;
GetClientRect(rect); //直接獲取使用者區rect
rect.top += 30; //這裡是設定距離頂部30畫素
m_ListCtrl.MoveWindow(rect);
}
}
形式2:用於在對話方塊視窗中
void CDlglistDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
CWnd *pList=GetDlgItem(IDC_LIST1);
if(pList)
{
CRect rect;
GetWindowRect(&rect); //獲取視窗rect,
ScreenToClient(rect); //從視窗尺寸轉換到使用者區rect
pList->MoveWindow(&rect,true);
}
}
形式3:也是用於單文件的,或者是自己手動建立的listctrl,不過這種方法還有待考證
void ClistcontrolView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: 在此處新增訊息處理程式程式碼
if(m_ListCtrl) //m_ListCtrl是listctrl的例項物件
{
m_ListCtrl.MoveWindow(0,30,cx,cy); //重置LIST大小
}
}