Drawing Rotated Text in .NET Compact Framework 2.0

Back in February of last year I posted a sample on drawing rotated text using .NET Compact Framework 1.0 and OpenNETCF Smart Device Framework 1.4.  Since then, Compact Framework 2.0 has been released and Smart Device Framework 2.0 Beta1 has also been released.  Changes in the OpenNETCF.Drawing namespace caused the existing sample to not work anymore simply because the functionality in the SDF2.0 was removed.  Why was it it removed?  With CF2.0 there is a new class within the Microsoft.WindowsCE.Forms namespace called LogFont which replaces the SDF1.4 OpenNETCF.Drawing.FontEx class. 

Some of the more trickier parts of the code are setting the proper angle.  With the OpenNETCF.Drawing.FontEx class setting the font angle was as easy as setting the Angle property.  With the LogFont class, there was no Angle property, but from reading the MSDN documentation on the class points us to the Escapement property.  To set the font at an angle of 90 degrees, you will have to set the Escapement property to 900 or (90 * 10).  The other thing to note is the FaceName property has to be a null terminated string so you have to end it withe a ‘/0’.  Below are some other properties to add more functionality to the  LogFont:

Here is an implementation of drawing rotated/angled text using the above information.  For a complete sample see the sample application. 

private void DrawTextSample()
   //For VGA support get the scale to adjust dynamically
   int scale = (int)this.Scale(this.offGfx);

   //Get the size of the current font
   SizeF size = this.offGfx.MeasureString(this.txtDraw.Text, this.Font);

   //Create a new logfont class
   Microsoft.WindowsCE.Forms.LogFont lf = new Microsoft.WindowsCE.Forms.LogFont();

   //Escapement defines the angle at which you want the text to draw
   lf.Escapement = Convert.ToInt32(this.numericUpDown1.Value)*10;
   //Set the wieght of the font
   lf.Weight = this.Weight;
   //Set the quality to draw the font
   lf.Quality = this.Quality;
   //Set the name of the font. Note the null termintion char
   lf.FaceName = this.Font.Name + “/0”;
   //Set the font height. Note that we mulitply by scale to adjust for VGA devices
   lf.Height = (int)size.Height * scale;
   //Specifies the font family
   lf.PitchAndFamily = Microsoft.WindowsCE.Forms.LogFontPitchAndFamily.Default;
   //Specify if it is strikeout
   lf.StrikeOut = Convert.ToByte(this.chkStrikeout.Checked);
   //Specify if underlined
   lf.Underline = Convert.ToByte(this.chkUnderline.Checked);
   //Create a font that is compatible with the graphics object
   Font font = Font.FromLogFont(lf);
   //Fill the background
   using(SolidBrush b = new SolidBrush(this.BackColor))
      this.offGfx.FillRectangle(b, new Rectangle(0,0,this.Width,this.Height));
   //Get the middle of the screen
   Point point = new Point(Screen.PrimaryScreen.WorkingArea.Width / 2,
                           this.chkStrikeout.Top / 2 );
   //String format is a new class for CF2.0. Here we need to specify to not wrap the text
   //and to not clip the text if it is outside the bounds
   StringFormat sf = new StringFormat(StringFormatFlags.NoWrap | StringFormatFlags.NoClip);
   //Draw the text
   using (SolidBrush brush = new SolidBrush(Color.Red))
      this.offGfx.DrawString(this.txtDraw.Text, font, brush, point.X, point.Y, sf);
   //Force the screen to repaint

It’s a little more work drawing rotated/angled text using CF2.0 but the end result is the same as previous.

RotatedText1.zip (14.21 KB)

Warning: count(): Parameter must be an array or an object that implements Countable in /home/usnbis1maldq/domains/markarteaga.com/html/wp-includes/class-wp-comment-query.php on line 405

One thought on “Drawing Rotated Text in .NET Compact Framework 2.0”

Leave a Reply