Monospaced font size

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 );
}
}
 
G

Guest

The code below worked fine for me (framework 1.1, .net 3000, xp 5.1). In
your example, why This.font vice drawfont?

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) _
Handles MyBase.Paint
e.Graphics.Clear(Me.BackColor)
Dim data As String = "________________________________"
data = data & data & data & vblf
For i As Integer = 1 To 5 : data &= data : Next
Dim drawfont As New System.Drawing.Font _
("Courier New", 8.5, FontStyle.Regular)
Dim drawBrush As New SolidBrush(Color.Black)
e.Graphics.DrawString(data, drawfont, drawBrush, 0, 0)
End Sub
 
G

Guest

Sorry, this.Font should be drawFont, else need to specify the font in the
Form attributes (which I have done as well). I've tested many different font
sizes and other attributes, so my example had a little bug. But,
nevertheless, it still doesn't work. I get a little space dot between many
of the lines.
I've tested on my XP screen res 1600*1200 and on a W2003 with screen 1024 *
768.
Did you ever test using c# code?
/Bjorn
 
B

Bob Powell [MVP]

Despite the font being monospaced there may be some fluctuation in
positioning due to the grid hinting used. Even for monospaced fonts the only
way to ensure exact placement is to use TextRenderingHint.Antialias. This
can work poorly for small font sizes however.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
G

Guest

TextRenderingHint.Antialias is not available in CF as far as I understand. I
was hoping to be able to produce a single executable for all .net platforms.
/Bjorn
 
G

Guest

1. No c# experience, and my case is 1024 x 768.
2. Try your test with Lucida Console font vice Courier New.
3. Urge you to try the test in VB on your equipment.
4. Also try this test on your gear. Make a form with RichTextBox, set rtb's
font to Courier New, or whatever you want. Type in strings of underscores
and see what happens. Try the same with Word or WordPad.

The purpose of 2, 3 and 4 is fault isolation. I'm dubious it helping much,
but the tests shouldn't take too long to do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top