Text Orientation in a Bitmap

Text Orientation in a Bitmap

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

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:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as