获取IWebBrowser2指针的方法(一)

在Internet Explorer编程中,获取WebBrowser指针通常是一件很重要的事情,因为有了WebBrowser指针,我们就有了对IE完整的控制权。我们就可以对IE浏览器为所欲为了,想干什么都可以。比方说获取或者设置DOM控件的值。调用页面中的JavaScript,或者控制浏览器的行为 比方说刷新,前进、后退等等等等。。

下面两篇文章中,我将介绍两种方法来获取IWebBrowser2指针。

第一种

使用OLEACC.dll动态库中的ObjectFromLresult函数来获取。

第二种

使用IShellWindows 获取当前浏览器个数,然后遍历获取浏览器对象和IWebBrowser指针。

参见:获取IWebBrowser2指针的方法(二)

下面介绍第一种方法:

在MSDN中对ObjectFromLresult函数的解释

The LresultFromObject function returns a reference, that is similar to a handle, to the specified object. Servers return this reference when handling WM_GETOBJECT.

所以就必须要调用::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );和SendMessageTimeout返回的与WM_GETOBJECT消息关联的值。这样就可以利用ObjectFromLresult来获取IHTMLDocument2指针,有了IHTMLDocument2,剩下的事情就是QueryInterface来获取IWebBrowser2指针,这样就很轻松了。

下面看下代码:
view plaincopy to clipboardprint?
//用来接收EnumChildWindows的回调函数  
//根据类名Internet Explorer_Server来判断是否是Internet Explorer控件  
BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam)   
{  
    TCHAR buf[100];  
    ::GetClassName( hwnd, (LPTSTR)&buf, 100 );  
    if ( _tcscmp( buf, _T("Internet Explorer_Server") ) == 0 )   
    {  
        *(HWND*)lParam = hwnd; return FALSE;   
    }   
    else return TRUE;   
};  
IWebBrowser2* GetWBByhWnd(HWND hIEWindow)   
{  
    CoInitialize( NULL );  
    // Explicitly load MSAA so we know if it's installed   
    HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );  
    IWebBrowser2* pWebBrowser2=NULL;  
    if ( hInst != NULL )   
    {   
        HWND hWnd ;  
        if(hIEWindow==NULL)  
        {  
            hWnd= FindWindow("IEFrame", NULL);  
            if(hWnd==NULL)  
                hWnd= FindWindow("CabinetWClass", NULL);  
            if( hWnd == NULL)  
            {  
                MessageBox (NULL,"No Running instance of Internet Explorer!","message", MB_OK);  
            }  
            // walk Shell DocObject View->Internet Explorer_Server  
            HWND hWndChild = FindWindowEx(hWnd, 0, "Shell DocObject View", NULL);  
            if(hWndChild !=0)  
            {  
                hWndChild = FindWindowEx(hWndChild, 0, "Internet Explorer_Server", NULL);  
            }  
            hWnd=hWndChild;  
        }  
        else 
        {  
            hWnd = hIEWindow;  
            HWND hWndChild=NULL;   
            // Get 1st document window   
            EnumChildWindows( hWnd, EnumChildProc, (LPARAM)&hWndChild );   
            if ( hWndChild )   
            {  
                CComPtr<IHTMLDocument2> spDoc;  
                LRESULT lRes;   
                UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );  
                ::SendMessageTimeout( hWndChild, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );   
                LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T("ObjectFromLresult") );  
                if ( pfObjectFromLresult != NULL )   
                {  
                    HRESULT hr;  
                    hr = (*pfObjectFromLresult)( lRes, IID_IHTMLDocument2, 0, (void**)&spDoc );  
                    if ( SUCCEEDED(hr) )   
                    {  
                        CComPtr<IHTMLWindow2>spWnd2;  
                        CComPtr<IServiceProvider>spServiceProv;  
                        hr=spDoc->get_parentWindow ((IHTMLWindow2**)&spWnd2);  
                        if(SUCCEEDED(hr))  
                        {  
                            hr=spWnd2->QueryInterface (IID_IServiceProvider,(void**)&spServiceProv);  
                            if(SUCCEEDED(hr))  
                            {  
                                hr = spServiceProv->QueryService(SID_SWebBrowserApp,IID_IWebBrowser2,(void**)&pWebBrowser2);  
                                if(SUCCEEDED(hr))  
                                {  
                                    return pWebBrowser2;  
                                }  
                            }  
                        }  
                    }  
                }  
            } // else document not ready   
        } // else Internet Explorer is not running   
        ::FreeLibrary( hInst );   
    } // else Active Accessibility is not installed   
    CoUninitialize();   
    return NULL;  
}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/tingsking18/archive/2009/09/30/4620065.aspx

posted @ 2010-02-14 17:43  猪悟能  阅读(586)  评论(0编辑  收藏  举报