Mega Code Archive

 
Categories / Delphi / OOP
 

Adding new Standard Actions

Title: Adding new Standard Actions Question: how to make your own standard actions Answer: You can register your own new action classes with RegisterActions. Here is a usefull example of a action printing the current form: Don't forget to add this unit to a design time package. unit AjsStdActions; interface uses windows, StdActns, classes, forms; type TWindowPrint = class(TWindowAction) public constructor Create(AOwner: TComponent); override; procedure ExecuteTarget(Target: TObject); override; function HandlesTarget(Target: TObject): Boolean;override; procedure UpdateTarget(Target: TObject); override; end; procedure Register; implementation uses ActnList, Dialogs, Menus; { TWindowPrint } constructor TWindowPrint.Create(AOwner: TComponent); begin inherited; // new default Shortcut ShortCut := TextToShortCut('F3'); // new default Caption Caption := 'Print Window'; end; procedure TWindowPrint.ExecuteTarget(Target: TObject); begin // only for testing ShowMessage('Printing Windows....'); // print the current form GetForm(Target).Print; end; function TWindowPrint.HandlesTarget(Target: TObject): Boolean; begin Result := ((Form nil) and (Target = Form) or (Form = nil) and (Target is TForm)); end; procedure TWindowPrint.UpdateTarget(Target: TObject); begin // override the behavior of TWindow.UpdateTarget end; procedure Register; begin // register our new action class RegisterActions('window', [TWindowPrint], nil); end; end.