如果您需要抑制IHTMLDocument用戶界面或用戶通知,則需要實現IOleClientSite
和定義爲DISPID_AMBIENT_DLCONTROL
的環境屬性。
從文檔"Download Control":
主機可以控制下載幀,圖像,爪哇, 的某些方面等-通過實現兩者的IOleClientSite和定義爲一個DISPID_AMBIENT_DLCONTROL環境屬性 。當主機的IDispatch :: Invoke 方法在dispidMember設置爲DISPID_AMBIENT_DLCONTROL時調用時, 應將零個或以下值的組合放置在 pvarResult中。
在這種情況下,您需要的標誌是DLCTL_SILENT
(也可能是DLCTL_NO_SCRIPTS
)。
如前所述,主機也應該實現IDispatch
(.Invoke
)和可選IPropertyNotifySink
(或其他COM event sink對象),如果你想從文件獲取事件通知(如DISPID_READYSTATE
例如)。
看看EmbeddedWB來源,看看它是如何實現的。特別是IEParser.pas
和UI_Less.pas
。它已經完全符合你的需求。
這裏是基於UI_Less
簡化的演示(沒有實施IPropertyNotifySink
):
uses ..., ActiveX, MSHTML;
const
DISPID_AMBIENT_DLCONTROL = (-5512);
type
TUILess = class(TComponent, IUnknown, IDispatch, IOleClientSite)
protected
// IDispatch
function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HRESULT; stdcall;
// IOleClientSite
function SaveObject: HRESULT; stdcall;
function GetMoniker(dwAssign: Longint; dwWhichMoniker: Longint;
out mk: IMoniker): HRESULT; stdcall;
function GetContainer(out container: IOleContainer): HRESULT; stdcall;
function ShowObject: HRESULT; stdcall;
function OnShowWindow(fShow: BOOL): HRESULT; stdcall;
function RequestNewObjectLayout: HRESULT; stdcall;
end;
implementation
function TUILess.Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HRESULT;
const
DLCTL_NO_SCRIPTS = $00000080;
DLCTL_NO_JAVA = $00000100;
DLCTL_NO_RUNACTIVEXCTLS = $00000200;
DLCTL_NO_DLACTIVEXCTLS = $00000400;
DLCTL_DOWNLOADONLY = $00000800;
DLCTL_SILENT = $40000000;
var
I: Integer;
begin
if DISPID_AMBIENT_DLCONTROL = DispID then
begin
I := DLCTL_DOWNLOADONLY + DLCTL_NO_SCRIPTS +
DLCTL_NO_JAVA + DLCTL_NO_DLACTIVEXCTLS +
DLCTL_NO_RUNACTIVEXCTLS +
DLCTL_SILENT;
PVariant(VarResult)^ := I;
Result := S_OK;
end
else
Result := DISP_E_MEMBERNOTFOUND;
end;
function TUILess.SaveObject: HRESULT;
begin
Result := E_NOTIMPL;
end;
function TUILess.GetMoniker(dwAssign: Longint; dwWhichMoniker: Longint;
out mk: IMoniker): HRESULT;
begin
Result := E_NOTIMPL;
end;
function TUILess.GetContainer(out container: IOleContainer): HRESULT;
begin
Result := E_NOTIMPL;
end;
function TUILess.ShowObject: HRESULT;
begin
Result := E_NOTIMPL;
end;
function TUILess.OnShowWindow(fShow: BOOL): HRESULT;
begin
Result := E_NOTIMPL;
end;
function TUILess.RequestNewObjectLayout: HRESULT;
begin
Result := E_NOTIMPL;
end;
procedure TForm1.Button1Click(Sender: TObject);
const
cHTML: WideString = '<b>test</b><script>alert("boo")</script>';
var
Doc: IHTMLDocument2;
DocClientSite: TUILess;
begin
DocClientSite := TUILess.Create(nil);
try
Doc := coHTMLDocument.Create as IHTMLDocument2;
try
(Doc as IOleObject).SetClientSite(DocClientSite);
(Doc as IOleControl).OnAmbientPropertyChange(DISPID_AMBIENT_DLCONTROL); // Invoke
OleVariant(Doc).write(cHTML);
Doc.close;
ShowMessage(Doc.body.innerHtml); // Test
finally
(Doc as IOleObject).SetClientSite(nil);
Doc := nil;
end;
finally
DocClientSite.Free;
end;
end;
您可以設置靜音標誌嗎? – 2014-08-29 15:15:42
您可以使用websearch http://docwiki.embarcadero.com/Libraries/en/SHDocVw.TWebBrowser.Silent – 2014-08-29 15:56:03
TDownloadURL沒有靜音屬性,對不對? – 2014-08-29 19:11:19