CWebBrowser2的用法

weblxj 2002-09-19 02:29:10
请教各位大哥

我如何在一个函数中自己动态(因为我不需要对话框)创建CWebBrowser2控件,实现打印一个指定.htm文件的功能?
急盼回复。
我的oicq:44758837
...全文
285 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
weblxj 2002-09-25
  • 打赏
  • 举报
回复
jiangsheng(蒋晟.Net):再帮帮我,请教

我是一个单文档的应用,下面是一个菜单事件(点击一菜单项触发),功能是想通过自己动态生成的explore控件实现.html文件的打印。结果最后execwb时就是没有执行打印。请大家帮我想想办法,中间还少了什么步骤,该怎么完成。不胜感激。下面是我的代码。
void CMainFrame::OnTt()
{

BOOL bRet = FALSE;
// create the control window
CRect rect;
AfxEnableControlContainer();

CWnd pUsrCtl;


CWnd *pParentWnd = AfxGetApp()->m_pMainWnd;
GetClientRect(&rect);
if (pUsrCtl.CreateControl(CLSID_WebBrowser, NULL,
WS_VISIBLE| WS_CHILD, CRect(0,0,0,0), this, AFX_IDW_PANE_FIRST))//
{
// in order to put the webbrowser in design mode, you must
// first load a document into it. "about:blank" seems to
// be the safest thing to load for a good starting point.
LPUNKNOWN lpUnk = pUsrCtl.GetControlUnknown();
HRESULT hr = lpUnk->QueryInterface(IID_IWebBrowser2, (void**) &pBrowserApp);
if (!SUCCEEDED(hr))
{
pBrowserApp = NULL;
pUsrCtl.DestroyWindow();
DestroyWindow();
return ;
}


COleVariant empty;
CString strURL("c:\\rr.htm");
BSTR bstrURL = strURL.AllocSysString();


COleVariant vURL("c:/rr.htm", VT_BSTR);
COleVariant vHeaders("", VT_BSTR);
COleVariant vTargetFrameName("", VT_BSTR);
COleVariant vFlags((long) 0, VT_I4);

pBrowserApp->Navigate2(vURL,vFlags, vTargetFrameName, NULL, vHeaders);
pBrowserApp->ExecWB(OLECMDID_PRINT,OLECMDEXECOPT_DONTPROMPTUSER,NULL,NULL);

}

}
蒋晟 2002-09-25
  • 打赏
  • 举报
回复
Do not create this control every time...place it on a CDialog/CFormView, or a controlbar.
蒋晟 2002-09-24
  • 打赏
  • 举报
回复
this是webbrowser2控件的父窗口
忘记CComPtr在哪里定义的了。去看一下MSDN
蒋晟 2002-09-24
  • 打赏
  • 举报
回复
this应该是一个有窗口的CWnd
weblxj 2002-09-24
  • 打赏
  • 举报
回复
jiangsheng(蒋晟.Net)::我知道this是webbrowser2控件的父窗口,并且我看过也是有值的,但就是不对,是不是需要自己重新创建一个新的窗口。
weblxj 2002-09-23
  • 打赏
  • 举报
回复

jiangsheng(蒋晟.Net) :你好,首先对你的帮助表示感谢。我根据你教我的方法,进行如下测试。
结果: if (CreateControl(CLSID_WebBrowser, NULL,
WS_VISIBLE, rect, this, AFX_IDW_PANE_FIRST))时老是出错。我debug后发现,是this参数用的不对。但我不知道原因,也不知道解决办法,所以再次请教你。

另外,你原来的代码中使用GetStartDocument()时,需要什么头文件。
void CMainFrame::OnTt()
{
// TODO: Add your command handler code here

BOOL bRet = FALSE;
// create the control window
CRect rect;
AfxEnableControlContainer();

CWnd *pParentWnd = AfxGetApp()->m_pMainWnd;
GetClientRect(&rect);
if (CreateControl(CLSID_WebBrowser, NULL,
WS_VISIBLE, rect, this, AFX_IDW_PANE_FIRST))
{
// in order to put the webbrowser in design mode, you must
// first load a document into it. "about:blank" seems to
// be the safest thing to load for a good starting point.
CComQIPtr<IWebBrowser2> pBrowserApp = GetControlUnknown();
ASSERT(pBrowserApp);
/* if (pBrowserApp)
{
CComVariant vEmpty;
LPCTSTR szDoc = GetStartDocument();
if (szDoc)
{
CComBSTR bstrStart(szDoc);
if (S_OK == pBrowserApp->Navigate(bstrStart,
&vEmpty,
&vEmpty,
&vEmpty,
&vEmpty))
{
bRet = TRUE;
}
}
else
bRet = TRUE;

}
}

if (!bRet)
DestroyWindow();
*/

pBrowserApp->Navigate (BSTR("c:\\rr.htm"),0,NULL,NULL,0);
pBrowserApp->ExecWB (OLECMDID_PRINT,OLECMDEXECOPT_DONTPROMPTUSER,NULL,NULL);
}

}
蒋晟 2002-09-19
  • 打赏
  • 举报
回复
BOOL CHtmlView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName,
DWORD dwStyle, const RECT& rect, CWnd* pParentWnd,
UINT nID, CCreateContext* pContext)
{
// create the view window itself
m_pCreateContext = pContext;
if (!CView::Create(lpszClassName, lpszWindowName,
dwStyle, rect, pParentWnd, nID, pContext))
{
return FALSE;
}

// assure that control containment is on
AfxEnableControlContainer();

RECT rectClient;
GetClientRect(&rectClient);

// create the control window
// AFX_IDW_PANE_FIRST is a safe but arbitrary ID
if (!m_wndBrowser.CreateControl(CLSID_WebBrowser, lpszWindowName,
WS_VISIBLE | WS_CHILD, rectClient, this, AFX_IDW_PANE_FIRST))
{
DestroyWindow();
return FALSE;
}

// cache the dispinterface
LPUNKNOWN lpUnk = m_wndBrowser.GetControlUnknown();
HRESULT hr = lpUnk->QueryInterface(IID_IWebBrowser2, (void**) &m_pBrowserApp);
if (!SUCCEEDED(hr))
{
m_pBrowserApp = NULL;
m_wndBrowser.DestroyWindow();
DestroyWindow();
return FALSE;
}

return TRUE;
}
BOOL CHtmlEditCtrl::Create(LPCTSTR lpszWindowName, DWORD /*dwStyle*/, const RECT& rect, CWnd* pParentWnd,
int nID, CCreateContext *pContext)
{
BOOL bRet = FALSE;
// create the control window

AfxEnableControlContainer();
if (CreateControl(CLSID_WebBrowser, lpszWindowName,
WS_VISIBLE | WS_CHILD, rect, pParentWnd, nID))
{
// in order to put the webbrowser in design mode, you must
// first load a document into it. "about:blank" seems to
// be the safest thing to load for a good starting point.
CComQIPtr<IWebBrowser2> pBrowserApp = GetControlUnknown();
ASSERT(pBrowserApp);
if (pBrowserApp)
{
CComVariant vEmpty;
LPCTSTR szDoc = GetStartDocument();
if (szDoc)
{
CComBSTR bstrStart(szDoc);
if (S_OK == pBrowserApp->Navigate(bstrStart,
&vEmpty,
&vEmpty,
&vEmpty,
&vEmpty))
{
bRet = TRUE;
}
}
else
bRet = TRUE;

}
}

if (!bRet)
DestroyWindow();
return bRet;
}
蒋晟 2002-09-19
  • 打赏
  • 举报
回复
see the CHtmlView code in MFC source
weblxj 2002-09-19
  • 打赏
  • 举报
回复
你好
请求给我to create的源码,我不知道其中的参数怎么用,怎么获得,谢谢!
蒋晟 2002-09-19
  • 打赏
  • 举报
回复
to print
m_wndWebbrowser2.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER,NULL,NULL);
to create
call CWnd::CreateControl

16,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧