Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have a TextBox in my Form and I am drawing a string into it as below.

C#
Font myFont = new Font("Arial",18F,FontStyle.Regular,GraphicsUnit.Point,128);
Graphics g = myTextBox.CreateGraphics();
Brush b = new SolidBrush(Color.Red);
g.DrawString("Item Drawn with DrawString",myFont ,b,new PointF(1,1));


The string displayed in the TextBox. Then I tried below code

C#
Font myFont = new Font("Arial",18F,FontStyle.Regular,GraphicsUnit.Point,128);
 Graphics g = myTextBox.CreateGraphics();
 TextRenderer.DrawText(g,"Item Drawn with DrawText",myFont,new Point(1,1),Color.Red);


Here the problem comes. Even though two methods g.DrawString() and TextRenderer.DrawText() uses same font, there is a difference in font style. That is some characters are rendered differently. If I use "1" instead of "128" in the font both methods will render characters as unique.

If I change GdiCharSet(128) value in font, while using g.DrawString() method there will be no effect. My question is why g.DrawString() method excludes GdiCharSet value?
Posted

1 solution

That's a good question. You'll have to download the source for those calls to find out.

Graphics.cs this is only one version there are others
public void DrawString(String s, Font font, Brush brush,RectangleF layoutRectangle, StringFormat format) 
{
   if (brush == null)
      throw new ArgumentNullException("brush"); 
   if (s == null || s.Length == 0)
      return; 
   if (font == null) 
      throw new ArgumentNullException("font");
 
   GPRECTF grf = new GPRECTF(layoutRectangle);
   IntPtr nativeStringFormat = (format == null) ? IntPtr.Zero : format.nativeFormat;
   int status = SafeNativeMethods.Gdip.GdipDrawString(new HandleRef(this, this.NativeGraphics),s, s.Length, new HandleRef(font, font.NativeFont), ref grf, new HandleRef(format,nativeStringFormat), new HandleRef(brush, brush.NativeBrush));
 
   //check error status sensitive to TS problems
   CheckErrorStatus(status); 
}


The other function I was not able to find so easily.
You can get the source from microsoft. There are instructions for using it in Visual Studio so that you can step through it.

Hope that helps.
Gregg
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900