Eine weitere Alternative ist das Versenden einer Nachricht
Delphi-Quellcode:
unit UserCommand;
interface
uses
System.Messaging;
type
{$SCOPEDENUMS ON}
TUserCommand = ( Print, Save, SaveAs );
TUserCommandMessage =
class( TMessage )
private
FCommand: TUserCommand;
FIsQuery: Boolean;
FHandled: Boolean;
public
constructor Create( ACommand : TUserCommand; AIsQuery : Boolean = False );
property Command: TUserCommand
read FCommand;
property IsQuery: Boolean
read FIsQuery;
property Handled: Boolean
read FHandled
write FHandled;
end;
implementation
{ TUserCommandMessage }
constructor TUserCommandMessage.Create(ACommand: TUserCommand;
AIsQuery: Boolean);
begin
inherited Create;
FCommand := ACommand;
FIsQuery := AIsQuery;
FHandled := False;
end;
end.
die von der Action verschickt wird
Delphi-Quellcode:
procedure TMainForm.FilePrint1Execute( Sender: TObject );
begin
TMessageManager.DefaultManager.SendMessage( Self, TUserCommandMessage.Create( TUserCommand.Print ), True );
end;
procedure TMainForm.FilePrint1Update( Sender: TObject );
var
LCmd: TUserCommandMessage;
begin
LCmd := TUserCommandMessage.Create( TUserCommand.Print, True );
try
TMessageManager.DefaultManager.SendMessage( Self, LCmd, False );
TAction( Sender ).Enabled := LCmd.Handled;
finally
LCmd.Free;
end;
end;
und von den Empfängern entsprechend behandelt wird
Delphi-Quellcode:
procedure TMDIChild.AfterConstruction;
begin
inherited;
TMessageManager.DefaultManager.SubscribeToMessage( TUserCommandMessage, HandleUserCommand );
end;
procedure TMDIChild.BeforeDestruction;
begin
TMessageManager.DefaultManager.UnSubscribe( TUserCommandMessage, HandleUserCommand );
inherited;
end;
procedure TMDIChild.HandleUserCommand( const Sender: TObject; const M: TMessage );
var
LCmd: TUserCommandMessage absolute M;
begin
if not Self.Active then
Exit;
case LCmd.Command of
TUserCommand.Print:
if LCmd.IsQuery then
LCmd.Handled := ( Memo1.Text <> '' );
else
// Dokument drucken
TUserCommand.Save:
;
TUserCommand.SaveAs:
;
end;
end;
Das geht dann ohne Interfaces ...
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ea 0a 4c 14 0d b6 3a a4 c1 c5 b9
dc 90 9d f0 e9 de 13 da 60)