那位大狭帮我把下面C++程序转成Delphi程序,根据Internet Explorer_Server窗口得到IHtmlDocument2接口 ( 积分: 10

  • 主题发起人 主题发起人 dxymzj
  • 开始时间 开始时间
D

dxymzj

Unregistered / Unconfirmed
GUEST, unregistred user!
那位大狭帮我把下面C++程序转成Delphi程序,根据Internet Explorer_Server窗口得到IHtmlDocument2接口 ( 积分: 100 )<br />#include &lt;mshtml.h&gt;
#include &lt;atlbase.h&gt;
#include &lt;oleacc.h&gt;

BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam)
{
TCHAR buf[100];

::GetClassName( hwnd, (LPTSTR)&amp;buf, 100 );
if ( _tcscmp( buf, _T(&quot;Internet Explorer_Server&quot;) ) == 0 ) /

{
*(HWND*)lParam = hwnd;
return FALSE;
}
else
return TRUE;
};

//You can store the interface pointer in a member variable
//for easier access
void CDlg::OnGetDocInterface(HWND hWnd)
{
CoInitialize( NULL );

// Explicitly load MSAA so we know if it's installed
HINSTANCE hInst = ::LoadLibrary( _T(&quot;OLEACC.DLL&quot;) );
if ( hInst != NULL )
{
if ( hWnd != NULL )
{
HWND hWndChild=NULL;
// Get 1st document window
::EnumChildWindows( hWnd, EnumChildProc, (LPARAM)&amp;hWndChild );
if ( hWndChild )
{
CComPtr&lt;IHTMLDocument2&gt; spDoc;
LRESULT lRes;

UINT nMsg = ::RegisterWindowMessage( _T(&quot;WM_HTML_GETOBJECT&quot;) );
::SendMessageTimeout( hWndChild, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&amp;lRes );

LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T(&quot;ObjectFromLresult&quot;) );
if ( pfObjectFromLresult != NULL )
{
HRESULT hr;
hr = (*pfObjectFromLresult)( lRes, IID_IHTMLDocument, 0, (void**)&amp;spDoc );
if ( SUCCEEDED(hr) )
{
CComPtr&lt;IDispatch&gt; spDisp;
CComQIPtr&lt;IHTMLWindow2&gt; spWin;
spDoc-&gt;get_Script( &amp;spDisp );
spWin = spDisp;
spWin-&gt;get_document( &amp;spDoc.p );
// Change background color to red
spDoc-&gt;put_bgColor( CComVariant(&quot;red&quot;) );
}
}
} // else document not ready
} // else Internet Explorer is not running
::FreeLibrary( hInst );
} // else Active Accessibility is not installed
CoUninitialize();
}
 
#include &lt;mshtml.h&gt;
#include &lt;atlbase.h&gt;
#include &lt;oleacc.h&gt;

BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam)
{
TCHAR buf[100];

::GetClassName( hwnd, (LPTSTR)&amp;buf, 100 );
if ( _tcscmp( buf, _T(&quot;Internet Explorer_Server&quot;) ) == 0 ) /

{
*(HWND*)lParam = hwnd;
return FALSE;
}
else
return TRUE;
};

//You can store the interface pointer in a member variable
//for easier access
void CDlg::OnGetDocInterface(HWND hWnd)
{
CoInitialize( NULL );

// Explicitly load MSAA so we know if it's installed
HINSTANCE hInst = ::LoadLibrary( _T(&quot;OLEACC.DLL&quot;) );
if ( hInst != NULL )
{
if ( hWnd != NULL )
{
HWND hWndChild=NULL;
// Get 1st document window
::EnumChildWindows( hWnd, EnumChildProc, (LPARAM)&amp;hWndChild );
if ( hWndChild )
{
CComPtr&lt;IHTMLDocument2&gt; spDoc;
LRESULT lRes;

UINT nMsg = ::RegisterWindowMessage( _T(&quot;WM_HTML_GETOBJECT&quot;) );
::SendMessageTimeout( hWndChild, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&amp;lRes );

LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T(&quot;ObjectFromLresult&quot;) );
if ( pfObjectFromLresult != NULL )
{
HRESULT hr;
hr = (*pfObjectFromLresult)( lRes, IID_IHTMLDocument, 0, (void**)&amp;spDoc );
if ( SUCCEEDED(hr) )
{
CComPtr&lt;IDispatch&gt; spDisp;
CComQIPtr&lt;IHTMLWindow2&gt; spWin;
spDoc-&gt;get_Script( &amp;spDisp );
spWin = spDisp;
spWin-&gt;get_document( &amp;spDoc.p );
// Change background color to red
spDoc-&gt;put_bgColor( CComVariant(&quot;red&quot;) );
}
}
} // else document not ready
} // else Internet Explorer is not running
::FreeLibrary( hInst );
} // else Active Accessibility is not installed
CoUninitialize();
}
 
用这个函数呢?
function TForm1.GetIEFromHWND(WHandle: HWND; var IE: IWebbrowser2): HRESULT;
var
hInst: HWND;
lRes: Cardinal;
MSG: Integer;
pDoc: IHTMLDocument2;
ObjectFromLresult: TObjectFromLresult;
begin
hInst := LoadLibrary('Oleacc.dll');
@ObjectFromLresult := GetProcAddress(hInst, 'ObjectFromLresult');
if @ObjectFromLresult &lt;&gt; nil then begin
try
MSG := RegisterWindowMessage('WM_HTML_GETOBJECT');
SendMessageTimeOut(WHandle, MSG, 0, 0, SMTO_ABORTIFHUNG, 1000, lRes);
Result := ObjectFromLresult(lRes, IHTMLDocument2, 0, pDoc);
if Result = S_OK then
(pDoc.parentWindow as IServiceprovider).QueryService(IWebbrowserApp, IWebbrowser2, IE);
finally
FreeLibrary(hInst);
end;
end;
end;
 
编译不过,你写如下函数:好么?谢谢!能够成功的话,马上给分,不够可以再加
function GetHtmlEditDocment(DHtmlEdit: TDHtmlEdit: ):IHTMLDocument2;
 
Uses那里加上这几个单元
uses
SHDocVW_TLB,MSHTML_TLB,
ActiveX, OleCtrls, SHDocVw;
 
多人接受答案了。
 
后退
顶部