devxlogo

Text Orientation in a Bitmap

Text Orientation in a Bitmap

Question:
How is it possible to write a vertical text (with TextOut procedure) in a bitmap (Delphi 2)?

Answer:
I know how to do this with a TLabel descendant, but you can probably apply the same principles to writing text to the canvas. Below is code for a component that will display text at any angle between 0 and 360 degrees. I use it a lot for creating cool effects on forms. You should concentrate on the Paint procedure. This does the actual writing and uses the TextOut function to display the angled text.

unit AngLabel;interfaceuses  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,  StdCtrls;type  TRotation = Integer;  TAngLabel = class(TCustomLabel)  private    FRotation : TRotation;    { Private declarations }  protected    { Protected declarations }    procedure Paint; override;    procedure SetRotation(Value: TRotation); virtual;    procedure CMFontChanged(var Message: TMessage);      message CM_FONTCHANGED;  public    { Public declarations }    constructor Create(AOwner: TComponent); override;  published    { properties published by TLabel but NOT by TRotationLabel:    Alignment, AutoSize, ShowAccelChar, WordWrap }    { Published properties declared in TCustomLabel}    property Align;    property Caption;    property Color;    property DragCursor;    property DragMode;    property Enabled;    property FocusControl;    property Font;    property ParentColor;    property ParentFont;    property ParentShowHint;    property PopupMenu;    property ShowHint;    property Transparent;    property Visible;    property OnClick;    property OnDblClick;    property OnDragDrop;    property OnDragOver;    property OnEndDrag;    property OnMouseDown;    property OnMouseMove;    property OnMouseUp;    {new properties}    property Angle : TRotation read FRotation write SetRotation;  end;procedure Register;implementation{$R *.dcr}procedure TAngLabel.CMFontChanged(var Message: TMessage);VAR TTM : TTextMetric;begin  Inherited;  if csLoading in ComponentState then Exit;  Canvas.Font := Font;  GetTextMetrics(Canvas.Handle, TTM);  if TTM.tmPitchAndFamily AND TMPF_TRUETYPE = 0 then    begin      Font.Name := 'Arial';      MessageBeep(MB_ICONSTOP);      ShowMessage('Only TrueType fonts permittted');    end;end;procedure TAngLabel.SetRotation(Value: TRotation);begin  if (Value <> FRotation) then    begin      FRotation := Value;      Invalidate;    end;end;constructor TAngLabel.Create(AOwner: TComponent);begin  Inherited Create(AOwner);  ControlStyle := ControlStyle + [csOpaque];  Width        := 65;  Height       := 65;  AutoSize     := False;  Transparent  := True;end;procedure TAngLabel.Paint;VAR  TLF    : TLogFont;  R      : TRect;  X,Y    : Integer;  TH, TW,           {text width and height}  AW, AH : Integer; {width & height of rect enclosing angled label}begin  R := ClientRect;  GetObject(Font.Handle, SizeOf(TLF), @TLF);  TLF.lfEscapement := (((Angle MOD 360) + 360) MOD 360) * 10;  with Canvas DO    begin      Font.Handle := CreateFontIndirect(TLF);      with Brush DO        if NOT Transparent then          begin            Style := bsSolid;            Color := Self.Color;          end        ELSE Style := bsClear;      TW := TextWidth(Caption);      TH := TextHeight(Caption);    end;  AW := Round((TW * Cos(Angle*pi/180)) +    (TH * Sin(Angle*pi/180)));  AH := Round((TW * Cos((Angle+90)*pi/180)) +    (TH * Sin((Angle+90)*pi/180)));  X := (R.Right-AW) DIV 2;  Y := (R.Bottom-AH) DIV 2;  if not Enabled then Canvas.Font.Color := clGrayText;  Canvas.TextOut(X, Y, Caption);end;procedure Register;begin  RegisterComponents('BD', [TAngLabel]);end;end.
See also  How HealthStream Learning Center Supports Healthcare Education and Compliance
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist