We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Delphi 2.0 - incorporateing units

Ham
Ham asked
on
Medium Priority
948 Views
Last Modified: 2012-05-04
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.    
***********************************************************


Comment
Watch Question

CERTIFIED EXPERT
Commented:
 What you have here is not simply a unit to be included in your code, but a custom written component. You can tell this as it has a register procedure. This procedure is used by Delphi to add the new component to the library of components (VCL). What you need to do then is add this component to your copy of Delphi.
  I'm currently running Delphi3 so I'm doing this from memory:

  Find the Install new components dialog from the menu. Ignore anything to do with OCX's for the moment and concentrate on the bottom window of the dialog. There is an Add button, or similar. After you click it you should be able to browse to the *.pas file that you are trying to install. select this and it should appear in the bottom window of the install dialog. OK this dialog and Delphi will recompile your VCL. There should now be a new tab (Beispiele) with the new component on it. Use this exactly as you do other components.

  If you want any more info then leave a comment.

  Steven.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Ham

Author

Commented:
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
CERTIFIED EXPERT

Commented:
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
Ham

Author

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

Author

Commented:
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).
Ham

Author

Commented:
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).
CERTIFIED EXPERT

Commented:
 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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.