Hjælp -= HASTER =-
Hej eksperter,Jeg har fundet følgende sourcecode:
unit frmUpdate;
interface
uses
Windows, URLMon, ActiveX, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleCtrls, SHDocVw, ComCtrls, ExtCtrls, Buttons, Gauges;
type
TUpdateForm = class(TForm)
Label1: TLabel;
Label2: TLabel;
StartBtn: TSpeedButton;
Label3: TLabel;
TotalLbl: TLabel;
ProgressBar: TGauge;
procedure StartBtnClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TStatusCallback = class(TInterfacedObject, IBindStatusCallback)
private
FDest : TStream;
FProgress : TGauge;
protected
{ IBindStatusCallback methods}
function OnStartBinding(dwReserved: DWORD; pib: IBinding): HResult; stdcall;
function GetPriority(out nPriority): HResult; stdcall;
function OnLowResource(reserved: DWORD): HResult; stdcall;
function OnProgress(ulProgress, ulProgressMax, ulStatusCode: ULONG;
szStatusText: LPCWSTR): HResult; stdcall;
function OnStopBinding(hresult: HResult; szError: LPCWSTR): HResult; stdcall;
function GetBindInfo(out grfBINDF: DWORD; var bindinfo: TBindInfo): HResult; stdcall;
function OnDataAvailable(grfBSCF: DWORD; dwSize: DWORD; formatetc: PFormatEtc;
stgmed: PStgMedium): HResult; stdcall;
function OnObjectAvailable(const iid: TGUID; punk: IUnknown): HResult; stdcall;
public
constructor Create(const AFileName : string; AProgress: TGauge);
destructor Destroy; override;
end;
var
UpdateForm: TUpdateForm;
implementation
{$R *.dfm}
{ TStatusCallback }
constructor TStatusCallback.Create(const AFileName: string;
AProgress: TGauge);
begin
inherited Create;
FDest := TFileStream.Create(AFileName, fmOpenRead or fmCreate);
FProgress := AProgress;
end;
destructor TStatusCallback.Destroy;
begin
FDest.Free;
inherited;
end;
function TStatusCallback.GetBindInfo(out grfBINDF: DWORD;
var bindinfo: TBindInfo): HResult;
begin
Result := S_OK;
end;
function TStatusCallback.GetPriority(out nPriority): HResult;
begin
Result := S_OK;
end;
function TStatusCallback.OnDataAvailable(grfBSCF, dwSize: DWORD;
formatetc: PFormatEtc; stgmed: PStgMedium): HResult;
var
Buffer : Pointer;
BytesRead : Integer;
begin
if formatetc^.tymed <> TYMED_ISTREAM then
begin
Result := E_INVALIDARG;
Exit;
end;
GetMem(Buffer, dwSize);
try
IStream(stgmed^.stm).Read(Buffer, dwSize, @BytesRead);
FDest.Write(Buffer^, BytesRead);
finally
FreeMem(Buffer, dwSize);
end;
Result := S_OK;
end;
function TStatusCallback.OnLowResource(reserved: DWORD): HResult;
begin
Result := S_OK;
end;
function TStatusCallback.OnObjectAvailable(const iid: TGUID;
punk: IInterface): HResult;
begin
Result := S_OK;
end;
function TStatusCallback.OnProgress(ulProgress, ulProgressMax,
ulStatusCode: ULONG; szStatusText: LPCWSTR): HResult;
begin
Result := S_OK;
with FProgress do
begin
UpdateForm.TotalLbl.Caption := IntToStr(ulProgress) + '/' + IntToStr(ulProgressMax) + ' bytes';
if ulProgressMax = 0 then
Exit;
UpdateForm.TotalLbl.Caption := IntToStr(ulProgress);
Progress := Trunc((ulProgress / ulProgressMax) * 100);
end;
end;
function TStatusCallback.OnStartBinding(dwReserved: DWORD;
pib: IBinding): HResult;
begin
Result := S_OK;
end;
function TStatusCallback.OnStopBinding(hresult: HResult;
szError: LPCWSTR): HResult;
begin
with FProgress do
Progress := 100;
Result := S_OK;
end;
procedure TUpdateForm.StartBtnClick(Sender: TObject);
const
URL = 'http://www.worldcraftguide.1go.dk/sprites.zip';
var
AppPath : string;
Downloads : string;
begin
AppPath := ExtractFilePath(ParamStr(0));
ForceDirectories(AppPath + 'Downloads');
Downloads := AppPath + 'DownLoads\';
URLOpenStream(nil, URL, 0, TStatusCallback.Create(Downloads + 'Test.zip', ProgressBar));
end;
end.
---------
Men den viser ikke hvor meget den har hentet fx en label hvor der står (hvis filen fylder 1 mb) når den er nået halvvejs "512/1024 byted". Hvordan får man den til det???
