64,642
社区成员
发帖
与我相关
我的任务
分享
HRESULT CViewSource::GetDocumentContent()
{
USES_CONVERSION;
// Get the WebBrowser's document object
CComPtr<IDispatch> pDisp;
HRESULT hr = m_spWebBrowser2->get_Document(&pDisp);
if (FAILED(hr))
return hr;
// Verify that what we get is a pointer to a IHTMLDocument2
// interface. To be sure, let's query for
// the IHTMLDocument2 interface (through smart pointers)
CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> spHTML;
spHTML = pDisp;
// Extract the source code of the document
if (spHTML)
{
// Get the BODY object
hr = spHTML->get_body(&m_pBody);
if (FAILED(hr))
return hr;
// Get the HTML text
BSTR bstrHTMLText;
hr = m_pBody->get_outerHTML(&bstrHTMLText);
if (FAILED(hr))
return hr;
// Convert the text from Unicode to ANSI
LPTSTR psz = new TCHAR[SysStringLen(bstrHTMLText)];
lstrcpy(psz, OLE2T(bstrHTMLText));
// Enable changes to the text
HWND hwnd = m_dlgCode.GetDlgItem(IDC_TEXT);
EnableWindow(hwnd, true);
hwnd = m_dlgCode.GetDlgItem(IDC_APPLY);
EnableWindow(hwnd, true);
// Set the text in the Code Window
m_dlgCode.SetDlgItemText(IDC_TEXT, psz);
delete [] psz;
}
else // The document isn't a HTML page
{
m_dlgCode.SetDlgItemText(IDC_TEXT, "");
HWND hwnd = m_dlgCode.GetDlgItem(IDC_TEXT);
EnableWindow(hwnd, false);
hwnd = m_dlgCode.GetDlgItem(IDC_APPLY);
EnableWindow(hwnd, false);
}
return S_OK;
}