Jump to content
Mark-

Aligning text in a rectangle...

Recommended Posts

Hello,

 

I have a need to align some text in a rectangle to the traditional nine locations. The user decides how to align each cell and I wanted to make one call, passing in the vertical and horizontal alignment selections. The text needs to wordbreak and may contain linefeeds.

 

Searching the web, no joy, so I created this test project. Perhaps someone knows a better solution, and will share, and/or this will help another.

 

Cheers,

 

Mark

TextAlign.zip

Edited by Mark-

Share this post


Link to post
Guest

??? my fault... my fault 2x... sorry, im testing my code here

 

Edited by Guest

Share this post


Link to post
2 hours ago, emailx45 said:

??? my fault...

 

While I am not sure what you are asking, the screen capture, red, looks correct, I will answer: Yes, run the project included in the zip.

Edited by Mark-

Share this post


Link to post
2 hours ago, emailx45 said:

??? my fault... my fault 2x... sorry, im testing my code here

OK

Share this post


Link to post
Guest

hi @Mark-

 

my sample using "TPAINEL" but you can use any other component!!!

NOTE: unfortunatelly, is necessary 2x click on button for text appears... maybe some RAD bug or not... I dont know.

NOTE2: if possible avoid use "TPainBox" because it stay sending/receiving message to Windows repaint the region...

type
  TfrmMySecondForm = class(TForm)
    btnGoJohnnyGo: TButton;
    LabeledEdit1: TLabeledEdit;
    GridPanel1: TGridPanel;
    pnlTopLeft: TPanel;
    pnlTopCenter: TPanel;
    pnlTopRight: TPanel;
    pnlMiddleLeft: TPanel;
    pnlMiddleCenter: TPanel;
    pnlMiddleRight: TPanel;
    pnlBottomLeft: TPanel;
    pnlBottomCenter: TPanel;
    pnlBottomRight: TPanel;
    procedure btnGoJohnnyGoClick(Sender: TObject);
  private
  public
  end;

var
  frmMySecondForm: TfrmMySecondForm;

implementation

{$R *.dfm}

type
  THackPanel = class(TPanel);

procedure prcWriteMyTextOnThisAligmentPlease(lMyPanel: TPanel; lAligVert: integer; lAligHor: integer; lText: string);
var
  lRect: TRect;
  lLeft: integer;
  lTop : integer;
begin
  lMyPanel.Caption := '';
  //
  THackPanel(lMyPanel).Canvas.Font.Color := clRed;
  THackPanel(lMyPanel).Canvas.Font.Size  := 20;
  //
  lRect := lMyPanel.BoundsRect; // bounds of TPanel...
  //
  lLeft := 0;
  lTop  := 0;
  //
  case lAligVert of
    Ord(tfTop):
      ;
    Ord(tfVerticalCenter):
      lTop := (lRect.Height div 2) - (THackPanel(lMyPanel).Canvas.TextHeight(lText) div 2);
    Ord(tfBottom):
      lTop := lRect.Height - (THackPanel(lMyPanel).Canvas.TextHeight(lText));
  end;
  //
  case lAligHor of
    Ord(tfLeft):
      ;
    Ord(tfCenter):
      lLeft := (lRect.Width div 2) - (THackPanel(lMyPanel).Canvas.TextWidth(lText) div 2);
    Ord(tfRight):
      lLeft := (lRect.Width - THackPanel(lMyPanel).Canvas.TextWidth(lText));
  end;
  //
  THackPanel(lMyPanel).Canvas.TextOut(lLeft, lTop, lText);
end;

procedure TfrmMySecondForm.btnGoJohnnyGoClick(Sender: TObject);
var
  lRect: TRect;
  lText: string;
begin
  lText := LabeledEdit1.Text;
  //
  if not(lText.Trim = '') then
  begin
    prcWriteMyTextOnThisAligmentPlease(pnlTopLeft, Ord(tfTop), Ord(tfLeft), lText);
    prcWriteMyTextOnThisAligmentPlease(pnlTopCenter, Ord(tfTop), Ord(tfCenter), lText);
    prcWriteMyTextOnThisAligmentPlease(pnlTopRight, Ord(tfTop), Ord(tfRight), lText);
    //
    prcWriteMyTextOnThisAligmentPlease(pnlMiddleLeft, Ord(tfVerticalCenter), Ord(tfLeft), lText);
    prcWriteMyTextOnThisAligmentPlease(pnlMiddleCenter, Ord(tfVerticalCenter), Ord(tfCenter), lText);
    prcWriteMyTextOnThisAligmentPlease(pnlMiddleRight, Ord(tfVerticalCenter), Ord(tfRight), lText);
    //
    prcWriteMyTextOnThisAligmentPlease(pnlBottomLeft, Ord(tfBottom), Ord(tfLeft), lText);
    prcWriteMyTextOnThisAligmentPlease(pnlBottomCenter, Ord(tfBottom), Ord(tfCenter), lText);
    prcWriteMyTextOnThisAligmentPlease(pnlBottomRight, Ord(tfBottom), Ord(tfRight), lText);
  end
  else
    ShowMessage('Please, type some text');
end;

 

image.thumb.png.2522d036049ccf0192ab09f028ea9450.png

hug

VCL_Using_TextOUT_to_write_a_text_on_Canvas_Component.rar

Edited by Guest

Share this post


Link to post
48 minutes ago, emailx45 said:

hi @Mark-

 

NOTE2: if possible avoid use "TPainBox" because it stay sending/receiving message to Windows repaint the region...

 

It's just to show the code to process the text and alignments, the important part.😉

 

Your code does not support word wrap or embedded line feeds. It was/is a requirement and stated in the first post.

 

Thanks for playing.

 

Edited by Mark-

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×