Solved

Rotated Text under Win95

Posted on 1997-11-25
5
178 Views
Last Modified: 2010-04-04
I need a way to draw rotated text, preferably on a tCanvas. Any ideas? Preferably not involving drawing it pixel by pixel... :)

Adam...
0
Comment
Question by:AJFleming
  • 3
  • 2
5 Comments
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 100 total points
Comment Utility
Hi

Here is a routine to draw text on any canvas at any angle:

procedure WriteTextAngle(Canvas: TCanvas; x, y: Integer; const Caption: string; AFont: TFont; Angle: Integer);
// "Angle" is in tenths of a degree, so for 90 degrees enter 900.
// Font MUST be True Type, e.g. Times New Roman.
var
      hfnt, hfntPrev: THandle;
      lf: TLogFont;
begin
      FillChar(lf, SizeOf(TLogFont), 0);
      SetBkMode(Canvas.Handle, TRANSPARENT);
      StrPCopy(lf.lfFaceName, AFont.Name);
      lf.lfHeight := AFont.Height;
      if (fsBold in AFont.Style) then
            lf.lfWeight := FW_BOLD
      else
            lf.lfWeight := FW_NORMAL;
      if (fsUnderline in AFont.Style) then
            lf.lfUnderline := 1;
      if (fsItalic in AFont.Style) then
            lf.lfItalic := 1;
      lf.lfEscapement := Angle;
      hfnt := CreateFontIndirect(lf);
      hfntPrev := SelectObject(Canvas.Handle, hfnt);
      TextOut(Canvas.Handle, x, y, PChar(Caption), Length(Caption));
      SelectObject(Canvas.Handle, hfntPrev);
      DeleteObject(hfnt);
      SetBkMode(Canvas.Handle, OPAQUE);
end;


To call it (e.g.):
  AFont.Name := 'Times New Roman';
  AFont.Size := 12;
  WriteTextAngle(MyCanvas, 50, 50, 'Hello there', AFont, 900);

Regards,
JB
0
 
LVL 5

Expert Comment

by:JimBob091197
Comment Utility
P.S.  Sorry about the code indentation.  It always gets lost when I paste from the Delphi IDE...
0
 
LVL 1

Author Comment

by:AJFleming
Comment Utility
JimBob,
this looks like exactly what I'm looking for. But, if you don't mind, I'd just like to make sure I've got the ideas behind it right so I've added comments saying what I think is going on.

--==Start Code==--

procedure WriteTextAngle(Canvas: TCanvas; x, y: Integer; const
                  Caption: string; AFont: TFont; Angle: Integer);
var
  hfnt, hfntPrev: THandle;
  lf: TLogFont;
begin
  FillChar(lf, SizeOf(TLogFont), 0); // initiliase LogFont

  SetBkMode(Canvas.Handle, TRANSPARENT); //  set bkgrnd                 // transparent so that text appears over background?

  StrPCopy(lf.lfFaceName, AFont.Name);
  lf.lfHeight := AFont.Height;
  if (fsBold in AFont.Style) then
      lf.lfWeight := FW_BOLD
  else
      lf.lfWeight := FW_NORMAL;
  if (fsUnderline in AFont.Style) then
      lf.lfUnderline := 1;
  if (fsItalic in AFont.Style) then
      lf.lfItalic := 1;
  // all of above simply copies from the Delphi Font to the  
  // LogFont?

  lf.lfEscapement := Angle; // set logfont angle

  hfnt := CreateFontIndirect(lf); // create a font. (why                                           // indirect?)

  hfntPrev := SelectObject(Canvas.Handle, hfnt);

  // select the new font into the context, retaining a ref to
  // the old one.

  TextOut(Canvas.Handle, x, y, PChar(Caption), Length(Caption));
  // output the text

  SelectObject(Canvas.Handle, hfntPrev);
  // reselect the old font

  DeleteObject(hfnt);
  // delete the font created from LogFont

  SetBkMode(Canvas.Handle, OPAQUE);
  // reset the background mode to opaque

end;

--==End Code==--

Does this look roughly right?

Cheers,

Adam.
0
 
LVL 5

Expert Comment

by:JimBob091197
Comment Utility
You comments are right.

Regarding your comments about "SetBkMode(Canvas.Handle, TRANSPARENT);" and back to OPAQUE, you are correct.  You can leave these lines out (experiment a little...) and you will see that the text overwrites anything already on the canvas under the text.

Regarding your question "all of above simply copies from the Delphi Font to the LogFont" - that is correct.  It basically copies the data from Delphi's TFont into the windows font structure (the logfont).

Regarding your comment "set logfont angle" - Remember this is tenths of a degree.  For 180 degrees enter 1800.

Regarding your question "why indirect" (i.e. CreateFontIndirect).  I suppose you could use "CreateFont" instead of "CreateFontIndirect", just the parameter lists differ.  I guess I'm just using CreateFontIndirect, and passing in a logfont structure.  If you use CreateFont you wouldn't need the logfont structure, but CreateFont takes a lot more parameters (i.e. all the logfont stuff).

JB
0
 
LVL 1

Author Comment

by:AJFleming
Comment Utility
Many Thanks,

Adam...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now