修改window.external使JavaScript可以调用Delphi内定义的方法
在JavaScript中,有一个比较特殊的对象,即window.external,用它可以调用浏览器提供的外部方法
一个很简单的例子就是将当前页添加到收藏夹
window.external.addFavorite(https://www.baidu.com/,”XX的百度’);
这样写脚本就可以了。
那么如果我想自己定义external,以便在自己的软件内使用IE核心的浏览器作为UI容器,该如何做呢?
本文即是解决此问题。
一、制作TLB
在File | New | Other 菜单下,选择新建一个Type Library,这个向导在ActiveX页内。
然后按下图所示,新建一个接口,在接口下新建一个DoSearchData方法,这个方法即是将来需要添加到external中的。
完成添加后,点击保存为TLB按钮,将生成一个TLB文件,此处我将它命名为GetData.tlb
二、实现IDocHostUIHandler接口
这部分相对比较简单,从MSDN上找到相关的C++代码,把它转换成Delphi的即可。代码如下:
01 unit DocHostUIHandler;
02
03 interface
04
05 uses
06 Windows, ActiveX;
07 const
08 DOCHOSTUIFLAG_DIALOG = $00000001;
09 DOCHOSTUIFLAG_DISABLE_HELP_MENU = $00000002;
10 DOCHOSTUIFLAG_NO3DBORDER = $00000004;
11 DOCHOSTUIFLAG_SCROLL_NO = $00000008;
12 DOCHOSTUIFLAG_DISABLE_SCRIPT_INACTIVE = $00000010;
13 DOCHOSTUIFLAG_OPENNEWWIN = $00000020;
14 DOCHOSTUIFLAG_DISABLE_OFFSCREEN = $00000040;
15 DOCHOSTUIFLAG_FLAT_SCROLLBAR = $00000080;
16 DOCHOSTUIFLAG_DIV_BLOCKDEFAULT = $00000100;
17 DOCHOSTUIFLAG_ACTIVATE_CLIENTHIT_ONLY = $00000200;
18 DOCHOSTUIFLAG_OVERRIDEBEHAVIORFACTORY = $00000400;
19 DOCHOSTUIFLAG_CODEPAGELINKEDFONTS = $00000800;
20 DOCHOSTUIFLAG_URL_ENCODING_DISABLE_UTF8 = $00001000;
21 DOCHOSTUIFLAG_URL_ENCODING_ENABLE_UTF8 = $00002000;
22 DOCHOSTUIFLAG_ENABLE_FORMS_AUTOCOMPLETE = $00004000;
23 DOCHOSTUIFLAG_ENABLE_INPLACE_NAVIGATION = $00010000;
24 DOCHOSTUIFLAG_IME_ENABLE_RECONVERSION = $00020000;
25 DOCHOSTUIFLAG_THEME = $00040000;
26 DOCHOSTUIFLAG_NOTHEME = $00080000;
27 DOCHOSTUIFLAG_NOPICS = $00100000;
28 DOCHOSTUIFLAG_NO3DOUTERBORDER = $00200000;
29 DOCHOSTUIFLAG_DISABLE_EDIT_NS_FIXUP = $1;
30 DOCHOSTUIFLAG_LOCAL_MACHINE_ACCESS_CHECK = $1;
31 DOCHOSTUIFLAG_DISABLE_UNTRUSTEDPROTOCOL = $1;
32 DOCHOSTUIDBLCLK_DEFAULT = 0;
33 DOCHOSTUIDBLCLK_SHOWPROPERTIES = 1;
34 DOCHOSTUIDBLCLK_SHOWCODE = 2;
35 DOCHOSTUITYPE_BROWSE = 0;
36 DOCHOSTUITYPE_AUTHOR = 1;
37
38 type
39 TDocHostUIInfo = record
40 cbSize: ULONG;
41 dwFlags: DWORD;
42 dwDoubleClick: DWORD;
43 pchHostCss: PWChar;
44 pchHostNS: PWChar;
45 end;
46
47 PDocHostUIInfo = ^TDocHostUIInfo;
48 IDocHostUIHandler = interface(IUnknown)
49 ['{bd3f23c0-d43e-11cf-893b-00aa00bdce1a}']
50 function ShowContextMenu(const dwID: DWORD; const ppt: PPOINT;
51 const pcmdtReserved: IUnknown; const pdispReserved: IDispatch): HResult;
52 stdcall;
53 function GetHostInfo(var pInfo: TDocHostUIInfo): HResult; stdcall;
54 function ShowUI(const dwID: DWORD;
55 const pActiveObject: IOleInPlaceActiveObject;
56 const pCommandTarget: IOleCommandTarget; const pFrame: IOleInPlaceFrame;
57 const pDoc: IOleInPlaceUIWindow): HResult; stdcall;
58 function HideUI: HResult; stdcall;
59 function UpdateUI: HResult; stdcall;
60 function EnableModeless(const fEnable: BOOL): HResult; stdcall;
61 function OnDocWindowActivate(const fActivate: BOOL): HResult; stdcall;
62 function OnFrameWindowActivate(const fActivate: BOOL): HResult; stdcall;
63 function ResizeBorder(const prcBorder: PRECT;
64 const pUIWindow: IOleInPlaceUIWindow; const fFrameWindow: BOOL): HResult;
65 stdcall;
66 function TranslateAccelerator(const lpMsg: PMSG; const pguidCmdGroup: PGUID;
67