Solved

Rotated Text under Win95

Posted on 1997-11-25
5
182 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
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…

679 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