TLama

Untitled

Jul 22nd, 2013
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.30 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs;
  8.  
  9. const
  10.   WM_UPDATE_FORM = WM_USER + 1;
  11.  
  12. type
  13.   TTaskUpdateEvent = procedure(Sender: TObject; TaskID, Position: Integer) of object;
  14.  
  15.   TFormUpdater = class
  16.   private
  17.     FWndHandle: HWND;
  18.     FOnTaskUpdate: TTaskUpdateEvent;
  19.     procedure WndProc(var AMessage: TMessage);
  20.   public
  21.     constructor Create;
  22.     destructor Destroy; override;
  23.     // the following event will be processed in the context of the main thread
  24.     property OnTaskUpdate: TTaskUpdateEvent read FOnTaskUpdate write FOnTaskUpdate;
  25.     // the following property is used to get handle which you'll pass to your threads
  26.     // and which will be the recipient of the posted messages; here WM_UPDATE_FORM
  27.     property WndHandle: HWND read FWndHandle;
  28.   end;
  29.  
  30. type
  31.   TForm1 = class(TForm)
  32.     procedure FormCreate(Sender: TObject);
  33.     procedure FormDestroy(Sender: TObject);
  34.   private
  35.     FFormUpdater: TFormUpdater;
  36.     procedure OnTaskUpdate(Sender: TObject; TaskID, Position: Integer);
  37.   public
  38.     { Public declarations }
  39.   end;
  40.  
  41. var
  42.   Form1: TForm1;
  43.  
  44. implementation
  45.  
  46. {$R *.dfm}
  47.  
  48. { TFormUpdater }
  49.  
  50. constructor TFormUpdater.Create;
  51. begin
  52.   inherited;
  53.   FWndHandle := AllocateHWnd(WndProc);
  54. end;
  55.  
  56. destructor TFormUpdater.Destroy;
  57. begin
  58.   if FWndHandle <> 0 then
  59.     DeallocateHWnd(FWndHandle);
  60.   inherited;
  61. end;
  62.  
  63. procedure TFormUpdater.WndProc(var AMessage: TMessage);
  64. begin
  65.   if AMessage.Msg = WM_UPDATE_FORM then
  66.   try
  67.     if Assigned(FOnTaskUpdate) then
  68.       FOnTaskUpdate(Self, AMessage.LParam, AMessage.WParam);
  69.   except
  70.     Application.HandleException(Self);
  71.   end
  72.   else
  73.     AMessage.Result := DefWindowProc(FWndHandle, AMessage.Msg,
  74.       AMessage.WParam, AMessage.LParam);
  75. end;
  76.  
  77. { TForm1 }
  78.  
  79. procedure TForm1.FormCreate(Sender: TObject);
  80. begin
  81.   FFormUpdater := TFormUpdater.Create;
  82.   FFormUpdater.OnTaskUpdate := OnTaskUpdate;
  83. end;
  84.  
  85. procedure TForm1.FormDestroy(Sender: TObject);
  86. begin
  87.   FFormUpdater.Free;
  88. end;
  89.  
  90. procedure TForm1.OnTaskUpdate(Sender: TObject; TaskID, Position: Integer);
  91. begin
  92.   // do whatever with the progress bars and other controls of the form here you're
  93.   // having here the TaskID and Position parameters passed from the posted message
  94. end;
  95.  
  96. end.
Advertisement
Add Comment
Please, Sign In to add comment