unit uAppMessages;
interface
uses classes, Windows, Messages;
type
IComponentControllerTimeComponent =
interface
['
{B4B1A347-0D72-433D-BF73-7DA96CBEB9EA}']
procedure TimeChanged;
end;
TComponentController =
class
private
FList : TList;
procedure TimeChanged;
protected
FWindow : HWND;
procedure MainWndProc(
var Message: TMessage);
procedure WndProc(
var Message: TMessage);
virtual;
public
constructor Create;
destructor Destroy;
override;
procedure AddObject(AObject : TObject);
virtual;
procedure RemoveObject(AObject : TObject);
virtual;
end;
function ComponentController : TComponentController;
implementation
uses Forms, StdCtrls, SysUtils;
var
FComponentController : TComponentController;
function ComponentController : TComponentController;
begin
if FComponentController =
nil then
FComponentController := TComponentController.Create;
result := FComponentController;
end;
{ TComponentController }
procedure TComponentController.AddObject(AObject: TObject);
begin
if FList.IndexOf(AObject) = -1
then
begin
if FList.Count = 0
then
FWindow := Forms.AllocateHWnd(MainWndProc);
FList.Add(AObject);
end;
end;
constructor TComponentController.Create;
begin
inherited Create;
FList := TList.Create;
end;
destructor TComponentController.Destroy;
begin
FList.Free;
inherited;
end;
procedure TComponentController.MainWndProc(
var Message: TMessage);
begin
try
WndProc(
Message);
except
Application.HandleException(Self);
end;
end;
procedure TComponentController.RemoveObject(AObject: TObject);
begin
FList.Remove(Pointer(AObject));
if FList.Count = 0
then
Forms.DeallocateHWnd(FWindow);
end;
procedure TComponentController.TimeChanged;
var
i : integer;
begin
for i := 0
to FList.Count-1
do
begin
// Besser über ein Interface und Supports realisieren
// nur als Demo für die Funktionalität
if TObject(Flist[i]).ClassNameIs('
TEdit')
then
TEdit(Flist[i]).Text := TimeToStr(Now);
end;
end;
procedure TComponentController.WndProc(
var Message: TMessage);
begin
if Message.Msg = WM_TIMECHANGE
then
begin
TimeChanged;
Message.Result := 0;
Exit;
end;
with Message do Result := DefWindowProc(FWindow, Msg, wParam, lParam);
end;
initialization
finalization
FComponentController.Free;
FComponentController :=
nil;
end.