Link to home
Start Free TrialLog in
Avatar of Ham
Ham

asked on

Delphi 2.0 - incorporateing units

Hello,

I have stumbled across Delphi programmers Corner achieve on the net. They offer of freeware (units/components) - some of which I have copied.

Q1. My problem is how do I incorporate their prewritten units successfully.
Q2. I have a particular example I would like solved.

(I realise I place the unit's name in the 'implementation uses' area of the unit calling it, but where do I go from there?)

The name of the unit is Aligrid (it aligns text in a TStringGrid) See following for Aligrid code:

************************************************************
unit Aligrid;

{Usage: There is one additional property
 "Alignment", the possible values are:
 alRight, alLeft, alCenter.
 This alignment hold for the complete grid;
 I'm currently working on one with an alignment
 for each cell.}

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, Grids;

type TMyAlign = ( alRight, alLeft, alCenter);

type
  TStringAlignGrid = class(TStringGrid)
  private
    FAlign : TMyAlign;
    function GetAlign:TMyAlign;
    procedure SetAlign(const Value: TMyAlign);
    procedure Initialize;
  protected
    procedure DrawCell(ACol, ARow: Longint; ARect: TRect;
      AState: TGridDrawState); override;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property Alignment: TMyAlign read GetAlign write SetAlign default alLeft;
  end;

procedure Register;

implementation

constructor TStringAlignGrid.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Initialize;
end;

procedure TStringAlignGrid.Initialize;
begin
   FAlign := alLeft;
end;

function TStringAlignGrid.GetAlign: TMyAlign;
begin
   Result := FAlign;
end;

procedure TStringAlignGrid.SetAlign(const Value: TMyAlign);
begin
   FAlign := Value;
   Invalidate;
end;


procedure TStringAlignGrid.DrawCell(ACol, ARow: Longint; ARect: TRect;
  AState: TGridDrawState);

  procedure DrawCellText;
  var
    Text: array[0..255] of Char;
    l : Integer;
    Temp : TRect;
    Alignment : TMyAlign;
  begin
    StrPCopy(Text, Cells[ACol, ARow]);
    l:=Canvas.TextWidth(Cells[ACol, ARow]);
    Temp := ARect;
    Alignment := FAlign;
    if Alignment in [alCenter] then
       if l < (Temp.Right-Temp.Left) then begin
          l := ((Temp.Right-Temp.Left) - l ) div 2;
          Temp.Left := Temp.Left + l - 1;
          Temp.Right := Temp.Right - l + 1 ;
       end;
    if Alignment in [alRight] then begin
       Temp.Left := Temp.Right -l - 4 ;
       if temp.left < ARect.Left then
          temp.left := ARect.Left;
    end;
    ExtTextOut(Canvas.Handle, Temp.Left+2, Temp.Top + 2,  ETO_CLIPPED or
      ETO_OPAQUE, @Temp, Text, StrLen(Text), nil);
  end;

begin
  if DefaultDrawing then DrawCellText;
  {inherited DrawCell(ACol, ARow, ARect, AState);}
end;

procedure Register;
begin
  RegisterComponents('Beispiele', [TStringAlignGrid]);
end;

end.    
***********************************************************


ASKER CERTIFIED SOLUTION
Avatar of StevenB
StevenB

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ham
Ham

ASKER

Okay that solved that particular one, but Im not successful with some other custom written units - for instance:

1) Can I still make a component with a compiled unit *.dcu (I get Error cannot find *.pas)

or

2) Error 16-Bit (was this made for Delphi 1.0?)

Thankyou
Ham
1) Yes you can add a component from a *.dcu file, however there are a few reasons for the message you describe. Some components are just badly written, and don't work. In this case there is nothing you can do. The more sinister problem is that there is a bug in Delphi. If the string that forms your search path excedes 250 characters then Delphi starts to behave very strangely and throws up this error. The solution is to put all your components in one directory, so that the search path doesn't grow with every new component (incidentally I seem to recall that you have to edit the search path manually when you uninstall a component, an oversight on the part of Borland). Check your search path, and if that doesn't seem to be the problem then mail me the offending component and I'll check it on my machine:

stemail@dial.pipex.com

2) Yes it very much seems that way. I've never encountered this message, but 16-bit code can only be intended for D1.

  Steven
Avatar of Ham

ASKER

Thankyou, I am happy with your reply, and I will follow up with any offenders. Paul
Avatar of Ham

ASKER

PS. Ive noticed that there is a limit to the amount of components that can be kept at once. After Delphi decides that the next component that you are tring to recompile is one to many - it decides to clean out your entire library of components - this makes one very angry fellow, but Im lucky I have a squeezy cow (a cow that you squeeze when mad - rather than put your fist through the screen - I suggest all programmers purchase one).
Avatar of Ham

ASKER

PS. Ive noticed that there is a limit to the amount of components that can be kept at once. After Delphi decides that the next component that you are tring to recompile is one to many - it decides to clean out your entire library of components - this makes one very angry fellow, but Im lucky I have a squeezy cow (a cow that you squeeze when mad - rather than put your fist through the screen - I suggest all programmers purchase one).
 I'm not aware of any limit to the actual number of components that can be added, although It's entirely possible. The real one to watch is definately the Search path length, this is the sort of problem that can drive people to extremes of anger that cannot be relieved by squeezing cows.
  I'm still receiving email notification on this question, so if any more problems crop up then put them in as comments, or mail me direct and I'll see if there's anything I can do.

  Good Luck, Steven.