海阔天空

海阔凭鱼跃 天高任鸟飞

 

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

程序源代码   

效果,如图:


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();
}

posted on 2010-04-01 12:59  liuym  阅读(3093)  评论(2编辑  收藏  举报

导航