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.
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may