VC/MFC 在對話方塊中建立檢視並在檢視區新增ListCtrl控制元件
自定義的view類用於顯示在對話方塊上
class CGroupMenuView : public CScrollView
{
protected:
CGroupMenuView();
………………
…………
};
class CWorkView : public CScrollView
{
protected:
CWorkView();
…………
…………
};
//建立左檢視
{
CCreateContext cx;
cx.m_pNewViewClass = RUNTIME_CLASS( CGroupMenuView ) ;
cx.m_pCurrentDoc = NULL;
CFrameWnd* pfWnd = (CFrameWnd*)this;
m_pLeftView = (CGroupMenuView*)pfWnd->CreateView( &cx );
m_pLeftView->SetScrollSizes( MM_TEXT, CSize(100, 100) );//當你繼承的基類是滾動檢視類時這句很重要,需要設定視的大小
m_pLeftView->SetParent( this );
m_pLeftView->InitView();
}
//建立右檢視
{
CCreateContext cx;
cx.m_pNewViewClass = RUNTIME_CLASS( CWorkView ) ;
cx.m_pCurrentDoc = NULL;
CFrameWnd* pfWnd = (CFrameWnd*)this;
m_pRightView = (CWorkView*)pfWnd->CreateView( &cx );
m_pRightView->SetScrollSizes( MM_TEXT, CSize(100, 100) );//當你繼承的基類是滾動檢視類時這句很重要,需要設定視的大小
m_pRightView->SetParent( this );
}
在檢視區新增ListCtrl控制元件
void CListzzzView::OnInitialUpdate()
{
CListView::OnInitialUpdate();
CListCtrl& ctl=GetListCtrl();
// TODO: Add your specialized code here and/or call the base class
//設定列表控制元件風格
DWORD dwStyle=::GetWindowLong(m_hWnd,GWL_STYLE);
dwStyle|=LVS_REPORT|LVS_SHOWSELALWAYS|LVS_EDITLABELS;
::SetWindowLong(m_hWnd,GWL_STYLE,dwStyle);
dwStyle=ctl.GetExtendedStyle();
dwStyle|= LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT;
//設定擴充套件風格
ctl.SetExtendedStyle(dwStyle);
//列表控制元件頭內容
TCHAR tittle[3][10] = {_T("姓名"),_T("單位"),_T("地址")};
LV_COLUMN lvcolumn;
CRect rect;
GetWindowRect(&rect);
//填充列表控制元件頭
for(int i=0;i<3;i++)
{
lvcolumn.mask =LVCF_FMT|LVCF_SUBITEM|LVCF_TEXT
|LVCF_WIDTH|LVCF_ORDER;
lvcolumn.fmt = LVCFMT_LEFT;
lvcolumn.pszText = tittle[i];
lvcolumn.iSubItem = i;
lvcolumn.iOrder = i;
lvcolumn.cx = rect.Height()/3;
GetListCtrl().InsertColumn(i, &lvcolumn);
}
}