在Dialog中创建一个htmlview(CHtmlView类)区,实现浏览网页

来源:互联网 发布:登山包的正确背法 知乎 编辑:程序博客网 时间:2024/05/18 01:58

1 派生一个CDocument类,并设置对话框为该类的友元,如:

class CXXDlg;

class CMyDocument : public CDocument
{
 DECLARE_DYNCREATE(CMyDocument)

     friend class CXXDlg;
public:
 CMyDocument();
 virtual ~CMyDocument();
#ifndef _WIN32_WCE
 virtual void Serialize(CArchive& ar);   // 为文档 I/O 重写
#endif
#ifdef _DEBUG
 virtual void AssertValid() const;
#ifndef _WIN32_WCE
 virtual void Dump(CDumpContext& dc) const;
#endif
#endif

protected:
 virtual BOOL OnNewDocument();

 DECLARE_MESSAGE_MAP()
};

 

2 派生一个CDocument类,并设置对话框为该类的友元,如:

class CXXDlg;

class CMyLogShView : public CHtmlView
{
 DECLARE_DYNCREATE(CMyLogShView)

    friend class CXXDlg;

protected:
 CMyLogShView();           // 动态创建所使用的受保护的构造函数
 virtual ~CMyLogShView();

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

protected:
 virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

 DECLARE_MESSAGE_MAP()
};

 

 

3 在CXXDlg中定义一个创建HTMLVIEW的函数(当然你也可以不用定义):

CMyLogShView* CXXDlg::CreateMyLogShView(void)
{
    CCreateContext pContext;
    CWnd* pFramewnd = this;
    pContext.m_pCurrentDoc = new CMyDocument;
    pContext.m_pNewViewClass = RUNTIME_CLASS(CMyLogShView);

    CMyLogShView *pMyLogShView =
        (CMyLogShView *)((CFrameWnd*)pFramewnd)->CreateView(&pContext);

    if (pMyLogShView == NULL)
    {
        return NULL;
    }

    pMyLogShView->ShowWindow(SW_NORMAL);

    CRect rectWindow;
    GetWindowRect(rectWindow);
    rectWindow.left -= 5;
    rectWindow.right += 15;
    rectWindow.top -= 22;   //这些是用来设置htmlview的位置的,可以任意设置个人认为合理的

    pMyLogShView->MoveWindow(rectWindow);
    return pMyLogShView;
}

 

我是在CXXDlgOnInitDialog()函数中调用CreateMyLogShView()的,如下:

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

    .....

    .....

    ......

    // TODO:  在此添加额外的初始化

    CMyLogShView* pMyLogShView;

    pMyLogShView = CreateMyLogShView();

    return TRUE;  // return TRUE unless you set the focus to a control
}

 

原创粉丝点击