MFC下Dialg下添加CScrollView

CxxxDialg.cpp文件

BOOL CxxxDialg::OnInitDialog() 
{
    CDialog::OnInitDialog();

    MoveWindow(0,0,xResDisplay,yResDisplay);

    CRect rect;
    GetClientRect(&rect);    context.m_pCurrentDoc=NULL;
    context.m_pCurrentFrame=(CFrameWnd*)this;
    context.m_pLastView=NULL;
    context.m_pNewDocTemplate=NULL;
    context.m_pNewViewClass=RUNTIME_CLASS(CMyView);
    m_pView=(CMyView*)context.m_pNewViewClass->CreateObject();
    
    if (m_pView==NULL) 
    {
        TRACE1("Warning: Dynamic create of view type %hs failed.\n",
            context.m_pNewViewClass->m_lpszClassName);
    }
    ASSERT_KINDOF(CWnd,m_pView);
    //2.真正创建视图窗口
    if (!m_pView->Create(NULL,NULL,AFX_WS_DEFAULT_VIEW,rect,this,
        AFX_IDW_PANE_FIRST,&context)) 
    {
        TRACE0("Warning: Couldn't create view for frame.\n");
        return FALSE;
    }
    CSize sizeTotal;
    sizeTotal.cx = sizeTotal.cy = 10000;//这个值可根据实际情况设
    m_pView->SetScrollSizes(MM_TEXT, sizeTotal);//这个函数必须先于MoveWindow执行
    rect.InflateRect(-200,-200);    m_pView->MoveWindow(rect);//确保MyView能显示出来
    return TRUE;  // return TRUE unless you set the focus to a control
                  // EXCEPTION: OCX Property Pages should return FALSE
}

CxxxDialg.h文件

{

    CMyView*  m_pView;
    CCreateContext context;

}

myview.h//(一定要添加4个虚函数,不然关闭对话框会报错)


class CMyView : public CScrollView
{
protected:
    CMyView();           // protected constructor used by dynamic creation
    DECLARE_DYNCREATE(CMyView)

// Attributes
public:

// Operations
public:

// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CMyView)
    protected:
    virtual void OnDraw(CDC* pDC);      // overridden to draw this view
    virtual void OnInitialUpdate();     // first time after construct
    //}}AFX_VIRTUAL

// Implementation
protected:
    virtual ~CMyView();
#ifdef _DEBUG
    virtual void AssertValid() const;
    virtual void Dump(CDumpContext& dc) const;
#endif

//新添加虚函数
    afx_msg int OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message);
    afx_msg void OnDestroy();
    virtual void PostNcDestroy();
    virtual void OnActivateFrame(UINT nState, CFrameWnd* pDeactivateFrame);

//

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

 

myview.cpp     //(一定要添加4个虚函数,不然关闭对话框会报错)

 


#include "stdafx.h"
#include "MyView.h"

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

/
// CMyView

IMPLEMENT_DYNCREATE(CMyView, CScrollView)

CMyView::CMyView()
{
}

CMyView::~CMyView()
{
}


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

/
// CMyView drawing

void CMyView::OnInitialUpdate()
{
    CScrollView::OnInitialUpdate();

    CSize sizeTotal;
    // TODO: calculate the total size of this view
    sizeTotal.cx = sizeTotal.cy = 100;
    SetScrollSizes(MM_TEXT, sizeTotal);
}

void CMyView::OnDraw(CDC* pDC)
{
    CDocument* pDoc = GetDocument();
    // TODO: add draw code here
}

/
// CMyView diagnostics

#ifdef _DEBUG
void CMyView::AssertValid() const
{
    CScrollView::AssertValid();
}

void CMyView::Dump(CDumpContext& dc) const
{
    CScrollView::Dump(dc);
}
#endif //_DEBUG

/
// CMyView message handlers

\
int CMyView::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message)
{
    // TODO: Add your message handler code here and/or call default
    
    return CWnd::OnMouseActivate(pDesktopWnd, nHitTest, message);
}
void CMyView::OnDestroy()
{
    CWnd::OnDestroy();
    
    // TODO: Add your message handler code here
}

void CMyView::PostNcDestroy()
{
    // TODO: Add your specialized code here and/or call the base class
    
    CWnd::PostNcDestroy();
}

void CMyView::OnActivateFrame(UINT nState, CFrameWnd* pDeactivateFrame)
{
    // TODO: Add your specialized code here and/or call the base class
    
   // CWnd::OnActivateFrame(nState, pDeactivateFrame);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值