Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Rotated Text under Win95

Posted on 1997-11-25
5
Medium Priority
?
186 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 400 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

715 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