Difference between revisions of "How To Write Lazarus Component"

From Lazarus wiki
(Removing Components)
(Enhancing mycom.pas)
Line 96: Line 96:
 
==Enhancing mycom.pas==
 
==Enhancing mycom.pas==
  
* The codes in mycom.pas above gives you the basics on what you need to create a component. The following is a more comprehensive version with some tips on writing codes for components.
+
* The codes in ''mycom.pas'' above gives you the basics on what you need to create a component. The following is a more comprehensive version with some tips on writing codes for components.
  
mycom.pas
 
 
<delphi>
 
<delphi>
 
unit mycom;
 
unit mycom;

Revision as of 19:48, 30 September 2008

Step 1: Create Icons For The Package

  • Size of PNG file should be 24x24
  • You may need to compile lazres at first use. Simply open the lazres.lpi in the IDE and at the menu click run > build.

To create the lrs file run (where samplepackage is the name of your package and TMyCom is the name of your component):

~/lazarus/tools/lazres samplepackage.lrs TMyCom.png

You can add more than one image to the lrs file by appending the image filename at the end. Eg. ~/lazarus/tools/lazres samplepackage.lrs TMyCom1.png TMyCom2.png ...

Following is a sample of the samplepackage.lrs file.

LazarusResources.Add('TMyCom','PNG',[
  #137'PNG'#13#10#26#10#0#0#0#13'IHDR'#0#0#0#24#0#0#0#24#8#2#0#0#0'o'#21#170#175
  +#0#0#0#4'gAMA'#0#0#177#143#11#252'a'#5#0#0#0'|IDAT8O'#237#212#209#10#192' '#8
  +#5'P'#247#231#251's'#215#138#133#164#166'\'#220#195'`'#209'c'#157'L'#173#131
  +#153#169'd4'#168'dP'#137'r_'#235'5'#136'@Zmk'#16'd9'#144#176#232#164'1'#247
  +'I'#8#160'IL'#206'C'#179#144#12#199#140'.'#134#244#141'~'#168#247#209'S~;'#29
  +'V+'#196#201'^'#10#15#150'?'#255#18#227#206'NZ>42'#181#159#226#144#15'@'#201
  +#148#168'e'#224'7f<@4'#130'u_YD'#23#213#131#134'Q]'#158#188#135#0#0#0#0'IEND'
  +#174'B`'#130
]);

Step 2: Create The Unit

  • Create a file called mycom.pas with the following code (The 'Sample' parameter in the RegisterComponents tells the IDE to put the component in a tab called Sample in the components tab)

<delphi> unit mycom;

{$mode objfpc}{$H+}

interface

uses

 Classes, SysUtils, StdCtrls, Forms,
 LCLType,LCLIntf,lresources,LCLProc;

type

 TMyCom = class (TCustomComboBox)
 private
 public
 end;

procedure Register;

implementation

procedure Register; begin

 RegisterComponents('Sample',[TMyCom]);

end;

initialization

 {$I samplepackage.lrs}

end. </delphi>

Step 3: Create The Package

  • Create a directory and but samplepackage.lrs and mycom.pas in it.
  • On the Lazarus IDE menu, click Package > New Package. The following image shows the Package Manager
Package Maker
  • Click the Add button, goto the Add Unit tab. At the Unit file name, browse to the mycom.pas file. Click the Add Unit button to confirm. If the packet manager complains that the unit is not in the unitpath, click yet to add the directory to the unit path.
  • Click the Add button again, goto the Add File tab and browse to the samplepackage.lrs file and click Ok.
  • Click the Add button again, goto the New Requirement tab, in the Package name select LCL (as the base class, TCustomComboBox, uses this) and click Ok.

End result should look like this:

Package Maker
  • Click the Options button. Select the IDE Integration tab. At the Package Type make sure Designtime and Runtime is selected.
  • Click the Save button. Save the package as samplepackage.lpk
  • Click the Compile button to check to see if the files compiles without errors.
  • Click the Install button, Lazarus will rebuild and restart automatically.

The component is created and ready to be used:

Component Created

Removing Components

  • To remove installed components, on the IDE menu, click Package > Configure installed packages. The following image shows the Installed Packages tool.
Installed Components
  • Select the package you want to uninstall and click Uninstall selection.

If something goes wrong with a package (eg package directory is deleted without first uninstalling it), Lazarus may not allow you to uninstall packages. To fix the problem, at the IDE menu click Tools > Build Lazarus. Lazarus will rebuild all packages and restart. You should now be able to uninstall problematic packages.

Enhancing mycom.pas

  • The codes in mycom.pas above gives you the basics on what you need to create a component. The following is a more comprehensive version with some tips on writing codes for components.

<delphi> unit mycom;

{$mode objfpc}{$H+}

interface

uses

 Classes, SysUtils, StdCtrls, Forms, Dialogs,
 LCLType,LCLIntf,lresources,LCLProc;

type

 TSampleEvent = procedure(MyText: String) of Object;
 TMyCom = class (TCustomComboBox)
 private
   FMyText: String;
   FOnChange2: TNotifyEvent;
   FOnSample: TSampleEvent;
 public
   constructor Create(TheOwner: TComponent); override;
   procedure CreateWnd; override;
   procedure Change; override;
 protected
   function GetMyText2: String;
   procedure SetMyText2(MyText: String);
 published
   property MyText: String read FMyText write FMyText;
   property MyText2: String read GetMyText2 write SetMyText2;
   property OnChange2: TNotifyEvent read FOnChange2 write FOnChange2;
   property OnSample: TSampleEvent read FOnSample write FOnSample;
   
   // properties from TCustomComboBox
   property Align;
   property Anchors;
   property ArrowKeysTraverseList;
   property AutoComplete;
   property AutoCompleteText;
   property AutoDropDown;
   property AutoSelect;
   property AutoSize;
   property BidiMode;
   property BorderSpacing;
   property CharCase;
   property Color;
   property Ctl3D;
   property Constraints;
   property DragCursor;
   property DragMode;
   property DropDownCount;
   property Enabled;
   property Font;
   property ItemHeight;
   property ItemIndex;
   property Items;
   property ItemWidth;
   property MaxLength;
   property OnChange;
   property OnChangeBounds;
   property OnClick;
   property OnCloseUp;
   property OnContextPopup;
   property OnDblClick;
   property OnDragDrop;
   property OnDragOver;
   property OnDrawItem;
   property OnEndDrag;
   property OnDropDown;
   property OnEditingDone;
   property OnEnter;
   property OnExit;
   property OnGetItems;
   property OnKeyDown;
   property OnKeyPress;
   property OnKeyUp;
   property OnMeasureItem;
   property OnMouseDown;
   property OnMouseMove;
   property OnMouseUp;
   property OnStartDrag;
   property OnSelect;
   property OnUTF8KeyPress;
   property ParentBidiMode;
   property ParentColor;
   property ParentCtl3D;
   property ParentFont;
   property ParentShowHint;
   property PopupMenu;
   property ReadOnly;
   property ShowHint;
   property Sorted;
   property Style;
   property TabOrder;
   property TabStop;
   property Text;
   property Visible;    
 end;

procedure Register;

implementation

procedure Register; begin

 RegisterComponents('Sample',[TMyCom]);

end;

constructor TMyCom.Create(TheOwner: TComponent); begin

 inherited Create(TheOwner);
 Self.Style := csDropDownList;

end;

procedure TMyCom.CreateWnd; begin

 inherited CreateWnd;
 Items.Assign(Screen.Fonts);

end;

procedure TMyCom.Change; begin

 inherited;
 if Assigned(FOnChange2) then FOnChange2(Self);
 if Assigned(FOnSample) then FOnSample(FMyText);

end;

function TMyCom.GetMyText2: String; begin

 Result:=FMyText;

end;

procedure TMyCom.SetMyText2(MyText: String); begin

 FMyText:=MyText;

end;

initialization

 {$I samplepackage.lrs}

end. </delphi>