Thema: Delphi Progress Form/Dialog VCL

Einzelnen Beitrag anzeigen

DieDolly

Registriert seit: 22. Jun 2018
2.175 Beiträge
 
#9

AW: Progress Form/Dialog VCL

  Alt 30. Sep 2018, 11:30
Ich bastel gleich mal eine Demo. Die Profis werden mich für den Code vermutlich zerreißen. Aber das ist das Resultat aus verschiedensten Empfehlungen.
Demo kommt gleich.

Hauptunit
Delphi-Quellcode:
type
 TForm1 ...
 private
  ...
  ProgressThread: TProgressThread;
 public
  ...
  procedure WndProc(var msg: TMessage); override;
 end;

const
 WM_PROGRESS_SET = WM_USER + 1;

implementation

{$R *.dfm}

procedure TForm1.WndProc(var msg: TMessage);
begin
 inherited;

 case msg.msg of
  WM_PROGRESS_SET:
   begin
    ProgressBar1.Position := msg.WParam;
   end;
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 ProgressBar1.Max := 1000;
 ProgressThread := TProgressThread.Create(Form1.Handle);
end;
Thread
Delphi-Quellcode:
unit uProgressThread;

interface

uses
 Winapi.Windows, System.Classes;

type
 TProgressThread = class(TThread)
 protected
 var
  DestinationWindowHandle: THandle;
 private
  {Private-Deklarationen}
 protected
  procedure Execute; override;
 public
  constructor Create(const DestinationWindowHandle: THandle);
 end;

implementation

uses Unit1;

{TProgressThread}

constructor TProgressThread.Create(const DestinationWindowHandle: THandle);
begin
 inherited Create;

 Self.DestinationWindowHandle := DestinationWindowHandle;
end;

procedure TProgressThread.Execute;
var
 i: Integer;
begin
 for i := 0 to 1000 do
  begin
   if Terminated then
    Break;

   PostMessage(DestinationWindowHandle, WM_PROGRESS_SET, i, 0);

   Sleep(25);
  end;
end;

end.
Das ist jetzt einfach nur schnell hingeschrieben. Die Kernfunktionalität (WndProc, PostMessage, Thread), sollte aber klar sein.

Geändert von DieDolly (30. Sep 2018 um 13:08 Uhr)
  Mit Zitat antworten Zitat