现在的位置: 首页 > 综合 > 正文

一个可以切换视图的splitter window类

2012年02月10日 ⁄ 综合 ⁄ 共 5823字 ⁄ 字号 评论关闭
说明如题。

使用示例:

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
    m_wndSplitter.CreateStatic(this, 1, 2);
    m_wndSplitter.AddView(RUNTIME_CLASS(CViewSel), 0, CRect(0, 0, 100, 100), 1, pContext);
    m_wndSplitter.AddView(RUNTIME_CLASS(CViewFtp), 0, CRect(0, 0, 1, 1), 2, pContext);
    m_wndSplitter.AddView(RUNTIME_CLASS(CViewHttp), 0, CRect(0, 0, 1, 1), 3, pContext);
    m_wndSplitter.AddView(RUNTIME_CLASS(CViewSocket), 0, CRect(0, 0, 1, 1), 4, pContext);
    m_wndSplitter.AddView(RUNTIME_CLASS(CSocSampleView), 0, CRect(0, 0, 1, 1), 5, pContext);
    m_wndSplitter.SwitchView(1, 0, 0, true);
    m_wndSplitter.SwitchView(4, 0, 1, true);
    m_wndSplitter.SetColumnInfo(0, 200, 0);
    m_wndSplitter.RecalcLayout();

    return TRUE;
}

void CMainFrame::OnPromptinfo()
{
    m_wndSplitter.SwitchView(5, 0, 1);
    m_wndSplitter.RecalcLayout();
}
 
源代码:

/////////////////////////////////////////////////////////////////////////////
//
//    description:
//        This is a switchable splitter window class. Use AddView to add switching
//        views(also can be windows), SwitchView to change views that added with AddView
//        in a pane, GetView to get added views.
//    date:
//        06-12-14
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_SPLITTERSWITCHWND_H__72831108_941D_4622_92C7_9032A12FF7E6__INCLUDED_)
#define AFX_SPLITTERSWITCHWND_H__72831108_941D_4622_92C7_9032A12FF7E6__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// SplitterSwitchWnd.h : header file
//

#include <map>
#include <vector>

/////////////////////////////////////////////////////////////////////////////
// CSplitterSwitchWnd window

class CSplitterSwitchWnd : public CSplitterWnd
{
public:

    //add switchable view
    int AddView(CRuntimeClass * pView, DWORD dwStyle, const RECT& rect,
        UINT nID, CCreateContext* pContext = NULL);
    //switch view
    int SwitchView(UINT nID, int nRow, int nCol, bool bFirst = false);
    //get view pointer
    CWnd* GetView(UINT nID);

    virtual BOOL CreateStatic(CWnd* pParentWnd,
        int nRows, int nCols, DWORD dwStyle = WS_CHILD | WS_VISIBLE,
        UINT nID = AFX_IDW_PANE_FIRST)
    {
        std::vector<int> vtRow = std::vector<int>(nCols, 0);
        m_vtID.assign(nRows, vtRow);
        return CSplitterWnd::CreateStatic(pParentWnd, nRows,
            nCols, dwStyle, nID);
    };

protected:

    //ID of each pane
    std::vector< std::vector<int> > m_vtID;
    std::map<UINT, CWnd*> m_mapViews;

// Construction
public:
    CSplitterSwitchWnd();

// Attributes
public:

// Operations
public:

// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CSplitterSwitchWnd)
    //}}AFX_VIRTUAL

// Implementation
public:
    virtual ~CSplitterSwitchWnd();

    // Generated message map functions
protected:
    //{{AFX_MSG(CSplitterSwitchWnd)
        // NOTE - the ClassWizard will add and remove member functions here.
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_SPLITTERSWITCHWND_H__72831108_941D_4622_92C7_9032A12FF7E6__INCLUDED_)

// SplitterSwitchWnd.cpp : implementation file
//

#include "stdafx.h"
#include "SplitterSwitchWnd.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

using namespace std;

/////////////////////////////////////////////////////////////////////////////
// CSplitterSwitchWnd

CSplitterSwitchWnd::CSplitterSwitchWnd()
{
}

CSplitterSwitchWnd::~CSplitterSwitchWnd()
{
}

BEGIN_MESSAGE_MAP(CSplitterSwitchWnd, CSplitterWnd)
    //{{AFX_MSG_MAP(CSplitterSwitchWnd)
        // NOTE - the ClassWizard will add and remove mapping macros here.
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSplitterSwitchWnd message handlers

/////////////////////////////////////////////////////////////////////////////
//    description:
//        add switchable view
//    param:
//        pView        view class, can be CWnd
//        dwStyle        view style
//        rect        region of view
//        nID            window ID
//        pContext    context
//    return:
//        0            success
/////////////////////////////////////////////////////////////////////////////
int CSplitterSwitchWnd::AddView(CRuntimeClass * pView, DWORD dwStyle, const RECT& rect,
    UINT nID, CCreateContext* pContext)
{
    CWnd *pViewInst;

    pViewInst = (CWnd*)pView->CreateObject();
    if (pViewInst == NULL)
    {
        return 2;
    }

    dwStyle |= WS_CHILD;
    if (!pViewInst->Create(NULL, NULL, dwStyle, rect, this, nID, pContext))
    {
        return 1;
    }

    m_mapViews.insert(pair<UINT, CWnd*>(nID, pViewInst));

    return 0;
}

/////////////////////////////////////////////////////////////////////////////
//    description:
//        switch view of pane in nRow and nCol
//    param:
//        nID            view ID
//        nRow        pane row
//        nCol        pane column
//        bFirst        the pane is the first time of switching views
//    return:
//        0            success
/////////////////////////////////////////////////////////////////////////////
int CSplitterSwitchWnd::SwitchView(UINT nID, int nRow, int nCol, bool bFirst)
{
    map<UINT, CWnd*>::iterator iteView = m_mapViews.find(nID);
    if (iteView == m_mapViews.end())
    {
        return 2;
    }

    if (!bFirst)
    {
        CWnd *pViewOld = GetPane(nRow, nCol);

        if (pViewOld != NULL)
        {
            pViewOld->ShowWindow(SW_HIDE);
            pViewOld->SetDlgCtrlID(m_vtID[nRow][nCol]);
        }
    }

    m_vtID[nRow][nCol] = iteView->first;
    iteView->second->SetDlgCtrlID(IdFromRowCol(nRow, nCol));
    iteView->second->ShowWindow(SW_SHOW);

    return 0;
}

/////////////////////////////////////////////////////////////////////////////
//    description:
//        get view pointer
//    param:
//        nID            view ID
//    return:
//                    view pointer
/////////////////////////////////////////////////////////////////////////////
CWnd* CSplitterSwitchWnd::GetView(UINT nID)
{
    map<UINT, CWnd*>::iterator iteView = m_mapViews.find(nID);
    if (iteView == m_mapViews.end())
    {
        return NULL;
    }
    else
    {
        return iteView->second;
    }
}

抱歉!评论已关闭.