about:blankでNavigate2しておく。
#import <SHDOCVW.DLL> rename_namespace ( "IE" ) named_guids #include <mshtml.h> #include <comdef.h> class CDlgWebBrowser : public CDialog { protected: IE::IWebBrowser2* m_pWebBrowser; CWnd m_wndBrowser; };
int CDlgWebBrowser::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CDialog::OnCreate(lpCreateStruct) == -1) return -1; AfxEnableControlContainer(); if (!m_wndBrowser.CreateControl(CLSID_WebBrowser, NULL, WS_VISIBLE|WS_CHILD, CRect(0, 0, 10, 10), this, AFX_IDW_PANE_FIRST)) { return -1; } LPUNKNOWN lpUnk = m_wndBrowser.GetControlUnknown(); HRESULT hr = lpUnk->QueryInterface(IID_IWebBrowser2, (void**) &m_pWebBrowser); if (!SUCCEEDED(hr)) { m_pWebBrowser = NULL; m_wndBrowser.DestroyWindow(); return -1; } _variant_t vtBuf = _T("about:blank"); m_pWebBrowser->Navigate2( &vtBuf, NULL, NULL, NULL, NULL ); return 0; } void CDlgWebBrowser::OnDestroy() { CDialog::OnDestroy(); if (m_pWebBrowser) { m_pWebBrowser->Release(); m_pWebBrowser = NULL; } } void CDlgWebBrowser::OnSize(UINT nType, int cx, int cy) { (略) m_wndBrowser.SetWindowPos(NULL, x, y, wx, wy, SWP_NOOWNERZORDER|SWP_NOZORDER); }
void CDlgWebBrowser::Write(LPCTSTR szMes) // szMesはHTML { HWND phWndBuf = ::GetFocus(); HRESULT hr; LPDISPATCH lpDisp = m_pWebBrowser->GetDocument(); if (lpDisp) { IHTMLDocument2* pHtmlDoc = NULL; if (SUCCEEDED(hr = lpDisp->QueryInterface( IID_IHTMLDocument2, (void**)&pHtmlDoc ))) { SAFEARRAY* sfArray; sfArray = ::SafeArrayCreateVector(VT_VARIANT, 0, 1); if (sfArray) { VARIANT* pvtBuf; hr = ::SafeArrayAccessData(sfArray, (LPVOID*)&pvtBuf); pvtBuf->vt = VT_BSTR; pvtBuf->bstrVal = ::SysAllocString(_bstr_t(szMes)); hr = ::SafeArrayUnaccessData(sfArray); hr = pHtmlDoc->write(sfArray); hr = pHtmlDoc->close(); ::SysFreeString(pvtBuf->bstrVal); ::SafeArrayDestroy(sfArray); } } } ::SetFocus(phWndBuf); }