Firemonkey 和 TDownloadUrl

发布于 2024-12-05 14:36:50 字数 159 浏览 0 评论 0原文

我有一个(Delphi XE2)VCL应用程序,其中包含一个对象TDownloadUrl(VCL.ExtActns)来检查多个网页,所以我想知道FireMonkey中是否有等效的对象,因为我想利用这个新平台的丰富功能。

使用线程的 Firemonkey 应用程序演示将不胜感激。提前致谢。

I have an (Delphi XE2) VCL app containing an object TDownloadUrl (VCL.ExtActns) to check several webpages, so I wonder if there is an equivalent object in FireMonkey, 'cause I wanna take advantage of rich features from this new platform.

A Firemonkey app demo using threads would appreciate. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏末染殇 2024-12-12 14:36:50

FireMonkey 尚不存在操作。

顺便说一句,您可以使用如下代码创建相同的行为:

IdHTTP1: TIdHTTP;

...

procedure TForm2.MenuItem1Click(Sender: TObject);
const
  FILENAME = 'C:\Users\Whiler\Desktop\test.htm';
  URL      = 'http://stackoverflow.com/questions/7491389/firemonkey-and-tdownloadurl';
var
//  sSource: string;
  fsSource: TFileStream;
begin
  if FileExists(FILENAME) then
  begin
    fsSource := TFileStream.Create(FILENAME, fmOpenWrite);
  end
  else
  begin
    fsSource := TFileStream.Create(FILENAME, fmCreate);
  end;

  try
    IdHTTP1.Get(URL, fsSource);
  finally
    fsSource.Free;
  end;
//  sSource := IdHTTP1.Get(URL);
end;

如果您只需要内存中的源,则注释行可以替换其他行...



如果您想使用线程,您可以像这样管理它:

unit Unit2;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, FMX.Menus;

type
  TDownloadThread = class(TThread)
  private
    idDownloader: TIdHTTP;
    FFileName   : string;
    FURL        : string;
  protected
    procedure Execute; override;
    procedure Finished;
  public
    constructor Create(const sURL: string; const sFileName: string);
    destructor  Destroy; override;
  end;
type
  TForm2 = class(TForm)
    MenuBar1: TMenuBar;
    MenuItem1: TMenuItem;
    procedure MenuItem1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.fmx}

procedure TForm2.MenuItem1Click(Sender: TObject);
const
  FILENAME = 'C:\Users\Whiler\Desktop\test.htm';
  URL      = 'http://stackoverflow.com/questions/7491389/firemonkey-and-tdownloadurl';
var
//  sSource: string;
  fsSource: TFileStream;
begin
  TDownloadThread.Create(URL, FILENAME).Start;
end;

{ TDownloadThread }

constructor TDownloadThread.Create(const sURL, sFileName: string);
begin
  inherited Create(true);
  idDownloader := TIdHTTP.Create(nil);
  FFileName       := sFileName;
  FURL            := sURL;
  FreeOnTerminate := True;
end;

destructor TDownloadThread.Destroy;
begin
  idDownloader.Free;
  inherited;
end;

procedure TDownloadThread.Execute;
var
//  sSource: string;
  fsSource: TFileStream;
begin
  inherited;
  if FileExists(FFileName) then
  begin
    fsSource := TFileStream.Create(FFileName, fmOpenWrite);
  end
  else
  begin
    fsSource := TFileStream.Create(FFileName, fmCreate);
  end;

  try
    idDownloader.Get(FURL, fsSource);
  finally
    fsSource.Free;
  end;
  Synchronize(Finished);
end;

procedure TDownloadThread.Finished;
begin
  // replace by whatever you need
  ShowMessage(FURL + ' has been downloaded!');
end;

end.

Actions don't exist yet with FireMonkey.

BTW, you can create the same behavior with a code like this:

IdHTTP1: TIdHTTP;

...

procedure TForm2.MenuItem1Click(Sender: TObject);
const
  FILENAME = 'C:\Users\Whiler\Desktop\test.htm';
  URL      = 'http://stackoverflow.com/questions/7491389/firemonkey-and-tdownloadurl';
var
//  sSource: string;
  fsSource: TFileStream;
begin
  if FileExists(FILENAME) then
  begin
    fsSource := TFileStream.Create(FILENAME, fmOpenWrite);
  end
  else
  begin
    fsSource := TFileStream.Create(FILENAME, fmCreate);
  end;

  try
    IdHTTP1.Get(URL, fsSource);
  finally
    fsSource.Free;
  end;
//  sSource := IdHTTP1.Get(URL);
end;

The commented lines can replace the others if you just need the source in memory...



If you want to use a thread, you can manage it like this:

unit Unit2;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, FMX.Menus;

type
  TDownloadThread = class(TThread)
  private
    idDownloader: TIdHTTP;
    FFileName   : string;
    FURL        : string;
  protected
    procedure Execute; override;
    procedure Finished;
  public
    constructor Create(const sURL: string; const sFileName: string);
    destructor  Destroy; override;
  end;
type
  TForm2 = class(TForm)
    MenuBar1: TMenuBar;
    MenuItem1: TMenuItem;
    procedure MenuItem1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.fmx}

procedure TForm2.MenuItem1Click(Sender: TObject);
const
  FILENAME = 'C:\Users\Whiler\Desktop\test.htm';
  URL      = 'http://stackoverflow.com/questions/7491389/firemonkey-and-tdownloadurl';
var
//  sSource: string;
  fsSource: TFileStream;
begin
  TDownloadThread.Create(URL, FILENAME).Start;
end;

{ TDownloadThread }

constructor TDownloadThread.Create(const sURL, sFileName: string);
begin
  inherited Create(true);
  idDownloader := TIdHTTP.Create(nil);
  FFileName       := sFileName;
  FURL            := sURL;
  FreeOnTerminate := True;
end;

destructor TDownloadThread.Destroy;
begin
  idDownloader.Free;
  inherited;
end;

procedure TDownloadThread.Execute;
var
//  sSource: string;
  fsSource: TFileStream;
begin
  inherited;
  if FileExists(FFileName) then
  begin
    fsSource := TFileStream.Create(FFileName, fmOpenWrite);
  end
  else
  begin
    fsSource := TFileStream.Create(FFileName, fmCreate);
  end;

  try
    idDownloader.Get(FURL, fsSource);
  finally
    fsSource.Free;
  end;
  Synchronize(Finished);
end;

procedure TDownloadThread.Finished;
begin
  // replace by whatever you need
  ShowMessage(FURL + ' has been downloaded!');
end;

end.
醉南桥 2024-12-12 14:36:50

关于这一点:

使用线程的 Firemonkey 应用程序演示将不胜感激。

您可以在此处找到使用 Thread 的 FireMonkey 演示:https://radstudiodemos.svn.sourceforge.net/svnroot/radstudiodemos/branches/RadStudio_XE2/FireMonkey/FireFlow/MainForm.pas

type

  TImageThread = class(TThread)
  private
    FImage: TImage;
    FTempBitmap: TBitmap;
    FFileName: string;
  protected
    procedure Execute; override;
    procedure Finished;
  public
    constructor Create(const AImage: TImage; const AFileName: string);
    destructor Destroy; override;
  end;

...

TImageThread.Create(Image, Image.TagString).Start;

如果您的示例目录中没有此演示,您可以从上面链接中使用的 subversion 存储库中查看它。

Regarding this:

A Firemonkey app demo using threads would appreciate.

You can find a FireMonkey demo which is using Thread here: https://radstudiodemos.svn.sourceforge.net/svnroot/radstudiodemos/branches/RadStudio_XE2/FireMonkey/FireFlow/MainForm.pas

type

  TImageThread = class(TThread)
  private
    FImage: TImage;
    FTempBitmap: TBitmap;
    FFileName: string;
  protected
    procedure Execute; override;
    procedure Finished;
  public
    constructor Create(const AImage: TImage; const AFileName: string);
    destructor Destroy; override;
  end;

...

TImageThread.Create(Image, Image.TagString).Start;

if you don't have this demo in your sample directory, you can check it out from the subversion repository used in the link above.

﹏雨一样淡蓝的深情 2024-12-12 14:36:50

您可以使用此代码。

unit BitmapHelperClass;

interface

uses
  System.Classes, FMX.Graphics;

type
  TBitmapHelper = class helper for TBitmap
  public
    procedure LoadFromUrl(AUrl: string);

    procedure LoadThumbnailFromUrl(AUrl: string; const AFitWidth, AFitHeight: Integer);
  end;

implementation

uses
  System.SysUtils, System.Types, IdHttp, IdTCPClient, AnonThread;

procedure TBitmapHelper.LoadFromUrl(AUrl: string);
var
  _Thread: TAnonymousThread<TMemoryStream>;
begin
  _Thread := TAnonymousThread<TMemoryStream>.Create(
    function: TMemoryStream
    var
      Http: TIdHttp;
    begin
      Result := TMemoryStream.Create;
      Http := TIdHttp.Create(nil);
      try
        try
          Http.Get(AUrl, Result);
        except
          Result.Free;
        end;
      finally
        Http.Free;
      end;
    end,
    procedure(AResult: TMemoryStream)
    begin
      if AResult.Size > 0 then
        LoadFromStream(AResult);
      AResult.Free;
    end,
    procedure(AException: Exception)
    begin
    end
  );
end;

procedure TBitmapHelper.LoadThumbnailFromUrl(AUrl: string; const AFitWidth,
  AFitHeight: Integer);
var
  Bitmap: TBitmap;
  scale: Single;
begin
  LoadFromUrl(AUrl);
  scale := RectF(0, 0, Width, Height).Fit(RectF(0, 0, AFitWidth, AFitHeight));
  Bitmap := CreateThumbnail(Round(Width / scale), Round(Height / scale));
  try
    Assign(Bitmap);
  finally
    Bitmap.Free;
  end;
end;

end.

You can using this code.

unit BitmapHelperClass;

interface

uses
  System.Classes, FMX.Graphics;

type
  TBitmapHelper = class helper for TBitmap
  public
    procedure LoadFromUrl(AUrl: string);

    procedure LoadThumbnailFromUrl(AUrl: string; const AFitWidth, AFitHeight: Integer);
  end;

implementation

uses
  System.SysUtils, System.Types, IdHttp, IdTCPClient, AnonThread;

procedure TBitmapHelper.LoadFromUrl(AUrl: string);
var
  _Thread: TAnonymousThread<TMemoryStream>;
begin
  _Thread := TAnonymousThread<TMemoryStream>.Create(
    function: TMemoryStream
    var
      Http: TIdHttp;
    begin
      Result := TMemoryStream.Create;
      Http := TIdHttp.Create(nil);
      try
        try
          Http.Get(AUrl, Result);
        except
          Result.Free;
        end;
      finally
        Http.Free;
      end;
    end,
    procedure(AResult: TMemoryStream)
    begin
      if AResult.Size > 0 then
        LoadFromStream(AResult);
      AResult.Free;
    end,
    procedure(AException: Exception)
    begin
    end
  );
end;

procedure TBitmapHelper.LoadThumbnailFromUrl(AUrl: string; const AFitWidth,
  AFitHeight: Integer);
var
  Bitmap: TBitmap;
  scale: Single;
begin
  LoadFromUrl(AUrl);
  scale := RectF(0, 0, Width, Height).Fit(RectF(0, 0, AFitWidth, AFitHeight));
  Bitmap := CreateThumbnail(Round(Width / scale), Round(Height / scale));
  try
    Assign(Bitmap);
  finally
    Bitmap.Free;
  end;
end;

end.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文