G
Guest
I am writing an application that draws text directly on the on the form
surface. The App must run on both .Net Compact Framework and the Normal .Net
Framework 1.1. I need to use Monospaced Font like Courier New (or
FontFamily.GenericMonospace). It is very important that each character takes
exactly the same amount of vertical space and that the distance between
characters is the same everywhere. This works fine on PocketPC emulator and
real device. It also works fine on Windows CE emulator. The problem is on
normal XP or W2003 and probably other targets running the normal .net
framework 1.1. I use the Graphics.DrawString method and it makes the
intercharacter distance different. It looks like there is a group of
characters with the same distance, and then one extra pixel gap followed by
another group. I have written a small OnPaint() demo that will demonstrate
the problem and make it easy to visualize since the character displayed is
'_' and should form a continous line.
Q: How do I make all characters occupy exactly the same number of pixels on
the screen?
Example:
protected override void OnPaint(PaintEventArgs g)
{
int x = 0, y = 0;
base.OnPaint(g);
String data = new System.String( '_', 80) ;
Font drawFont = new System.Drawing.Font("Courier New", 8.5F,
FontStyle.Regular);
SizeF CharPixel = g.Graphics.MeasureString("M", drawFont);
// SizeF CharPixel2 = new SizeF();
// CharPixel2 = g.Graphics.MeasureString("MM", drawFont);
// int fontWidth = (int)CharPixel2.Width - (int)CharPixel.Width;
int fontHeight = (int)CharPixel.Height;
SolidBrush drawBrush = new SolidBrush(Color.Black);
for ( int i = 0; i < 30; i++ )
{
g.Graphics.DrawString(data,
this.Font,
drawBrush,
x,
y + i * fontHeight );
}
}
surface. The App must run on both .Net Compact Framework and the Normal .Net
Framework 1.1. I need to use Monospaced Font like Courier New (or
FontFamily.GenericMonospace). It is very important that each character takes
exactly the same amount of vertical space and that the distance between
characters is the same everywhere. This works fine on PocketPC emulator and
real device. It also works fine on Windows CE emulator. The problem is on
normal XP or W2003 and probably other targets running the normal .net
framework 1.1. I use the Graphics.DrawString method and it makes the
intercharacter distance different. It looks like there is a group of
characters with the same distance, and then one extra pixel gap followed by
another group. I have written a small OnPaint() demo that will demonstrate
the problem and make it easy to visualize since the character displayed is
'_' and should form a continous line.
Q: How do I make all characters occupy exactly the same number of pixels on
the screen?
Example:
protected override void OnPaint(PaintEventArgs g)
{
int x = 0, y = 0;
base.OnPaint(g);
String data = new System.String( '_', 80) ;
Font drawFont = new System.Drawing.Font("Courier New", 8.5F,
FontStyle.Regular);
SizeF CharPixel = g.Graphics.MeasureString("M", drawFont);
// SizeF CharPixel2 = new SizeF();
// CharPixel2 = g.Graphics.MeasureString("MM", drawFont);
// int fontWidth = (int)CharPixel2.Width - (int)CharPixel.Width;
int fontHeight = (int)CharPixel.Height;
SolidBrush drawBrush = new SolidBrush(Color.Black);
for ( int i = 0; i < 30; i++ )
{
g.Graphics.DrawString(data,
this.Font,
drawBrush,
x,
y + i * fontHeight );
}
}