uses
Windows, Sysutils, Messages, Registry, Shellapi, ActiveX, Classes, ComObj,
Shlobj, Dialogs, Commctrl, ShDocVW, MSHTML,IEForm, StdVcl, Controls;
type
TGetMailBand = class(TComObject, IDeskBand, IObjectWithSite, IPersistStreamInit, IDispatch)
private
frmIE: TForm1;
m_pSite: IInputObjectSite;
m_hwndParent: HWND;
m_hWnd: HWND;
m_dwViewMode: Integer;
m_dwBandID: Integer;
IsComplete: boolean;
protected
public
function GetTypeInfoCount(out Count:Integer):HResult;stdcall;
function GetTypeInfo(Index,LocaleID:Integer;out TypeInfo):HResult;stdcall;
function GetIDsOfNames(const IID:TGUID;Names
ointer;
NameCount,LocaleID:Integer;DispIDs
ointer):HResult;stdcall;
{声明IDeskBand方法}
function GetBandInfo(dwBandID, dwViewMode: DWORD; var pdbi: TDeskBandInfo):
HResult; stdcall;
function ShowDW(fShow: BOOL): HResult; stdcall;
function CloseDW(dwReserved: DWORD): HResult; stdcall;
function ResizeBorderDW(var prcBorder: TRect; punkToolbarSite: IUnknown;
fReserved: BOOL): HResult; stdcall;
function GetWindow(out wnd: HWnd): HResult; stdcall;
function ContextSensitiveHelp(fEnterMode: BOOL): HResult; stdcall;
{声明IObjectWithSite方法}
function SetSite(const pUnkSite: IUnknown): HResult; stdcall;
function GetSite(const riid: TIID; out site: IUnknown): HResult; stdcall;
{声明IPersistStream方法}
function GetClassID(out classID: TCLSID): HResult; stdcall;
function IsDirty: HResult; stdcall;
function InitNew: HResult; stdcall;
function Load(const stm: IStream): HResult; stdcall;
function Save(const stm: IStream; fClearDirty: BOOL): HResult; stdcall;
function GetSizeMax(out cbSize: Largeint): HResult; stdcall;
function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult; stdcall;
end;
const
Class_GetMailBand: TGUID = '{954F618B-0DEC-4D1A-9317-E0FC96F87865}';
{以下是系统接口的IID}
IID_IUnknown: TGUID = (
D1: $00000000; D2: $0000; D3: $0000; D4: ($C0, $00, $00, $00, $00, $00, $00, $46));
IID_IOleObject: TGUID = (
D1: $00000112; D2: $0000; D3: $0000; D4: ($C0, $00, $00, $00, $00, $00, $00, $46));
IID_IOleWindow: TGUID = (
D1: $00000114; D2: $0000; D3: $0000; D4: ($C0, $00, $00, $00, $00, $00, $00, $46));
IID_IInputObjectSite: TGUID = (
D1: $F1DB8392; D2: $7331; D3: $11D0; D4: ($8C, $99, $00, $A0, $C9, $2D, $BF, $E8));
sSID_SInternetExplorer: TGUID = '{0002DF05-0000-0000-C000-000000000046}';
sIID_IWebBrowserApp: TGUID = '{0002DF05-0000-0000-C000-000000000046}';
{工具面板所允许的最小宽度和高度}
MIN_SIZE_X = 250;
MIN_SIZE_Y = 22;
EB_CLASS_NAME = 'GetMailAddress'; {工具的类名}
setsite方法:
function TGetMailBand.SetSite(const pUnkSite: IUnknown): HResult; stdcall;//设置现场
var
pOleWindow: IOleWindow;
pOLEcmd: IOleCommandTarget;
pSP: IServiceProvider;
CPC:IConnectionPointContainer;
CP:IConnectionPoint;
rc: TRect;
Cookie:Integer;
begin
IsComplete := false;
//如果pUnkSite不为NULL, 则表示要建立一个新的现场
if Assigned(pUnkSite) then
begin
m_hwndParent := 0;
m_pSite := pUnkSite as IInputObjectSite;
pOleWindow := PunkSIte as IOleWindow;
{获得父窗口IE面板窗口的句柄}
pOleWindow.GetWindow(m_hwndParent);
if (m_hwndParent = 0) then
begin
Result := E_FAIL;
exit;
end;
{获得父窗口区域}
GetClientRect(m_hwndParent, rc);
if not Assigned(frmIE) then //如果没有建立主窗口
begin
{建立TIEForm窗口,父窗口为m_hwndParent}
frmIE := TForm1.CreateParented(m_hwndParent);
m_Hwnd := frmIE.Handle; //保存主窗口句柄
SetWindowLong(frmIE.Handle, GWL_STYLE, GetWindowLong(frmIE.Handle,
GWL_STYLE) or WS_CHILD); //子窗口的风格
{根据父窗口区域设置窗口位置}
with frmIE do
begin
Left := rc.Left;
Top := rc.top;
Width := rc.Right - rc.Left;
Height := rc.Bottom - rc.Top;
end;
frmIE.Visible := True; //显示主窗口
{获得与浏览器相关联的Webbrowser对象}
pOLEcmd := pUnkSite as IOleCommandTarget;
pSP := pOLEcmd as IServiceProvider;
if Assigned(pSP) then
begin
{检索提供的服务}
pSP.QueryService(IWebbrowserApp, IWebbrowser2, frmIE.IEThis);
if(Assigned(frmIE.IEThis))then
begin
//frmIE.IEThis.QueryInterface(IConnectionPointContainer,CPC);//寻找连接点
//CPC.FindConnectionPoint(DWEBBrowserEvents2,CP);
//CPC.FindConnectionPoint(HTMLDocumentEvents2,CP);
InterfaceConnect(frmIE.IEThis, HTMLDocumentEvents, Self, Cookie);
//CP.Advise(Self,Cookie);//通过Advise方法建立Com自身与连接点的连接
end;
end;
end;
end;
Result := S_OK;
end;