Writing Custom Common Controls

Top  Previous  Next

 

FastReport contains a set of common controls which can be placed on dialogue forms inside reports. They are as follows:

 

TfrxLabelControl

TfrxEditControl

TfrxMemoControl

TfrxButtonControl

TfrxCheckBoxControl

TfrxRadioButtonControl

TfrxListBoxControl

TfrxComboBoxControl

TfrxDateEditControl

TfrxImageControl

TfrxBevelControl

TfrxPanelControl

TfrxGroupBoxControl

TfrxBitBtnControl

TfrxSpeedButtonControl

TfrxMaskEditControl

TfrxCheckListBoxControl

 

These controls correspond to the Delphi component palette standard controls. If the standard functionality is not sufficient then you can create your own common controls for use in your reports.

 

The base class for all common controls is the “TfrxDialogControl” class, declared in the frxClass file:

 

TfrxDialogControl = class(TfrxReportComponent)

protected

procedure InitControl(AControl: TControl);

public

constructor Create(AOwner: TComponent); override;

destructor Destroy; override;

class function GetDescription: String; virtual;

property Caption: String;

property Color: TColor;

property Control: TControl;

property OnClick: TfrxNotifyEvent;

property OnDblClick: TfrxNotifyEvent;

property OnEnter: TfrxNotifyEvent;

property OnExit: TfrxNotifyEvent;

property OnKeyDown: TfrxKeyEvent;

property OnKeyPress: TfrxKeyPressEvent;

property OnKeyUp: TfrxKeyEvent;

property OnMouseDown: TfrxMouseEvent;

property OnMouseMove: TfrxMouseMoveEvent;

property OnMouseUp: TfrxMouseEvent;

published

property Left;

property Top;

property Width;

property Height;

property Font;

property ParentFont;

property Enabled: Boolean;

property Visible;

end;

 

To create your own control you should inherit from this class and override at least the constructor and the “GetDescription” method. It will be necessary to create the common control and initialize it using the “InitControl” method in the constructor. The GetDescription method is for returning a description of the common control. As you can see the TfrxDialogControl class already has a large number of properties and methods in the public section. Move properties and events into the “published” section of your common control as required and also create new properties that are specific to your control.

 

Common control registration and deletion is performed by using the frxObjects global object methods declared in the frxDsgnIntf file:

 

frxObjects.RegisterObject(ClassRef: TfrxComponentClass;

                         ButtonBmp: TBitmap);

frxObjects.Unregister(ClassRef: TfrxComponentClass);

 

During registration you should specify the control class name and its picture. The ButtonBmp size should be 16x16 pixels.

 

Let's look at an example of a common control that simplifies the functionality of the standard Delphi TBitBtn control.

 

uses frxClass, frxDsgnIntf, Buttons;

 

type

 TfrxBitBtnControl = class(TfrxDialogControl)

private

   FButton: TBitBtn;

  procedure SetKind(const Value: TBitBtnKind);

  function GetKind: TBitBtnKind;

public

  constructor Create(AOwner: TComponent); override;

  class function GetDescription: String; override;

  property Button: TBitBtn read FButton;

published

   { add new properties }

  property Kind: TBitBtnKind read GetKind

                              write SetKind default bkCustom;

   { following properties are already declared in parent class }

  property Caption;

  property OnClick;

  property OnEnter;

  property OnExit;

  property OnKeyDown;

  property OnKeyPress;

  property OnKeyUp;

  property OnMouseDown;

  property OnMouseMove;

  property OnMouseUp;

end;

 

constructor TfrxBitBtnControl.Create(AOwner: TComponent);

begin

 { default constructor }

inherited;

 { create required common control }

 FButton := TBitBtn.Create(nil);

 FButton.Caption := 'BitBtn';

 { initialize it }

 InitControl(FButton);

 

 { set default size }

 Width := 75;

 Height := 25;

end;

 

class function TfrxBitBtnControl.GetDescription: String;

begin

 Result := 'BitBtn control';

end;

 

procedure TfrxBitBtnControl.SetKind(const Value: TBitBtnKind);

begin

 FButton.Kind := Value;

end;

 

function TfrxBitBtnControl.GetKind: TBitBtnKind;

begin

 Result := FButton.Kind;

end;

 

var

 Bmp: TBitmap;

 

initialization

 Bmp := TBitmap.Create;

 { load picture from resource;

   it should have already been placed there, of course }

 Bmp.LoadFromResourceName(hInstance, 'frxBitBtnControl');

 frxObjects.RegisterObject(TfrxBitBtnControl, Bmp);

 

finalization

 frxObjects.Unregister(TfrxBitBtnControl);

 Bmp.Free;

 

end.