Link to home
Start Free TrialLog in
Avatar of eldraco
eldraco

asked on

Right Justify TEdit

I need some souce code examples of Right justification of a TEdit in Delphi 6
Thanks
ASKER CERTIFIED SOLUTION
Avatar of hongjun
hongjun
Flag of Singapore image

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 eldraco
eldraco

ASKER

I apologise. I forgot to mention that I do not want to use another component.
Thanks anyway
I think there is no way to do that :(
This is the minimum you can do
http://www.undu.com/Articles/000207c.html

hongjun
Avatar of eldraco

ASKER

I tried the article from UNDU but I could not get it to work. The operating system I am using is Windows 2000
Avatar of TheRealLoki
it's a pretty ugly workaround anyway, you have to call the RJustify procedure on every change/kerpress.
The only way of actually getting a nice result is to use hongjun's example
note: you do not actually have to install a component, just do it like this
(I don't want points, just helping out)

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TEditAlign = class(TEdit)
  private
    { Private declarations }
  f_alignment: TAlignment;
  protected
    { Protected declarations }
    procedure CreateParams(var params:TCreateParams); override;
    procedure SetAlignment(value: TAlignment);
 public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
  published
    { Published declarations }
    property Alignment:TAlignment read f_alignment write SetAlignment;
  end;


type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    MyRightAlignedEdit: TEditAlign; // define your edit box here
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

IMPLEMENTATION

{$R *.DFM}


// TEDIT Methods
constructor TEditAlign.Create(AOwner: TComponent);
    begin
        inherited create(AOwner);
    end;

procedure TEditAlign.SetAlignment(value: TAlignment);
    begin
        if value<>f_alignment then
        begin
            f_alignment:=value;
            recreatewnd;
        end;
    end;

procedure TEditAlign.CreateParams(var params:TCreateParams);
    const
        Alignments : array[TAlignment] of Longint = (ES_LEFT, ES_RIGHT, ES_CENTER);
    begin
        inherited CreateParams(Params);
        Params.Style := Params.Style or Alignments[f_Alignment] or ES_MULTILINE;
    end;

procedure RJustifyEdit(var ThisEdit : TEdit);
    var
        Left, Width : Integer;
        GString : String;
        Rgn : TRect;
        TheCanvas : TControlCanvas;
    begin
        TheCanvas := TControlCanvas.Create;
        try
            TheCanvas.Control := ThisEdit;
            GString := ThisEdit.Text;
            Rgn     := ThisEdit.ClientRect;
            TheCanvas.FillRect(Rgn);
            Width   := TheCanvas.TextWidth(GString);
            Left := Rgn.Right - Width - 1;
            TheCanvas.TextRect(Rgn, Left, 0, GString);
        finally
            TheCanvas.Free;
            end ;
        end;

// TEDIT Methods - End

procedure TForm1.FormCreate(Sender: TObject);
    begin
// Create your edit box here, it's really quite simple
        MyRightAlignedEdit := TEditAlign.Create(self);
        with MyRightAlignedEdit do
        begin
            Left := 152;
            Top := 56;
            Width := 121;
            Height := 21;
            TabOrder := 0;
            Text := 'RightAlignedEdit';
            Alignment := taRightJustify; // this is the bit you want
            Parent := Form1; // so you can actually see it :)  make the parent a panel or groupbox if you want it there
        end;
       
    end;

end.