Solved

Rotated Text under Win95

Posted on 1997-11-25
5
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 100 total points
ID: 1351834
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
ID: 1351835
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
ID: 1351836
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
ID: 1351837
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
ID: 1351838
Many Thanks,

Adam...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
select query - oracle 16 103
Controlled Assessment GCSE - desperate help needed 4 123
Performance of SQL statement 37 148
How to build JSON File in Delphi 6 3 96
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

710 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