在很多管理資訊系統的表單都有一些共同點:可以通過架構菜單開啟各種類型的視圖視窗,而這些視圖視窗具有MDI的一些特點,可以在架構表單中最小化最大化還原等,並且點擊菜單只能產生一個視窗(象SDI)。這種形式的視窗設計給人與一種條理感。於是我用VC6.0嘗試建立這種視窗,現將整個過程為大家寫下來,這裡應含有MFC的一些內幕技術。
一、建立AppWizad(exe)工程,名為mdisdi,基於MDI的文檔視模式(其餘預設)。
二、在CApp子類中添加如下代碼:
public:
CMultiDocTemplate* pDoctemp1;
CMultiDocTemplate* pDoctemp2;
三、修改InitInstance()中將如下代碼:
CMultiDocTemplate* pDocTemplate; pDocTemplate = new CMultiDocTemplate( IDR_MDISDITYPE, RUNTIME_CLASS(CMdisdiDoc), RUNTIME_CLASS(CChildFrame), // custom MDI child frame RUNTIME_CLASS(CMdisdiView)); AddDocTemplate(pDocTemplate);
改為:
//CMultiDocTemplate* pDocTemplate;//刪除 pDoctemp1 = new CMultiDocTemplate( IDR_MDISDITYPE, RUNTIME_CLASS(CMdisdiDoc), RUNTIME_CLASS(CChildFrame), // custom MDI child frame RUNTIME_CLASS(CMdisdiView)); AddDocTemplate(pDoctemp1);
四、加入新類CDoc2,基類為CDocument(利用菜單Insert-New Class…讓其自動產生即可);加入新架構類CChildFrame2,基類為CMDIChildWnd;
加入新視類,這裡新加CView2基類為CFormView類(Insert-New Form…),注意在添加對話方塊中將Document選為CDoc2;
五、同(三)將如下代碼:
CMultiDocTemplate* pNewDocTemplate = new CMultiDocTemplate( IDR_VIEW2_TMPL, RUNTIME_CLASS(CDoc2), // document class RUNTIME_CLASS(CMDIChildWnd), // frame class RUNTIME_CLASS(CView2)); // view class AddDocTemplate(pNewDocTemplate);
改為:
pDoctemp2 = new CMultiDocTemplate( IDR_VIEW2_TMPL, RUNTIME_CLASS(CDoc2), // document class RUNTIME_CLASS(CChildFrame2), // frame class RUNTIME_CLASS(CView2)); // view class AddDocTemplate(pDoctemp2);
並在mdisdi.cpp頭部添加:
#include "Doc2.h"
六、修改IDR_MAINFRAME菜單,新添一菜單“功能”,其子功能表為視窗1和視窗2.用ctrl+c複製,ctrl+v粘貼,產生二個新菜單IDR_MAINFRAME1和IDR_MAINFRAME2,再將其更名為IDR_MDISDITYPE,IDR_VIEW2_TMPL.
七、為IDR_MAINFRAME菜單新添菜單增加訊息應射函數OnMenuitem32771()和OnMenuitem32772()
八、在MainFrm.h加入:
#include "mdisdiDoc.h" #include "Doc2.h"
並在類中添加:
public: CMdisdiDoc * pDoc1; CDoc2 * pDoc2;
在MainFrm.cpp增加代碼如下:
void CMainFrame::OnMenuitem32771() { // TODO: Add your command handler code here if(pDoc1==NULL) { CMdisdiApp * pmdisdiapp =(CMdisdiApp *)AfxGetApp(); pDoc1=(CMdisdiDoc *) pmdisdiapp->pDoctemp1->OpenDocumentFile(NULL); } else { POSITION pos; pos=pDoc1->GetFirstViewPosition(); CView * pView; pView=pDoc1->GetNextView(pos); pView->GetParentFrame()->ActivateFrame(); } } void CMainFrame::OnMenuitem32772() { if(pDoc2==NULL) { CMdisdiApp * pmdisdiapp =(CMdisdiApp *)AfxGetApp(); pDoc2=(CDoc2 *) pmdisdiapp->pDoctemp2->OpenDocumentFile(NULL); } else { POSITION pos; pos=pDoc2->GetFirstViewPosition(); CView * pView; pView=pDoc2->GetNextView(pos); pView->GetParentFrame()->ActivateFrame(); } }
九、在MainFrm.cpp添加如下代碼:
CMainFrame::CMainFrame() { pDoc1=NULL; pDoc2=NULL; }
在ChildFrm.cpp中添加:
#include "MainFrm.h"
和如下代碼:
CChildFrame::~CChildFrame() { CMainFrame * pmainwnd= (CMainFrame *)AfxGetMainWnd(); pmainwnd->pDoc1=NULL; }
在ChildFrame2.cpp中添加:
#include "MainFrm.h"
和如下代碼:
CChildFrame2::~CChildFrame2() { CMainFrame * pmainwnd= (CMainFrame *)AfxGetMainWnd(); pmainwnd->pDoc2=NULL; }
最後將InitInstance()中如下幾行注釋掉:
CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line if (!ProcessShellCommand(cmdInfo)) return FALSE;
作用是去掉建立選擇視類對話方塊;
代碼:http://www.vczx.com/article/file/20041011111218_vczx_mdisdi.rar