创建没有Document的MFC MDI应用程序

本文介绍如何在MFC中创建不包含文档的MDI应用程序,通过自定义视图类展示树形和列表数据,同时提供菜单响应及窗口激活的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建没有Document的MFC MDI应用程序

from:https://www.cnblogs.com/zkliuym/archive/2010/04/01/1702168.html

相关博文:
· MFC中创建没有子窗口的MDI应用程序
· MFC中创建没有子窗口的MDI应用程序
· mfc创建一个没有文档类的视图程序
· MDI 窗口的创建
· VTK + MFC Single Document

程序源代码   

效果,如图:

1)

创建一个MFC MDI应用程序,Wizard设置如图,

然后点Finish

 

2)

CMyTreeView 

在CMyTreeView中新增三个函数,显示些示例数据

    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);//本例没有使用该函数

    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);

    virtual void OnInitialUpdate();

源代码如下:

int CMyTreeView::OnCreate(LPCREATESTRUCT lpCreateStruct)

{

    if (CTreeView::OnCreate(lpCreateStruct) == -1)

        return -1;

 

    CTreeCtrl& m_treeCtrl = GetTreeCtrl();

 

HTREEITEM hItem;

hItem = m_treeCtrl.InsertItem("node 1", TVI_ROOT);

            m_treeCtrl.Expand(hItem, TVE_EXPAND);

 

    hItem = m_treeCtrl.InsertItem("node 2", TVI_ROOT);

            m_treeCtrl.Expand(hItem, TVE_EXPAND);

 

    hItem = m_treeCtrl.InsertItem("node 3", hItem);

            m_treeCtrl.Expand(hItem, TVE_EXPAND);

 

    return 0;

}

 

void CMyTreeView::OnInitialUpdate()

{

    CTreeView::OnInitialUpdate();

    CTreeCtrl& m_treeCtrl = GetTreeCtrl();

m_treeCtrl.ModifyStyle(0, WS_VISIBLE | WS_TABSTOP | TVS_HASBUTTONS | TVS_LINESATROOT | TVS_HASLINES | TVS_DISABLEDRAGDROP);

}

 

CMyListView

在CMyListView中新增三个函数,显示些示例数据

    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);//本例没有使用该函数

    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);

    virtual void OnInitialUpdate();

源代码如下:

 

int CMyListView::OnCreate(LPCREATESTRUCT lpCreateStruct)

{

    if (CListView::OnCreate(lpCreateStruct) == -1)

        return -1;

 

    // TODO:  Add your specialized creation code here

    CListCtrl& m_list = GetListCtrl();

 

    CString sTmp = "asdf";//示例数据,代码中最好封装成函数

    int iPos = m_list.GetItemCount();

    m_list.InsertItem(iPos, sTmp);

    m_list.SetItemText(iPos, 1, sTmp+sTmp);

 

    return 0;

}

 

void CMyListView::OnInitialUpdate()

{

    CListView::OnInitialUpdate();

 

    CListCtrl& listCtrl = GetListCtrl();

 

    LONG lStyle;

    lStyle = GetWindowLong(listCtrl.m_hWnd, GWL_STYLE);   

    lStyle &= ~LVS_TYPEMASK;                            

    lStyle |= LVS_REPORT ;          

    SetWindowLong(listCtrl.m_hWnd, GWL_STYLE, lStyle);    

 

    DWORD dwStyle = listCtrl.GetExtendedStyle();

    dwStyle |= LVS_EX_FULLROWSELECT | TVS_SHOWSELALWAYS | LVS_EX_FULLROWSELECT;

    listCtrl.SetExtendedStyle(dwStyle);

 

    CRect rect;

    GetClientRect(&rect);

 

    listCtrl.InsertColumn( 0, "信息",     LVCFMT_CENTER,  rect.Width()/2 );

    listCtrl.InsertColumn( 1, "信息2",     LVCFMT_CENTER,  rect.Width()/2 );

}

 

CFrmChildTree

FrmChildTree.h

 

class CFrmChildTree : public CMDIChildWnd

 

#include "MyTreeView.h"

声明一个CMyTreeView数据成员

    CMyTreeView m_FrmChildTree;

 

FrmChildTree.cpp

int CFrmChildTree::OnCreate(LPCREATESTRUCT lpCreateStruct)

{

    if (CMDIChildWnd::OnCreate(lpCreateStruct) == -1)

        return -1;

 

    // TODO:  Add your specialized creation code here

    if (!m_FrmChildTree.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW, 

CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL))

{

TRACE0("Failed to create view window\n");

return -1;

}

 

    return 0;

}

 

CFrmChildList

 

FrmChildList.h

 

class CFrmChildList : public CMDIChildWnd(with Splitter)

 

#include "MyListView.h"

最好声明一个MyListView数据成员,用于记录Spliter创建的View,以便以后使用

CMyListView* m_pMyListView;

 

FrmChildList.cpp

BOOL CFrmChildList::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)

{

//return m_wndSplitter.Create(this,

// 2, 2,       // TODO: adjust the number of rows, columns

// CSize(10, 10),  // TODO: adjust the minimum pane size

// pContext);

    // TODO: Add your specialized code here and/or call the base class

CRect rect;

GetClientRect(&rect);

if (!m_wndSplitter.CreateStatic(this, 2, 1))

{

return FALSE;

}

m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CMyListView),CSize(rect.Width(),rect.Height()/5  ),pContext);

    m_wndSplitter.CreateView(1, 0, RUNTIME_CLASS(CMyListView),CSize(rect.Width(),rect.Height()/5  ),pContext);

    m_pMyListView = (CMyListView*)m_wndSplitter.GetPane(0, 0);  //... ...

 

return CMDIChildWnd::OnCreateClient(lpcs, pContext);

}

 

3)

修改menu:IDR_MFC_MDI_BUT_NO_TYPE

增加:ID_FILE_TREE ID_FILE_LIST 两个菜单,分别为Tree 和 List

在app中响应

 

app.h

 

#include "FrmChildList.h"

#include "FrmChildTree.h"

 

    CMDIChildWnd* m_pFrmChildList; //两个CMDIChildWnd对象

CMDIChildWnd* m_pFrmChildTree;

 

    afx_msg void OnFileTree();

    afx_msg void OnFileList();

 

private:

void ActiveChildWnd(CMDIChildWnd* m_pMDIChildWnd);

app.cpp

 

BOOL CMFC_MDI_BUT_NO_DOCApp::InitInstance()

{

... ...

    if( NULL == m_pFrmChildList )

{

m_pFrmChildList =pFrame->CreateNewChild( RUNTIME_CLASS(CFrmChildList), IDR_MFC_MDI_BUT_NO_TYPE, 0, 0);

}

    ... ...

}

 

void CMFC_MDI_BUT_NO_DOCApp::OnFileNew() //屏蔽该代码,删除对应的New菜单

{

//CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);

//// create a new MDI child window

//pFrame->CreateNewChild(

//RUNTIME_CLASS(CChildFrame), IDR_MFC_MDI_BUT_NO_TYPE, m_hMDIMenu, m_hMDIAccel);

}

 

void CMFC_MDI_BUT_NO_DOCApp::OnFileTree()

{

    // TODO: Add your command handler code here

    CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);

if( NULL == m_pFrmChildTree )

{

m_pFrmChildTree =pFrame->CreateNewChild( RUNTIME_CLASS(CFrmChildTree), IDR_MFC_MDI_BUT_NO_TYPE, 0, 0);

}

else

{

ActiveChildWnd( m_pFrmChildTree );

}

m_pFrmChildTree->SetWindowTextA("Tree");    

}

 

void CMFC_MDI_BUT_NO_DOCApp::OnFileList()

{

    // TODO: Add your command handler code here]

    CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);

if( NULL == m_pFrmChildList )

{

m_pFrmChildList =pFrame->CreateNewChild( RUNTIME_CLASS(CFrmChildList), IDR_MFC_MDI_BUT_NO_TYPE, 0, 0);

}

else

{

ActiveChildWnd( m_pFrmChildList );

}

m_pFrmChildList->SetWindowTextA("List");

}

 

void CMFC_MDI_BUT_NO_DOCApp::ActiveChildWnd(CMDIChildWnd* m_pMDIChildWnd)

{

m_pMDIChildWnd->MDIActivate();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值