void CMyHtmlView::OnDesignMode()
{
// TODO: Add your command handler code here
HRESULT hr;
IHTMLDocument2* piDoc = 0;
IWebBrowser2* piWeb = 0;
IDispatch* piDisp = 0;
IUnknown* piUnk = m_wndBrowser.GetControlUnknown();
hr = piUnk->QueryInterface(IID_IWebBrowser2, (void**)&piWeb);
if(FAILED(hr))
{
AfxMessageBox("QueryInterface for IWebBrowser2");
return;
}
hr = piWeb->get_Document(&piDisp);
if(SUCCEEDED(hr))
{
hr = piDisp->QueryInterface(IID_IHTMLDocument2, (void**)&piDoc);
if(SUCCEEDED(hr))
{
if(m_MyClientSite.m_bDocDesignMode)
piDoc->put_designMode(L"Inherit");
else
piDoc->put_designMode(L"On");
m_MyClientSite.m_bDocDesignMode = !m_MyClientSite.m_bDocDesignMode;
piDoc->Release();
}
else
AfxMessageBox("QueryInterface doc");
piDisp->Release();
}
else
AfxMessageBox("get_Document");
}
void CMyHtmlView::OnSaveToFile()
{
// TODO: Add your command handler code here
CFileDialog dlg(FALSE, NULL, NULL,
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
NULL, NULL);
if(dlg.DoModal()==IDCANCEL)
return;
_bstr_t path=dlg.GetPathName();
HRESULT hr;
IWebBrowser2* piWeb = 0;
IDispatch* piDisp = 0;
IPersistFile* piPF = 0;
IUnknown* piUnk = m_wndBrowser.GetControlUnknown();
hr = piUnk->QueryInterface(IID_IWebBrowser2, (void**)&piWeb);
if(FAILED(hr))
{
AfxMessageBox("QueryInterface for IWebBrowser2");
return;
}
hr = piWeb->get_Document(&piDisp);
if(SUCCEEDED(hr))
{
hr = piDisp->QueryInterface(IID_IPersistFile, (void**)&piPF);
if(SUCCEEDED(hr))
{
piPF->Save(path, TRUE);
piPF->Release();
}
piDisp->Release();
}
}
void CMyHtmlView::OnModifyBody()
{
// TODO: Add your command handler code here
HRESULT hr;
IHTMLDocument2* piDoc = 0;
IWebBrowser2* piWeb = 0;
IDispatch* piDisp = 0;
IHTMLElement* piElem = 0;
IHTMLBodyElement* piBody = 0;
IUnknown* piUnk = m_wndBrowser.GetControlUnknown();
hr = piUnk->QueryInterface(IID_IWebBrowser2, (void**)&piWeb);
if(FAILED(hr))
{
AfxMessageBox("QueryInterface for IWebBrowser2");
return;
}
hr = piWeb->get_Document(&piDisp);
if(SUCCEEDED(hr))
{
hr = piDisp->QueryInterface(IID_IHTMLDocument2, (void**)&piDoc);
if(SUCCEEDED(hr))
{
hr = piDoc->get_body(&piElem);
if(SUCCEEDED(hr))
{
hr = piElem->QueryInterface(IID_IHTMLBodyElement, (void**)&piBody);
if(SUCCEEDED(hr))
{
VARIANT v;
piBody->get_bgColor(&v);
_bstr_t bstr=v;
// AfxMessageBox((char*)bstr);
v.vt=VT_BSTR;
v.bstrVal = ::SysAllocString(OLESTR("#00FF00"));
piBody->put_bgColor(v);
::SysFreeString(v.bstrVal);
piBody->Release();
}
else
AfxMessageBox("QueryInterface body");
piElem->Release();
}
else
AfxMessageBox("get_body");
piDoc->Release();
}
else
AfxMessageBox("QueryInterface doc");
piDisp->Release();
}
else
AfxMessageBox("get_Document");
}
其中m_MyClientSite.m_bDocDesignMode是当前状态。