关于接口的实现 ( 积分: 100 )

  • 主题发起人 主题发起人 zanpen2001
  • 开始时间 开始时间
Z

zanpen2001

Unregistered / Unconfirmed
GUEST, unregistred user!
最近在csdn上看了srw 的《用delphi实现文件下载的几种方法》,里面谈到urlmon这个单元封装了URLMON.DLL中的方法,其中有个接口是IBindStatusCallback,提供了根踪文件下载进度的方法,代码如下(我直接copy的):<br>
代码:
 &nbsp;<br>IBindStatusCallback = interface<br> &nbsp; &nbsp;['{79eac9c1-baf9-11ce-8c82-00aa004ba90b}']<br> &nbsp; &nbsp;function OnStartBinding(dwReserved: DWORD; pib: IBinding): HResult; stdcall;<br> &nbsp; &nbsp;function GetPriority(out nPriority): HResult; stdcall;<br> &nbsp; &nbsp;function OnLowResource(reserved: DWORD): HResult; stdcall;<br> &nbsp; &nbsp;function OnProgress(ulProgress, ulProgressMax, ulStatusCode: ULONG;<br> &nbsp; &nbsp; &nbsp;szStatusText: LPCWSTR): HResult; stdcall;<br> &nbsp; &nbsp;function OnStopBinding(hresult: HResult; szError: LPCWSTR): HResult; stdcall;<br> &nbsp; &nbsp;function GetBindInfo(out grfBINDF: DWORD; var bindinfo: TBindInfo): HResult; stdcall;<br> &nbsp; &nbsp;function OnDataAvailable(grfBSCF: DWORD; dwSize: DWORD; formatetc: PFormatEtc;<br> &nbsp; &nbsp; &nbsp;stgmed: PStgMedium): HResult; stdcall;<br> &nbsp; &nbsp;function OnObjectAvailable(const iid: TGUID; punk: IUnknown): HResult; stdcall;<br>
<br>我想实现其中的OnProgress方法,请教各位老题怎么做,请给出具体代码,无限感激!
 
最近在csdn上看了srw 的《用delphi实现文件下载的几种方法》,里面谈到urlmon这个单元封装了URLMON.DLL中的方法,其中有个接口是IBindStatusCallback,提供了根踪文件下载进度的方法,代码如下(我直接copy的):<br>
代码:
 &nbsp;<br>IBindStatusCallback = interface<br> &nbsp; &nbsp;['{79eac9c1-baf9-11ce-8c82-00aa004ba90b}']<br> &nbsp; &nbsp;function OnStartBinding(dwReserved: DWORD; pib: IBinding): HResult; stdcall;<br> &nbsp; &nbsp;function GetPriority(out nPriority): HResult; stdcall;<br> &nbsp; &nbsp;function OnLowResource(reserved: DWORD): HResult; stdcall;<br> &nbsp; &nbsp;function OnProgress(ulProgress, ulProgressMax, ulStatusCode: ULONG;<br> &nbsp; &nbsp; &nbsp;szStatusText: LPCWSTR): HResult; stdcall;<br> &nbsp; &nbsp;function OnStopBinding(hresult: HResult; szError: LPCWSTR): HResult; stdcall;<br> &nbsp; &nbsp;function GetBindInfo(out grfBINDF: DWORD; var bindinfo: TBindInfo): HResult; stdcall;<br> &nbsp; &nbsp;function OnDataAvailable(grfBSCF: DWORD; dwSize: DWORD; formatetc: PFormatEtc;<br> &nbsp; &nbsp; &nbsp;stgmed: PStgMedium): HResult; stdcall;<br> &nbsp; &nbsp;function OnObjectAvailable(const iid: TGUID; punk: IUnknown): HResult; stdcall;<br>
<br>我想实现其中的OnProgress方法,请教各位老题怎么做,请给出具体代码,无限感激!
 
Accepted Answer from Cynna <br>Date: 09/04/2002 05:15AM PDT<br>Grade: A<br> Accepted Answer &nbsp;<br><br><br>This is a simple class based on IBindStatusCallback. It's placed<br>in a separate unit. For demo purposes, OnProgress handler is implemented,<br>so that when download is started, progress will be shown in Form1 caption.<br>(I assumed you have Unit1/Form1).<br><br>1. Create new unit and overwrite it with this:<br><br><br>unit BindStatusCallback;<br>// Implementation of TBindStatusCallback<br><br>interface<br><br>uses SysUtils, Windows, UrlMon, ActiveX;<br><br>type TBindStatusCallback = class(TObject, IBindStatusCallback)<br> &nbsp;protected<br> &nbsp; &nbsp; FRefCount: Integer;<br> &nbsp; // IUnknown<br> &nbsp; &nbsp; function QueryInterface(const IID: TGUID; out Obj): Integer; stdcall;<br> &nbsp; &nbsp; function _AddRef: Integer; stdcall;<br> &nbsp; &nbsp; function _Release: Integer; stdcall;<br> &nbsp;public<br> &nbsp; // IBindStatusCallback<br> &nbsp; &nbsp;function OnStartBinding(dwReserved: DWORD; pib: IBinding): HResult; stdcall;<br> &nbsp; &nbsp;function GetPriority(out nPriority): HResult; stdcall;<br> &nbsp; &nbsp;function OnLowResource(reserved: DWORD): HResult; stdcall;<br> &nbsp; &nbsp;function OnProgress(ulProgress, ulProgressMax, ulStatusCode: ULONG;<br> &nbsp; &nbsp; &nbsp;szStatusText: LPCWSTR): HResult; stdcall;<br> &nbsp; &nbsp;function OnStopBinding(hresult: HResult; szError: LPCWSTR): HResult; stdcall;<br> &nbsp; &nbsp;function GetBindInfo(out grfBINDF: DWORD; var bindinfo: TBindInfo): HResult; stdcall;<br> &nbsp; &nbsp;function OnDataAvailable(grfBSCF: DWORD; dwSize: DWORD; formatetc: PFormatEtc;<br> &nbsp; &nbsp; &nbsp;stgmed: PStgMedium): HResult; stdcall;<br> &nbsp; &nbsp;function OnObjectAvailable(const iid: TGUID; punk: IUnknown): HResult; stdcall;<br> &nbsp;end;<br><br>implementation<br><br>{ TBindStatusCallback }<br><br>uses Unit1; // where Form1 is<br><br>function TBindStatusCallback.QueryInterface(const IID: TGUID;<br> &nbsp;out Obj): Integer;<br>begin<br> &nbsp;if GetInterface(IID, Obj) then Result := S_OK<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else Result := E_NOINTERFACE;<br>end;<br><br>function TBindStatusCallback._AddRef: Integer;<br>begin<br> Inc(FRefCount);<br> Result := FRefCount;<br>end;<br><br>function TBindStatusCallback._Release: Integer;<br>begin<br> Dec(FRefCount);<br> Result := FRefCount;<br>end;<br><br>function TBindStatusCallback.GetBindInfo(out grfBINDF: DWORD;<br> &nbsp;var bindinfo: TBindInfo): HResult;<br>begin<br> &nbsp;Result := E_NOTIMPL;<br>end;<br><br>function TBindStatusCallback.GetPriority(out nPriority): HResult;<br>begin<br> &nbsp;Result := E_NOTIMPL;<br>end;<br><br>function TBindStatusCallback.OnDataAvailable(grfBSCF, dwSize: DWORD;<br> &nbsp;formatetc: PFormatEtc; stgmed: PStgMedium): HResult;<br>begin<br> &nbsp;Result := E_NOTIMPL;<br>end;<br><br>function TBindStatusCallback.OnLowResource(reserved: DWORD): HResult;<br>begin<br> &nbsp;Result := E_NOTIMPL;<br>end;<br><br>function TBindStatusCallback.OnObjectAvailable(const iid: TGUID;<br> &nbsp;punk: IUnknown): HResult;<br>begin<br> &nbsp;Result := E_NOTIMPL;<br>end;<br><br>function TBindStatusCallback.OnStartBinding(dwReserved: DWORD;<br> &nbsp;pib: IBinding): HResult;<br>begin<br> &nbsp;Result := E_NOTIMPL;<br>end;<br><br>function TBindStatusCallback.OnStopBinding(hresult: HResult;<br> &nbsp;szError: LPCWSTR): HResult;<br>begin<br> &nbsp;Result := E_NOTIMPL;<br>end;<br><br>function TBindStatusCallback.OnProgress(ulProgress, ulProgressMax,<br> &nbsp;ulStatusCode: ULONG; szStatusText: LPCWSTR): HResult;<br>begin<br> &nbsp;// Do something on main form- for example, show progress:<br> &nbsp;Form1.Caption:='Downloaded: '+IntToStr(ulProgress) +' bytes';<br> &nbsp;Result := S_OK;<br>end;<br><br><br>end.<br><br><br><br>-------------------------------------------------------------------------------<br><br>2. In Unit1 :<br><br><br>uses ...., BindStatusCallback, UrlMon;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br><br> &nbsp;// ... blah, blah ...<br><br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br> &nbsp;bsc: TBindStatusCallback; // in unit BindStatusCallback<br><br> &nbsp;// ... your code ...<br><br> &nbsp; &nbsp;procedure TForm1.FormCreate(Sender: TObject);<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;bsc:=TBindStatusCallback.Create;<br> &nbsp; &nbsp;end;<br><br> &nbsp;// ... your code ...<br><br> &nbsp;// DEMO:<br> &nbsp;//----------<br> &nbsp;procedure TForm1.Button1Click(Sender: TObject);<br> &nbsp;begin<br> &nbsp; &nbsp; URLDownloadToFile(nil,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20349921.html',<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'c:/ee.html', 0 , bsc);<br> &nbsp;end;
 
谢谢老师!我试过了,可以工作。
 
后退
顶部