/********************************************************************
@created: 2006/01/16
@version: 0.1
@filename: WebBrowser.cpp
@file base: WebBrowser
@file ext: cpp
@author: Pomelo Wu
@purpose: Implementation for class CWebBrowser, a wrapper of
IWebBrowser2.
@remark: NULL
@revision history:
-- Date Ver. Modifier Description--
2006/01/16 0.1 Pomelo Create
*********************************************************************/
#include <Exdisp.h> /* Defines of stuff like IWebBrowser2. This is an include file with Visual C 6 and above */
#include <Mshtml.h> /* Defines of stuff like IHTMLDocument2. This is an include file with Visual C 6 and above */
/* If you gets some error when compling under VS2005, move mshtml.h to top*/
#include <crtdbg.h> /* for _ASSERT() */
#include "WBStorage.h"
#include "WBClientsite.h"
#include "WebBrowser.h"
/* This is used by DisplayHTMLStr(). It can be global because we never change it. */
static const SAFEARRAYBOUND ArrayBound = {1, 0};
CWebBrowser::CWebBrowser(void)
{
m_pBrowserObj = NULL;
}
CWebBrowser::~CWebBrowser(void)
{
}
void CWebBrowser::UnEmbedBrowserObject(HWND /*hwnd*/)
{
//////////////////////////////////////////////////////////////////////////
//
// Not implemented for multiple browsing...
//
//IOleObject **ppBrowserHandle;
//IOleObject *pBrowserObject;
//
//// Retrieve the browser object's pointer we stored in our window's GWL_USERDATA when
//// we initially attached the browser object to this window.
//if ((ppBrowserHandle = (IOleObject **)GetWindowLong(hwnd, GWL_USERDATA)))
//{
// // Unembed the browser object, and release its resources.
// pBrowserObject = *ppBrowserHandle;
// pBrowserObject->Close(OLECLOSE_NOSAVE);
// pBrowserObject->Release();
//}
//////////////////////////////////////////////////////////////////////////
if (m_pBrowserObj)
{
// Unembed the browser object, and release its resources.
m_pBrowserObj->Close(OLECLOSE_NOSAVE);
m_pBrowserObj->Release();
return;
}
// You must have called this for a window that wasn't successfully passed to EmbedBrowserObject().
// Bad boy!
_ASSERT(0);
}
long CWebBrowser::DisplayHTMLStr(HWND /*hwnd*/, LPCTSTR string)
{
IWebBrowser2 *pWebBrowser2;
LPDISPATCH lpDispatch;
IHTMLDocument2 *pHtmlDoc2;
SAFEARRAY *psfArray;
VARIANT varMyURL;
VARIANT *pVar;
BSTR bstr;
// Assume an error.
bstr = 0;
//////////////////////////////////////////////////////////////////////////
//
// Not implemented for multiple browsing...
//
//IOleObject **ppBrowserHandle;
//IOleObject *pBrowserObject;
//
//// Retrieve the browser object's pointer we stored in our window's GWL_USERDATA when
//// we initially attached the browser object to this window.
//if ((ppBrowserHandle = (IOleObject **)GetWindowLong(hwnd, GWL_USERDATA)))
//{
// // Unembed the browser object, and release its resources.
// pBrowserObject = *ppBrowserHandle;
//}
//////////////////////////////////////////////////////////////////////////
// We want to get the base address (ie, a pointer) to the IWebBrowser2 object embedded within the browser
// object, so we can call some of the functions in the former's table.
if (!m_pBrowserObj->QueryInterface(IID_IWebBrowser2, (void**)&pWebBrowser2))
{
// Before we can get_Document(), we actually need to have some HTML page loaded in the browser. So,
// let's load an empty HTML page. Then, once we have that empty page, we can get_Document() and
// write() to stuff our HTML string into it.
VariantInit(&varMyURL);
varMyURL.vt = VT_BSTR;
varMyURL.bstrVal = SysAllocString(L"about:blank");
// Call the Navigate2() function to actually display the page.
pWebBrowser2->Navigate2(&varMyURL, 0, 0, 0, 0);
// Free any resources (including the BSTR).
VariantClear(&varMyURL);
// Call the IWebBrowser2 object's get_Document so we can get its DISPATCH object. I don't know why you
// don't get the DISPATCH object via the browser object's QueryInterface(), but you don't.
if (!pWebBrowser2->get_Document(&lpDispatch))
{
// We want to get a pointer to the IHTMLDocument2 object embedded within the DISPATCH
// object, so we can call some of the functions in the former's table.
if (!lpDispatch->QueryInterface(IID_IHTMLDocument2, (void**)&pHtmlDoc2))
{
// Our HTML must be in the form of a BSTR. And it must be passed to write() in an
// array of "VARIENT" structures. So let's create all that.
if ((psfArray = SafeArrayCreate(VT_VARIANT, 1, (SAFEARRAYBOUND *)&ArrayBound)))
{
if (!SafeArrayAccessData(psfArray, (void**)&pVar))
{
pVar->vt = VT_BSTR;
#ifndef UNICODE
{
wchar_t *buffer;
DWORD size;
size = MultiByteToWideChar(CP_ACP, 0, string, -1, 0, 0);
if (!(buffer = (wchar_t *)GlobalAlloc(GMEM_FIXED, sizeof(wchar_t) * size))) goto bad;
MultiByteToWideChar(CP_ACP, 0, string, -1, buffer, size);
bstr = SysAllocString(buffer);
GlobalFree(buffer);
}
#else
bstr = SysAllocString(string);
#endif
// Store our BSTR pointer in the VARIENT.
if ((pVar->bstrVal = bstr))
{
// Pass the VARIENT with its BSTR to write() in order to shove our desired HTML string
// into the body of that empty page we created above.
pHtmlDoc2->write(psfArray);
// Normally, we'd need to free our BSTR, but SafeArrayDestroy() does it for us
// SysFreeString(bstr);
}
}
// Free the array. This also frees the VARIENT that SafeArrayAccessData created for us,
// and even frees the BSTR we allocated with SysAllocString
SafeArrayDestroy(psfArray);
}
// Release the IHTMLDocument2 object.
#ifndef UNICODE
bad:
#endif
pHtmlDoc2->Release();
}
// Release the DISPATCH object.
lpDispatch->Release();
}
// Release the IWebBrowser2 object.
pWebBrowser2->Release();
}
// No error?
if (bstr) return 0;
// An error
return -1;
}
long CWebBrowser::DisplayHTMLPage(HWND /*hwnd*/, LPCTSTR webPageName)
{
IWebBrowser2 *pWebBrowser2;
VARIANT varMyURL;
//////////////////////////////////////////////////////////////////////////
//
// Not implemented for multiple browsing...
//
//IOleObject **ppBrowserHandle;
//IOleObject *pBrowserObject;
//
//// Retrieve the browser object's pointer we stored in our window's GWL_USERDATA when
//// we initially attached the browser object to this window.
//if ((ppBrowserHandle = (IOleObject **)GetWindowLong(hwnd, GWL_USERDATA)))
//{
// // Unembed the browser object, and release its resources.
// pBrowserObject = *ppBrowserHandle;
//}
//////////////////////////////////////////////////////////////////////////
// We want to get the base address (ie, a pointer) to the IWebBrowser2 object embedded within the browser
// object, so we can call some of the functions in the former's table.
if (!m_pBrowserObj->QueryInterface(IID_IWebBrowser2, (void**)&pWebBrowser2))
{
// Our URL (ie, web address, such as "http://www.microsoft.com" or an HTM filename on disk
// such as "c:\myfile.htm") must be passed to the IWebBrowser2's Navigate2() function as a BSTR.
// A BSTR is like a pascal version of a double-byte character string. In other words, the
// first unsigned short is a count of how many characters are in the string, and then this
// is followed by those characters, each expressed as an unsigned short (rather than a
// char). The string is not nul-terminated. The OS function SysAllocString can allocate and
// copy a UNICODE C string to a BSTR. Of course, we'll need to free that BSTR after we're done
// with it. If we're not using UNICODE, we first have to convert to a UNICODE string.
//
// What's more, our BSTR needs to be stuffed into a VARIENT stru
《WebBrowser Control的一个简单C++封装》的实例代码。
5星 · 超过95%的资源 需积分: 14 128 浏览量
2009-01-15
14:31:06
上传
评论 5
收藏 8KB 7Z 举报
pomelowu
- 粉丝: 81
- 资源: 1
- 1
- 2
- 3
- 4
前往页