Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  Text Techniques (1)  »  Code Breakdown (2)
 »  Home  »  Windows Development  »  Graphics  »  Text Techniques (1)  »  Code Breakdown (2)
Text Techniques (1)
by Ged Mead | Published  03/30/2005 | .NET Newbie Graphics | Rating:
Code Breakdown (2)


1.  The first three lines will be familiar from the first example that we just dissected, so I won’t go through them again.

  Dim strText As String = "Bottom to Top"
  '  The Graphics object for this label
  Dim g2 As Graphics = e.Graphics
  '  The font, size and style to draw text 
  Dim fnt As Font = New Font("Arial", 12, FontStyle.Bold)

2.   Translations can become very complex, but this one is fairly straightforward.   I see that the MSDN definition of TranslateTransform is:” Prepends the specified translation to the transformation matrix of this Graphics object”

     Well, that’s clear enough then!   To be fair, it’s a totally accurate description, but I’m just not sure just how much further forward it would take your understanding of the topic.   In other circumstances it may be necessary to get right into the technical details, but for today we can take a much shallower dip into the water.

     For our purposes here, when you set the values for TranslateTransform all you are doing is moving the start point of the string that will be drawn.   The first value is the X-Position (number of pixels from the left  of the label border) and second value is the Y-Position (pixels from the top of the label).

    The values I’ve used should make sense to you now.   The X-Position is 13 pixels from the left edge.   The Y-Position is calculated by counting 25 pixels backwards from the bottom of the label.   This point (X:13, Y:Label2.Height – 25) is where the top left hand corner of the text string will begin.

  If you want to see the effect of this TranslateTransform line, try commenting out the line which follows it  (i.e. the RotateTransform one).   You will then see quite clearly how the start point of the string has been shunted down the label.  And will also see why we do in fact need the next line to swivel the text so that it is written upwards instead of horizontally.

  g2.TranslateTransform(13, Label2.Height - 25)


3.  Next,  the code which rotates the text.  The value of 270 represents the number of degrees in that rotation, so the text will swivel from horizontal to vertical.  In case you were wondering, a value of -90 (minus 90) will have exactly the same effect.




  The final lines show very little change from the code in the first example.  You will see that I changed the TextRenderingHint setting because this one seemed to produce a better finish for the black on white label on my monitor.   You may want to experiment with these settings to find the one that best suits your purposes.


   g2.TextRenderingHint = TextRenderingHint.ClearTypeGridFit
   '  Finally we draw the string
   g2.DrawString(strText, fnt, Brushes.White, 0, 0)

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p> 

<o:p>  As with the first example, you can apply the above technique to many other controls in order to rotate the text as required.   </o:p>


<o:p>  You probably don’t need me to tell you that by tweaking the number of degrees in the rotation angle, you can also write diagonally.</o:p>



Sponsored Links