Mega Code Archive

 
Categories / Delphi / Examples
 

Regcomponent

Creating and registering Delphi components. Notes from Richard. There are various aspects to this, but here we have an example that does things in perhaps the most simple way. The program below is complete and should be used like this: in the example we don't create a new 'object' from scratch (which we COULD do) -here we change the behaviour of an existing object. We define our own OwnerDrawRichEdit object to be a descendant of the RichEdit component, and we say in effect 'everything you do is fine except for things to do with a WMPaint message' so we REdefine that here. Having defined our new object, and spelled out the new procedure in a program, we add a new special procedure to the end called Register, with one line that says RegisterComponents('RichEbbs', [TOwnerDrawRichEdit]); so that now when we run this program we have a special .dcu (component definition) file among the other compiled files. From Delphi's IDE choose Component and Install and ADD THE PATH TO THIS DCU FILE TO THE EXISTING PATH (ie put a semicolon at the end of the the existing pathand then add the rest) and continue the procedure to ADD your new component to the IDE component pallette. Then choose Rebuild Library and lo and behold your new component has it's own tab and you can drag it across the desktop like any other. Wow! ************************************************************************** unit RegUnit; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, ComCtrls; type TOwnerDrawRichEdit = class(TRichEdit) private {private declarations} procedure WMPaint(var Message: TWMPaint); message WM_PAINT; protected {protected declarations} public {public declarations} published {published declarations} end; procedure Register; implementation procedure TOwnerDrawRichEdit.WMPaint(var Message: TWMPaint); var Buffer: Array[0..255] of Char; PS: TPaintStruct; DC: HDC; i: Integer; X,Y,Z: Word; OldColor: LongInt; begin DC := Message.DC; if DC = 0 then DC := BeginPaint(Handle, PS); try X := 1; Y := 1; SetBkColor(DC, Color); SetBkMode(DC, Transparent); OldColor := Font.Color; for i:=0 to Pred(Lines.Count) do begin if odd(i) then SetTextColor(DC, clRed) else SetTextColor(DC, OldColor); Z := Length(Lines[i]); StrPCopy(Buffer, Lines[i]); Buffer[Z] := #0; { not really needed } TextOut(DC, X,Y, Buffer, Z); Inc(Y, abs(Font.Height)); end; finally if Message.DC = 0 then EndPaint(Handle, PS); end; end; procedure Register; begin RegisterComponents('RichEbbs', [TOwnerDrawRichEdit]); end; end.