最簡單的 SDI 專案,在主視圖 CXindowView 中動態建立兩個子視圖:左側 CXindowForm (CFormView) 顯示資訊面板,右側 CXindowList (CListView) 顯示列表資料。
int CXindowView::OnCreate(LPCREATESTRUCT lpCreateStruct) {
if (CView::OnCreate(lpCreateStruct) == -1) return -1;
m_pForm = (CXindowForm*)okCreateView(RUNTIME_CLASS(CXindowForm), 1001);
m_pList = (CXindowList*)okCreateView(RUNTIME_CLASS(CXindowList), 1002);
m_pForm->m_pParent = this; // 回指父視圖以便通知
return 0;
}
void CXindowView::OnSize(UINT nType, int cx, int cy) {
CView::OnSize(nType, cx, cy);
const int nFormWidth = 200;
if (cx > 400) { // 寬度足夠顯示雙欄
m_pForm->ShowWindow(SW_SHOW);
m_pForm->MoveWindow(0, 0, nFormWidth, cy);
m_pList->ShowWindow(SW_SHOW);
m_pList->MoveWindow(nFormWidth, 0, cx-nFormWidth, cy);
} else { // 窄視窗隱藏左側面板
m_pForm->ShowWindow(SW_HIDE);
m_pList->ShowWindow(SW_SHOW);
m_pList->MoveWindow(0, 0, cx, cy);
}
}
void CXindowForm::OnInitialUpdate() {
CFormView::OnInitialUpdate();
m_add.SetCommand(1); // 識別標籤 ID
}
BOOL CXindowForm::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) {
LPNMHDR pnmh = (LPNMHDR)lParam;
if (pnmh->code == NM_LINKCLICK) {
CXLabel* pLabel = (CXLabel*)GetDlgItem(pnmh->idFrom);
CString str; str.Format(_T("%d"), pLabel->GetCommand());
AfxMessageBox(str); // 示範用
if (m_pParent->GetSafeHwnd()) {
CListCtrl& list = ((CXindowView*)m_pParent)->m_pList->GetListCtrl();
int n = list.GetItemCount();
int idx = list.InsertItem(n, _T("2003-8-15"));
list.SetItemText(idx, 1, _T("192.168.3.1"));
list.SetItemText(idx, 2, _T("www.vckbase.com"));
list.SetItemText(idx, 3, _T("程式設計"));
}
}
return CFormView::OnNotify(wParam, lParam, pResult);
}
CXLabel 衍生自 CLabel,擴充 SetCommand/GetCommand 作為識別碼。
CHtmlView 載入真正 Web 內容,支援 HTML/CSS/JSCSplitterWnd) 允許使用者調整欄寬以 SDI 主視圖動態建立雙 CFormView/CListView 子視圖,配合 OnSize 動態版面與 NM_LINKCLICK 跨視圖互動,快速構建類資源管理器雙欄介面。程式碼簡潔,易於擴充為完整檔案管理或資訊瀏覽應用。