<br>下面是我的一篇稿件,是关于添加一个ComboBox到IE任务栏中的,里面的图片我删除了,如果对你有帮助,别忘了给分
<br><br>我们知道一些Internet软件能将自身功能集成在IE中,象网络蚂蚁。当设置了浏览器点击整合以后。如果用户点击IE页面中的指向.Exe、.Zip文件的连接后,蚂蚁会自动启动,下载连接指向地址的文件。这就是利用了IE扩展的功能实现的。<br>实现IE扩展的基本方法如下:建立一个COM服务器,编写代码使COM对象实现系统规定的若干接口(这些接口在Delphi的库中已经定义好了)。然后注册服务器并将COM对象的信息写入系统注册表中规定的位置。IE在运行时会通过注册表信息调用相应的COM对象中的方法从而实现IE扩展。<br>在下面的实例中,将建立一个IE工具栏(Explorer Bars)。在工具栏中放置一个按钮和一个下拉框,当用户点击按钮后,程序会自动获取IE页面中的所有电子邮件地址连接并将它添加到下拉列表框中。在这个范例内可以学习到如何在一个COM服务器中建立多个COM对象、在COM对象中实现多接口以及一些InternetExplorer对象的编程方法。<br><br>需要实现的接口<br>实现添加工具栏功能的COM对象需要实现以下的接口:<br>IDeskBand <br>IObjectWithSite <br>IPersistStream <br><br>IDeskBand接口用于处理工具栏,例如工具栏大小的改变,用户选择显示或隐藏工具栏等。接口在ShlObj.pas下有定义。<br>IObjectWithSite接口用于处理包含工具栏的对象, 在ActiveX.pas下有定义。<br>IPersistStream接口用于处理附加的信息。该接口并不需要,所以所有的方法都返回E_NOTIMPL,接口在ActiveX.pas下有定义。<br>这些接口的详细信息在微软的MSDN中都有详细的描述以及范例。<br><br>提示<br>MSDN(Microsoft Develop Network)是微软提供的Windows开发手册,也是最完整和翔实的Windows开发手册,包含了开发微软各个系统的帮助,Bug列表,范例等等。对于Windows下的程序员,无论是否使用微软的开发工具,都应该在自己的机器中安装MSDN。<br> <br>IE扩展的实现<br>同上面建立COM服务器一样,我们首先要建立一个ActiveX Library。将其保存为MailIEBand.Dpr;然后建立一个COM Object,将其保存为BandUnit.pas;然后建立一个Form,这个窗口将作为子窗口显示在IE工具栏中,将窗口的BorderStyle属性改为bsNone,添加一个TButton组件和一个TComboBox组件,将TButton的Caption属性改为获取全部,然后将窗口文件其保存为IEForm.pas。<br>在BandUnit中,需要建立一个实现上面提到的接口的TComObject对象。如下:<br>TGetMailBand = class(TComObject, IDeskBand, IObjectWithSite, IPersistStreamInit)<br><br>另外由于需要在COM服务器注册时添加一些注册表信息,所以还需要建立一个继承自TComObjectFactory类的对象,在对象的UpdateRegistry事件中编写代码添加附加的注册表信息。<br>下面的程序清单3-1到3-3是实现COM服务器的全部程序代码:<br><br>程序清单3-1 MailIEBand.dpr<br>library MailIEBand;<br><br>uses<br> ComServ,<br> BandUnit in 'BandUnit.pas',<br> IEForm in 'IEForm.pas' {Form1},<br> MailIEBand_TLB in 'MailIEBand_TLB.pas';<br><br>exports<br> DllGetClassObject,<br> DllCanUnloadNow,<br> DllRegisterServer,<br> DllUnregisterServer;<br><br>{$R *.TLB}<br><br>{$R *.RES}<br><br>begin<br>end.<br><br>程序清单3-2 BandUnit.pas<br><br>unit BandUnit;<br><br>interface<br><br>uses<br> Windows, Sysutils, Messages, Registry, Shellapi, ActiveX, Classes, ComObj,<br> Shlobj, Dialogs, Commctrl,ShDocVW,IEForm;<br><br>type<br> TGetMailBand = class(TComObject, IDeskBand, IObjectWithSite, IPersistStreamInit)<br> private<br> frmIE:TForm1;<br> m_pSite:IInputObjectSite;<br> m_hwndParent:HWND;<br> m_hWnd:HWND;<br> m_dwViewMode:Integer;<br> m_dwBandID:Integer;<br> protected<br><br> public<br> {Declare IDeskBand methods here}<br> function GetBandInfo(dwBandID, dwViewMode: DWORD; var pdbi: TDeskBandInfo):<br> HResult; stdcall;<br> function ShowDW(fShow: BOOL): HResult; stdcall;<br> function CloseDW(dwReserved: DWORD): HResult; stdcall;<br> function ResizeBorderDW(var prcBorder: TRect; punkToolbarSite: IUnknown;<br> fReserved: BOOL): HResult; stdcall;<br> function GetWindow(out wnd: HWnd): HResult; stdcall;<br> function ContextSensitiveHelp(fEnterMode: BOOL): HResult; stdcall;<br><br> {Declare IObjectWithSite methods here}<br> function SetSite(const pUnkSite: IUnknown ):HResult; stdcall;<br> function GetSite(const riid: TIID; out site: IUnknown):HResult;stdcall;<br><br> {Declare IPersistStream methods here}<br> function GetClassID(out classID: TCLSID): HResult; stdcall;<br> function IsDirty: HResult; stdcall;<br> function InitNew: HResult; stdcall;<br> function Load(const stm: IStream): HResult; stdcall;<br> function Save(const stm: IStream; fClearDirty: BOOL): HResult; stdcall;<br> function GetSizeMax(out cbSize: Largeint): HResult; stdcall;<br> end;<br><br>const<br> Class_GetMailBand: TGUID = '{954F618B-0DEC-4D1A-9317-E0FC96F87865}';<br> //以下是系统接口的IID<br> IID_IUnknown: TGUID = (<br> D1:$00000000;D2:$0000;D3:$0000;D4
$C0,$00,$00,$00,$00,$00,$00,$46));<br> IID_IOleObject: TGUID = (<br> D1:$00000112;D2:$0000;D3:$0000;D4
$C0,$00,$00,$00,$00,$00,$00,$46));<br> IID_IOleWindow: TGUID = (<br> D1:$00000114;D2:$0000;D3:$0000;D4
$C0,$00,$00,$00,$00,$00,$00,$46));<br><br> IID_IInputObjectSite : TGUID = (<br> D1:$f1db8392;D2:$7331;D3:$11d0;D4
$8C,$99,$00,$A0,$C9,$2D,$BF,$E8));<br> sSID_SInternetExplorer : TGUID = '{0002DF05-0000-0000-C000-000000000046}';<br> sIID_IWebBrowserApp : TGUID= '{0002DF05-0000-0000-C000-000000000046}';<br><br> //面板所允许的最小宽度和高度。<br> MIN_SIZE_X = 54;<br> MIN_SIZE_Y = 22;<br> EB_CLASS_NAME = 'GetMailAddress';<br>implementation<br><br>uses ComServ;<br><br><br>function TGetMailBand.GetWindow(out wnd: HWnd): HResult; stdcall;<br>begin<br> wnd:=m_hWnd;<br> Result:=S_OK;<br>end;<br><br>function TGetMailBand.ContextSensitiveHelp(fEnterMode: BOOL): HResult; stdcall;<br>begin<br> Result:=E_NOTIMPL;<br>end;<br><br>function TGetMailBand.ShowDW(fShow: BOOL): HResult; stdcall;<br>begin<br> if m_hWnd<>0 then<br> if fShow then<br> ShowWindow(m_hWnd,SW_SHOW)<br> else<br> ShowWindow(m_hWnd,SW_HIDE);<br> Result:=S_OK;<br>end;<br><br>function TGetMailBand.CloseDW(dwReserved: DWORD): HResult; stdcall;<br>begin<br> if frmIE<>nil then<br> frmIE.Destroy;<br> Result:= S_OK;<br>end;<br><br>function TGetMailBand.ResizeBorderDW(var prcBorder: TRect;<br> punkToolbarSite: IUnknown;fReserved: BOOL): HResult; stdcall;<br>begin<br> Result:=E_NOTIMPL;<br>end;<br><br>function TGetMailBand.SetSite(const pUnkSite: IUnknown):HResult;stdcall;<br>var<br> pOleWindow:IOleWindow;<br> pOLEcmd:IOleCommandTarget;<br> pSP:IServiceProvider;<br> rc:TRect;<br>begin<br> if Assigned(pUnkSite) then begin<br> m_hwndParent := 0;<br><br> m_pSite:=pUnkSite as IInputObjectSite;<br> pOleWindow := PunkSIte as IOleWindow;<br> //获得父窗口IE面板窗口的句柄<br> pOleWindow.GetWindow(m_hwndParent);<br><br> if(m_hwndParent=0)then begin<br> Result := E_FAIL;<br> exit;<br> end;<br><br> //获得父窗口区域<br> GetClientRect(m_hwndParent, rc);<br><br> if not Assigned(frmIE) then begin<br> //建立TIEForm窗口,父窗口为m_hwndParent<br> frmIE:=TForm1.CreateParented(m_hwndParent);<br><br> m_Hwnd:=frmIE.Handle;<br><br> SetWindowLong(frmIE.Handle, GWL_STYLE, GetWindowLong(frmIE.Handle,<br> GWL_STYLE) Or WS_CHILD);<br> //根据父窗口区域设置窗口位置<br> with frmIE do begin<br> Left :=rc.Left ;<br> Top:=rc.top;<br> Width:=rc.Right - rc.Left;<br> Height:=rc.Bottom - rc.Top;<br> end;<br> frmIE.Visible := True;<br><br> //获得与浏览器相关联的Webbrowser对象。<br> pOLEcmd:=pUnkSite as IOleCommandTarget;<br> pSP:=pOLEcmd as IServiceProvider;<br><br> if Assigned(pSP)then begin<br> pSP.QueryService(IWebbrowserApp, IWebbrowser2,frmIE.IEThis);<br> end;<br> end;<br> end;<br><br> Result := S_OK;<br>end;<br><br>function TGetMailBand.GetSite(const riid: TIID; out site: IUnknown):HResult;stdcall;<br>begin<br> if Assigned(m_pSite) then result:=m_pSite.QueryInterface(riid, site)<br> else<br> Result:= E_FAIL;<br>end;<br><br>function TGetMailBand.GetBandInfo(dwBandID, dwViewMode: DWORD; var pdbi: TDeskBandInfo):<br> HResult; stdcall;<br>begin<br> Result:=E_INVALIDARG;<br> if not Assigned(frmIE) then frmIE:=TForm1.CreateParented(m_hwndParent);<br> if(@pdbi<>nil)then begin<br> m_dwBandID := dwBandID;<br> m_dwViewMode := dwViewMode;<br><br> if(pdbi.dwMask and DBIM_MINSIZE)<>0 then begin<br> pdbi.ptMinSize.x := MIN_SIZE_X;<br> pdbi.ptMinSize.y := MIN_SIZE_Y;<br> end;<br><br> if(pdbi.dwMask and DBIM_MAXSIZE)<>0 then begin<br> pdbi.ptMaxSize.x := -1;<br> pdbi.ptMaxSize.y := -1;<br> end;<br><br> if(pdbi.dwMask and DBIM_INTEGRAL)<>0 then begin<br> pdbi.ptIntegral.x := 1;<br> pdbi.ptIntegral.y := 1;<br> end;<br><br> if(pdbi.dwMask and DBIM_ACTUAL)<>0 then begin<br> pdbi.ptActual.x := 0;<br> pdbi.ptActual.y := 0;<br> end;<br><br> if(pdbi.dwMask and DBIM_MODEFLAGS)<>0 then<br> pdbi.dwModeFlags := DBIMF_VARIABLEHEIGHT;<br><br> if(pdbi.dwMask and DBIM_BKCOLOR)<>0 then<br> pdbi.dwMask := pdbi.dwMask and (not DBIM_BKCOLOR);<br> end;<br>end;<br><br><br>function TGetMailBand.GetClassID(out classID: TCLSID): HResult; stdcall;<br>begin<br> classID:= Class_GetMailBand;<br> Result:=S_OK;<br>end;<br><br>function TGetMailBand.IsDirty: HResult; stdcall;<br>begin<br> Result:=S_FALSE;<br>end;<br><br>function TGetMailBand.InitNew: HResult;<br>begin<br> Result := E_NOTIMPL;<br>end;<br><br>function TGetMailBand.Load(const stm: IStream): HResult; stdcall;<br>begin<br> Result:=S_OK;<br>end;<br><br>function TGetMailBand.Save(const stm: IStream; fClearDirty: BOOL): HResult; stdcall;<br>begin<br> Result:=S_OK;<br>end;<br><br>function TGetMailBand.GetSizeMax(out cbSize: Largeint): HResult; stdcall;<br>begin<br> Result:=E_NOTIMPL;<br>end;<br><br><br>//TIEClassFac类实现COM组件的注册<br>type<br> TIEClassFac=class(TComObjectFactory) //<br> public<br> procedure UpdateRegistry(Register: Boolean); override;<br> end;<br><br>procedure TIEClassFac.UpdateRegistry(Register: Boolean);<br>var<br> ClassID: string;<br> a:Integer;<br>begin<br> inherited UpdateRegistry(Register);<br> if Register then begin<br> ClassID:=GUIDToString(Class_GetMailBand);<br> with TRegistry.Create do<br> try<br> //添加附加的注册表项<br> RootKey:=HKEY_LOCAL_MACHINE;<br> OpenKey('/SOFTWARE/Microsoft/Internet Explorer/Toolbar',False);<br> a:=0;<br> WriteBinaryData(GUIDToString(Class_GetMailBand),a,0);<br> OpenKey('/SOFTWARE/Microsoft/Windows/CurrentVersion/Shell Extensions/Approved',True);<br> WriteString (GUIDToString(Class_GetMailBand),EB_CLASS_NAME);<br> RootKey:=HKEY_CLASSES_ROOT;<br> OpenKey('/CLSID/'+GUIDToString(Class_GetMailBand),False);<br> WriteString('',EB_CLASS_NAME);<br> finally<br> Free;<br> end;<br> end<br> else begin<br> with TRegistry.Create do<br> try<br> RootKey:=HKEY_LOCAL_MACHINE;<br> OpenKey('/SOFTWARE/Microsoft/Internet Explorer/Toolbar',False);<br> DeleteValue(GUIDToString(Class_GetMailBand));<br> OpenKey('/Software/Microsoft/Windows/CurrentVersion/Shell Extensions/Approved',False);<br> DeleteValue(GUIDToString(Class_GetMailBand));<br> finally<br> Free;<br> end;<br> end;<br>end;<br><br>initialization<br> TIEClassFac.Create(ComServer, TGetMailBand, Class_GetMailBand,<br> 'GetMailAddress', '', ciMultiInstance, tmApartment);<br>end.<br><br>程序清单3-3 IEForm.pas<br><br>unit IEForm;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> SHDocVw,MSHTML, StdCtrls;<br><br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> ComboBox1: TComboBox;<br> procedure FormResize(Sender: TObject);<br> procedure Button1Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> IEThis:IWebbrowser2;<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.FormResize(Sender: TObject);<br>begin<br> With Button1 do begin<br> Left := 0;<br> Top := 0;<br> Height:=Self.ClientHeight;<br> end;<br> With ComboBox1 do begin<br> Left := Button1.Width +3;<br> Top := 0;<br> Height:=Self.ClientHeight;<br> Width:=Self.ClientWidth - Left;<br> end;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> doc:IHTMLDocument2;<br> all:IHTMLElementCollection;<br> len,i,flag:integer;<br> item:IHTMLElement;<br> vAttri:Variant;<br>begin<br> if Assigned(IEThis)then begin<br> ComboBox1.Clear;<br> //获得Webbrowser对象中的文档对象<br> doc:=IEThis.Document as IHTMLDocument2;<br> //获得文档中所有的HTML元素集合<br> all:=doc.Get_all;<br><br> len:=all.Get_length;<br><br> //访问HTML元素集合中的每一个元素<br> for i:=0 to len-1 do begin<br> item:=all.item(i,varempty) as IHTMLElement;<br> //如果该元素是一个链接<br> if item.Get_tagName = 'A'then begin<br> flag:=0;<br> vAttri:=item.getAttribute('protocol',flag); //获得链接属性<br> //如果是mailto链接则将链接的目标地址添加到ComboBox1<br> if vAttri = 'mailto:'then begin<br> vAttri:=item.getAttribute('href',flag);<br> ComboBox1.Items.Add(vAttri);<br> end;<br> end;<br> end;<br> end;<br>end;<br><br>end.<br><br>编译工程,关闭所有的IE窗口,然后点击Delphi菜单的Run | Register ActiveX Server 项注册服务器。然后打开IE,点击菜单 察看 | 工具栏 项,可以看到子菜单中多了一个GetMailAddress项,选中改项,工具栏就出现在IE工具栏中,如图:<br><br> <br>图1-6 IE工具栏<br><br>点击 获取地址 按钮,就可以将当前页面中的Mail地址统统添加到ComboBox1中。<br>注意上面的IEForm.pas引用到了MSHTML,微软IE的整个结构如下图所示:<br> <br>图1-7 IE基本架构<br><br>其中MSHTML是位于SHDOCVW和HTML页面之间的对象。SHDOCVW对象用于处理页面的显示,而MSHTML用于处理页面的语法分析。它可以将页面中的标记(例如<P></P>、< A href></A>)转换为元素,同时它MSHTML又是一个COM服务器,允许客户端访问。所以上面TForm1.Button1Click部分的代码:<br><br>if Assigned(IEThis)then begin<br> ComboBox1.Clear;<br> //获得Webbrowser对象中的文档对象<br> doc:=IEThis.Document as IHTMLDocument2;<br> //获得文档中所有的HTML元素集合<br> all:=doc.Get_all;<br><br> len:=all.Get_length;<br><br> //访问HTML元素集合中的每一个元素<br> for i:=0 to len-1 do begin<br> item:=all.item(i,varempty) as IHTMLElement;<br> //如果该元素是一个链接<br> if item.Get_tagName = 'A'then begin<br> flag:=0;<br> vAttri:=item.getAttribute('protocol',flag); //获得链接属性<br> //如果是mailto链接则将链接的目标地址添加到ComboBox1<br> if vAttri = 'mailto:'then begin<br> vAttri:=item.getAttribute('href',flag);<br> ComboBox1.Items.Add(vAttri);<br> end;<br> end;<br> end;<br> end;<br>end;<br><br>就是通过MSHTML定义的接口访问页面中的元素并获得链接元素中的地址。<br><br>