MFC用List Control 和Picture控件实现界面切换效果
添加List Control 和Picture控件
添加 3个子窗体
把子窗体边框设置为None, 样式设为Child
声明
CListCtrl m_listPageForm;
void ShowForm(int nIndex);
void CreatFormList();
void CMFCApplication3Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST_PAGE, m_listPageForm);
}
List控件和子窗体初始化
void CMFCApplication3Dlg::CreatFormList()
{
CImageList imgList;
imgList.Create(16, 16, ILC_COLOR8 | ILC_MASK, 0, 0);
m_listPageForm.SetImageList(&imgList, LVSIL_SMALL);
imgList.Add(::AfxGetApp()->LoadIcon(IDR_MAINFRAME));
imgList.Detach();
CRect rect;
GetDlgItem(IDC_STATIC_RECT)->GetWindowRect(rect);
rect.DeflateRect(1, 1);
ScreenToClient(&rect);
page1* pHang = new page1;
pHang->Create(IDD_DIALOG1, this);
pHang->MoveWindow(rect);
pHang->ShowWindow(SW_SHOW);
m_listPageForm.InsertItem(0, TEXT("界面1"), 0);
m_listPageForm.SetItemData(0, (DWORD_PTR)pHang);
page2* pSkill = new page2;
pSkill->Create(IDD_DIALOG2, this);
pSkill->MoveWindow(rect);
m_listPageForm.InsertItem(1, TEXT("界面2"), 0);
m_listPageForm.SetItemData(1, (DWORD_PTR)pSkill);
page3* pbag = new page3;
pbag->Create(IDD_DIALOG3, this);
pbag->MoveWindow(rect);
m_listPageForm.InsertItem(2, TEXT("界面3"), 0);
m_listPageForm.SetItemData(2, (DWORD_PTR)pbag);
}
BOOL CMFCApplication3Dlg::OnInitDialog()
{
...
...
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
CreatFormList();
return TRUE;
}
List控件的左键点击
void CMFCApplication3Dlg::OnNMClickListPage(NMHDR* pNMHDR, LRESULT* pResult)
{
LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
if (-1 == pNMItemActivate->iItem) return;
ShowForm(pNMItemActivate->iItem);
*pResult = 0;
}
void CMFCApplication3Dlg::ShowForm(int nIndex)
{
CDialog* pForm = NULL;
for (int i = 0; i < m_listPageForm.GetItemCount(); ++i)
{
pForm = (CDialog*)(DWORD_PTR)m_listPageForm.GetItemData(i);//获取所有的窗口页面
if (pForm) pForm->ShowWindow(SW_HIDE);//如果pform为空,都隐藏掉
pForm = (CDialog*)(DWORD_PTR)m_listPageForm.GetItemData(nIndex);//根据索引把选中的要显示的窗口拿出来
if (pForm) pForm->ShowWindow(SW_SHOW);//如果pform为空,显示选中的窗口
}
}