我需要将CView派生类放入CDockablePane中。某处是否有任何代码示例,或者有人可以提供这样的代码?

我试过的

显然应该很简单,在网上我发现了一些建议,例如“仅创建 View 并将其父级设置为对话框或可停靠 Pane 或所需的窗口类型”。但是由于某种原因,它不起作用,也许是因为它需要CFrameWnd,我不知道。

无论如何,我需要能够执行此操作而无需创建另一个文档模板类。只是为了使用预先存在的文档和 View 类。

最佳答案

这是一个例子:

从CDockablePane派生的类:

// CRichEditPane .h

class CRichEditPane : public CDockablePane
{
    DECLARE_DYNAMIC(CRichEditPane)

public:
    CRichEditPane();
    virtual ~CRichEditPane();

protected:
    void AdjustLayout();
protected:
    DECLARE_MESSAGE_MAP()
public:
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg void OnSize(UINT nType, int cx, int cy);
};

// CRichEditPane .cpp
IMPLEMENT_DYNAMIC(CRichEditPane, CDockablePane)

CRichEditPane::CRichEditPane()
{

}

CRichEditPane::~CRichEditPane()
{
}


BEGIN_MESSAGE_MAP(CRichEditPane, CDockablePane)
    ON_WM_CREATE()
    ON_WM_SIZE()
END_MESSAGE_MAP()


// CRichEditPane message handlers


int CRichEditPane::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CDockablePane::OnCreate(lpCreateStruct) == -1)
        return -1;

    CRuntimeClass *pClass = RUNTIME_CLASS(CRichEditViewInPane);

    // calling constructor using IMPLEMENT_DYNCREATE macro
    CRichEditViewInPane *pView = (CRichEditViewInPane*)pClass->CreateObject();


    if (!pView->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW, CRect(0,0,0,0), this, AFX_IDW_PANE_FIRST, NULL))
    {
        return -1;
    }

    CRichEditCtrl ctrl;
    ctrl.Create(WS_CHILD, CRect(0, 0, 0, 0), this, 10991);

    return 0;
}


void CRichEditPane::OnSize(UINT nType, int cx, int cy)
{
    CDockablePane::OnSize(nType, cx, cy);

    AdjustLayout();
}



// CRichEditViewInPane .h
class CRichEditViewInPane : public CRichEditView
{
    DECLARE_DYNCREATE(CRichEditViewInPane)

protected:
    CRichEditViewInPane();           // protected constructor used by dynamic creation
    virtual ~CRichEditViewInPane();

public:
#ifdef _DEBUG
    virtual void AssertValid() const;
#ifndef _WIN32_WCE
    virtual void Dump(CDumpContext& dc) const;
#endif
#endif

protected:
    DECLARE_MESSAGE_MAP()
};

// CRichEditViewInPane。 cpp
IMPLEMENT_DYNCREATE(CRichEditViewInPane, CRichEditView)

CRichEditViewInPane::CRichEditViewInPane()
{

}

CRichEditViewInPane::~CRichEditViewInPane()
{
}

BEGIN_MESSAGE_MAP(CRichEditViewInPane, CRichEditView)
END_MESSAGE_MAP()

关于c++ - MFC CView放入CDockablePane,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27331291/

10-13 07:04