unit1:
unit Unit1;
interface
uses
Windows, URLMon, ActiveX, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleCtrls, SHDocVw, ComCtrls, ExtCtrls;
type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
GroupBox3: TGroupBox;
WebBrowser1: TWebBrowser;
Button7: TButton;
Button8: TButton;
Button9: TButton;
Button10: TButton;
Button11: TButton;
ProgressBar1: TProgressBar;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
procedure Button9Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button10Click(Sender: TObject);
procedure Button11Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Unit2, Unit3;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Form2.ShowModal
end;
procedure TForm1.Button8Click(Sender: TObject);
begin
Close;
end;
procedure TForm1.Button9Click(Sender: TObject);
begin
WebBrowser1.Stop;
end;
procedure TForm1.Button7Click(Sender: TObject);
begin
Webbrowser1.Navigate('
http://www.eksperten.dk');
end;
procedure TForm1.Button10Click(Sender: TObject);
begin
Form3.ShowModal
end;
procedure TForm1.Button11Click(Sender: TObject);
begin
WebBrowser1.Refresh;
end;
procedure TForm1.Button4Click(Sender: TObject);
type
TForm1 = class(TForm)
ProgressBar1: TProgressBar;
Button4: TButton;
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TStatusCallback = class(TInterfacedObject, IBindStatusCallback)
private
FDest : TStream;
FProgress : TProgressBar;
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: TProgressBar);
destructor Destroy; override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TStatusCallback }
constructor TStatusCallback.Create(const AFileName: string;
AProgress: TProgressBar);
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
if ulProgressMax = 0 then
Exit;
Position := Trunc((ulProgress / ulProgressMax) * Max);
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
Position := Max;
Result := S_OK;
end;
procedure TForm1.Button4Click(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', ProgressBar1));
end;
end;
procedure TForm1.Button6Click(Sender: TObject);
const
BufSize = 1024; //1 KB
var
AppPath : string;
Downloads : string;
Stm : IStream;
Buffer : Pointer;
BytesRead : Integer;
TotalBytesRead : Integer;
Dest : TStream;
Stat : TStatStg;
begin
AppPath := ExtractFilePath(ParamStr(0));
ForceDirectories(AppPath + 'Downloads');
Downloads := AppPath + 'DownLoads\';
URLOpenBlockingStream(nil, '
http://www.worldcraftguide.1go.dk/sprites.zip',
Stm, 0, nil);
if Stm = nil then
Exit;
ProgressBar1.Position := 0;
Stm.Stat(Stat, 0);
TotalBytesRead := 0;
Dest := TFileStream.Create(Downloads + 'Test.zip', fmOpenRead or fmCreate);
GetMem(Buffer, BufSize);
try
repeat
Stm.Read(Buffer, BufSize, @BytesRead);
Dest.Write(Buffer^, BytesRead);
Inc(TotalBytesRead, BytesRead);
ProgressBar1.Position := Trunc((TotalBytesRead / Stat.cbSize) * ProgressBar1.Max);
Application.ProcessMessages;
if ProgressBar1.Position = 100 then
ShowMessage('Finen er hentet') else
timer1.enabled := true
until BytesRead <> BufSize;
finally
FreeMem(Buffer, BufSize);
Dest.Free;
end;
end;
procedure TForm1.Button5Click(Sender: TObject);
const
BufSize = 10024; //1 KB
var
AppPath : string;
Downloads : string;
Stm : IStream;
Buffer : Pointer;
BytesRead : Integer;
TotalBytesRead : Integer;
Dest : TStream;
Stat : TStatStg;
begin
AppPath := ExtractFilePath(ParamStr(0));
ForceDirectories(AppPath + 'Downloads');
Downloads := AppPath + 'DownLoads\';
URLOpenBlockingStream(nil, '
http://www.worldcraftguide.1go.dk/worldcraft3.exe',
Stm, 0, nil);
if Stm = nil then
Exit;
ProgressBar1.Position := 0;
Stm.Stat(Stat, 0);
TotalBytesRead := 0;
Dest := TFileStream.Create(Downloads + 'worldcraft3.exe', fmOpenRead or fmCreate);
GetMem(Buffer, BufSize);
try
repeat
Stm.Read(Buffer, BufSize, @BytesRead);
Dest.Write(Buffer^, BytesRead);
Inc(TotalBytesRead, BytesRead);
ProgressBar1.Position := Trunc((TotalBytesRead / Stat.cbSize) * ProgressBar1.Max);
Application.ProcessMessages;
if ProgressBar1.Position = 100 then
timer1.enabled := true else
timer1.enabled := false
until BytesRead <> BufSize;
finally
FreeMem(Buffer, BufSize);
Dest.Free;
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
ProgressBar1.Position :=0
end;
end.