Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 189
  • Last Modified:

Rotated Text under Win95

I need a way to draw rotated text, preferably on a tCanvas. Any ideas? Preferably not involving drawing it pixel by pixel... :)

Adam...
0
AJFleming
Asked:
AJFleming
  • 3
  • 2
1 Solution
 
JimBob091197Commented:
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
 
JimBob091197Commented:
P.S.  Sorry about the code indentation.  It always gets lost when I paste from the Delphi IDE...
0
 
AJFlemingAuthor Commented:
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
 
JimBob091197Commented:
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
 
AJFlemingAuthor Commented:
Many Thanks,

Adam...
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now