6 个解决方案
#1
网上现成的代码
/*函数名:GetDocInterface
参数:hWnd,WebBrowser控件的窗口句柄
功能:通过WM_HTML_GETOBJECT取得控件的IHTMLDocument2接口
*/
IHTMLDocument2* GetDocInterface(HWND hWnd,char * url)
{
// 我们需要显示地装载OLEACC.DLL,这样我们才知道有没有安装MSAA
HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );
IHTMLDocument2* pDoc2=NULL;
if ( hInst != NULL )
{
if ( hWnd != NULL )
{
CComPtr<IHTMLDocument> spDoc=NULL;
LRESULT lRes;
UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
::SendMessageTimeout( hWnd, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );
LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T("ObjectFromLresult") );
if ( pfObjectFromLresult != NULL )
{
HRESULT hr;
hr=pfObjectFromLresult(lRes,IID_IHTMLDocument,0,(void**)&spDoc);
if ( SUCCEEDED(hr) )
{
CComPtr<IDispatch> spDisp;
CComQIPtr<IHTMLWindow2> spWin;
spDoc->get_Script( &spDisp );
spWin = spDisp;
spWin->get_document( &pDoc2 );
if(url)
{
BSTR bstrTxt1;
pDoc2->get_URL(&bstrTxt1);
CString cstr(bstrTxt1);
strcpy(url,cstr);
}
}
}
}
::FreeLibrary(hInst);
}
else
{//如果没有安装MSAA
AfxMessageBox(_T("请您安装Microsoft Active Accessibility(OLEACC.DLL)"));
}
return pDoc2;
}
#2
http://blog.vckbase.com/TeacherYang/archive/2005/04/20/4830.aspx
这是另一个方法。
这是另一个方法。
#3
不行呀。。获取的指针为空值。。下面是我的代码
CoInitialize( NULL );
long l = 1179966;//窗口句柄
HWND hhhh = (HWND)l;
::ShowWindow(hhhh,SW_SHOWNORMAL); //激活找到的前一个程序
::SetForegroundWindow(hhhh);
HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );
IHTMLDocument2* pDoc2=NULL;
if ( hInst != NULL )
{
if ( hhhh != NULL )
{
CComPtr<IHTMLDocument> spDoc=NULL;
LRESULT lRes;
UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
::SendMessageTimeout( hhhh, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );
LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T("ObjectFromLresult") );
if ( pfObjectFromLresult != NULL )
{
HRESULT hr;
hr=pfObjectFromLresult(lRes,IID_IHTMLDocument,0,(void**)&spDoc);
if ( SUCCEEDED(hr) )
{
CComPtr<IDispatch> spDisp;
CComQIPtr<IHTMLWindow2> spWin;
spDoc->get_Script( &spDisp );
spWin = spDisp;
spWin->get_document( &pDoc2 );
}
}
}
else
{
MessageBox("句柄为空!");
}
::FreeLibrary(hInst);
}
else
{//如果没有安装MSAA
AfxMessageBox(_T("请您安装Microsoft Active Accessibility(OLEACC.DLL)"));
}
if (pDoc2 == NULL)
MessageBox("获取的文档指针为空");
else
pDoc2->put_bgColor(CComVariant("green"));
CoUninitialize();
#4
好像不行 spWin = spDisp;
接口转换用这个
用 spDisp->QueryInterface(spWin);
接口转换用这个
用 spDisp->QueryInterface(spWin);
#5
VC6:
CComPtr< IShellWindows > spShellWin;
spShellWin.CoCreateInstance( CLSID_ShellWindows );
CComPtr< IDispatch > spDispIE;
spShellWin->Item(CComVariant( (long)0 ), &spDispIE );
CComQIPtr< IWebBrowser2 > spBrowser = spDispIE;
CComPtr < IDispatch > spDispDoc;
spBrowser->get_Document( &spDispDoc );
CComQIPtr< IHTMLDocument2 > spDocument2 = spDispDoc;
这样就可以获得了 spDocument2
CComPtr< IShellWindows > spShellWin;
spShellWin.CoCreateInstance( CLSID_ShellWindows );
CComPtr< IDispatch > spDispIE;
spShellWin->Item(CComVariant( (long)0 ), &spDispIE );
CComQIPtr< IWebBrowser2 > spBrowser = spDispIE;
CComPtr < IDispatch > spDispDoc;
spBrowser->get_Document( &spDispDoc );
CComQIPtr< IHTMLDocument2 > spDocument2 = spDispDoc;
这样就可以获得了 spDocument2
#6
C# 怎么写?
#1
网上现成的代码
/*函数名:GetDocInterface
参数:hWnd,WebBrowser控件的窗口句柄
功能:通过WM_HTML_GETOBJECT取得控件的IHTMLDocument2接口
*/
IHTMLDocument2* GetDocInterface(HWND hWnd,char * url)
{
// 我们需要显示地装载OLEACC.DLL,这样我们才知道有没有安装MSAA
HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );
IHTMLDocument2* pDoc2=NULL;
if ( hInst != NULL )
{
if ( hWnd != NULL )
{
CComPtr<IHTMLDocument> spDoc=NULL;
LRESULT lRes;
UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
::SendMessageTimeout( hWnd, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );
LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T("ObjectFromLresult") );
if ( pfObjectFromLresult != NULL )
{
HRESULT hr;
hr=pfObjectFromLresult(lRes,IID_IHTMLDocument,0,(void**)&spDoc);
if ( SUCCEEDED(hr) )
{
CComPtr<IDispatch> spDisp;
CComQIPtr<IHTMLWindow2> spWin;
spDoc->get_Script( &spDisp );
spWin = spDisp;
spWin->get_document( &pDoc2 );
if(url)
{
BSTR bstrTxt1;
pDoc2->get_URL(&bstrTxt1);
CString cstr(bstrTxt1);
strcpy(url,cstr);
}
}
}
}
::FreeLibrary(hInst);
}
else
{//如果没有安装MSAA
AfxMessageBox(_T("请您安装Microsoft Active Accessibility(OLEACC.DLL)"));
}
return pDoc2;
}
#2
http://blog.vckbase.com/TeacherYang/archive/2005/04/20/4830.aspx
这是另一个方法。
这是另一个方法。
#3
不行呀。。获取的指针为空值。。下面是我的代码
CoInitialize( NULL );
long l = 1179966;//窗口句柄
HWND hhhh = (HWND)l;
::ShowWindow(hhhh,SW_SHOWNORMAL); //激活找到的前一个程序
::SetForegroundWindow(hhhh);
HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );
IHTMLDocument2* pDoc2=NULL;
if ( hInst != NULL )
{
if ( hhhh != NULL )
{
CComPtr<IHTMLDocument> spDoc=NULL;
LRESULT lRes;
UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
::SendMessageTimeout( hhhh, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );
LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T("ObjectFromLresult") );
if ( pfObjectFromLresult != NULL )
{
HRESULT hr;
hr=pfObjectFromLresult(lRes,IID_IHTMLDocument,0,(void**)&spDoc);
if ( SUCCEEDED(hr) )
{
CComPtr<IDispatch> spDisp;
CComQIPtr<IHTMLWindow2> spWin;
spDoc->get_Script( &spDisp );
spWin = spDisp;
spWin->get_document( &pDoc2 );
}
}
}
else
{
MessageBox("句柄为空!");
}
::FreeLibrary(hInst);
}
else
{//如果没有安装MSAA
AfxMessageBox(_T("请您安装Microsoft Active Accessibility(OLEACC.DLL)"));
}
if (pDoc2 == NULL)
MessageBox("获取的文档指针为空");
else
pDoc2->put_bgColor(CComVariant("green"));
CoUninitialize();
#4
好像不行 spWin = spDisp;
接口转换用这个
用 spDisp->QueryInterface(spWin);
接口转换用这个
用 spDisp->QueryInterface(spWin);
#5
VC6:
CComPtr< IShellWindows > spShellWin;
spShellWin.CoCreateInstance( CLSID_ShellWindows );
CComPtr< IDispatch > spDispIE;
spShellWin->Item(CComVariant( (long)0 ), &spDispIE );
CComQIPtr< IWebBrowser2 > spBrowser = spDispIE;
CComPtr < IDispatch > spDispDoc;
spBrowser->get_Document( &spDispDoc );
CComQIPtr< IHTMLDocument2 > spDocument2 = spDispDoc;
这样就可以获得了 spDocument2
CComPtr< IShellWindows > spShellWin;
spShellWin.CoCreateInstance( CLSID_ShellWindows );
CComPtr< IDispatch > spDispIE;
spShellWin->Item(CComVariant( (long)0 ), &spDispIE );
CComQIPtr< IWebBrowser2 > spBrowser = spDispIE;
CComPtr < IDispatch > spDispDoc;
spBrowser->get_Document( &spDispDoc );
CComQIPtr< IHTMLDocument2 > spDocument2 = spDispDoc;
这样就可以获得了 spDocument2
#6
C# 怎么写?