动态更改资源ID的方法
其实说是动态的,还是得预先定义两个FormView类。
大概说一下步骤:
1。新建一个MDI应用程序,基于FormView类的
2。增加一个FormView类,增加一个对话框资源,设定Style为Child,Border为None,右键类向导指向已存在的类CFormView2。
3。在IDR_XXXTYPE菜单的查看项中增加两个菜单项“FormView 1”以及“FormView 2”,ID分别为ID_VIEW_FORMVIEW1和ID_VIEW_FORMVIEW2。
4。在MainFrame类中增加一个Protected或者Public变量CMultiDocTemplate* pMDT。
5。在MainFrame类中为新增菜单项添加On_Command_Range事件。
主要代码如下:
/***************** MainFrm.H ********************/
class CMainFrame : public CMDIFrameWnd
{
DECLARE_DYNAMIC(CMainFrame)
public:
CMainFrame();
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMainFrame)
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
//}}AFX_VIRTUAL
// Implementation
public:
CMultiDocTemplate* pMDT;
virtual ~CMainFrame();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected: // control bar embedded members
CStatusBar m_wndStatusBar;
CToolBar m_wndToolBar;
// Generated message map functions
protected:
//{{AFX_MSG(CMainFrame)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnFormviewSwitch(UINT nCmdID);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/******************* MainFrm.CPP *********************/
IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_COMMAND_RANGE(ID_VIEW_FORMVIEW1, ID_VIEW_FORMVIEW2, OnFormviewSwitch)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// CMainFrame message handlers
extern CRuntimeClass* const pClass1 = RUNTIME_CLASS(CFormviewDoc);
extern CRuntimeClass* const pClass2 = RUNTIME_CLASS(CFormviewDoc);
extern CRuntimeClass* const pFormViewClass1 = RUNTIME_CLASS(CFormviewView);
extern CRuntimeClass* const pFormViewClass2 = RUNTIME_CLASS(CFormView2);
void CMainFrame::OnFormviewSwitch(UINT nCmdID)
{
switch(nCmdID) {
case ID_VIEW_FORMVIEW1:
//pMDT = new CMultiDocTemplate(IDD_FORMVIEW_FORM, pClass1, RUNTIME_CLASS(CMDIChildWnd), pFormViewClass1);
pMDT = new CMultiDocTemplate(IDD_FORMVIEW_FORM, RUNTIME_CLASS(CFormviewDoc), RUNTIME_CLASS(CMDIChildWnd), RUNTIME_CLASS(CFormviewView));
pMDT->OpenDocumentFile(NULL);
break;
case ID_VIEW_FORMVIEW2:
//pMDT = new CMultiDocTemplate(IDD_DIALOG_NEW, pClass2, RUNTIME_CLASS(CMDIChildWnd), pFormViewClass2);
pMDT = new CMultiDocTemplate(IDD_DIALOG_NEW, RUNTIME_CLASS(CFormviewDoc), RUNTIME_CLASS(CMDIChildWnd), RUNTIME_CLASS(CFormView2));
pMDT->OpenDocumentFile(NULL);
break;
default:;
}
}
本文地址:http://www.45fan.com/a/question/70263.html