DrawString and MeasureString in Windows forms

Sunday, December 28, 2003
When I first got the .NET Beta I began working on a z-machine interpreter. A Z-machine is the name of a virtual machine targeted by programs written, by among others, Infocom. You can find the z-machine specification online.

As a kid I used to love playing the text based adventure games like Zork and Witness. There are still people writing interactive fiction for the Z Machine, and I had a .NET Z-Machine up and running and playing Zork!

Ok, not very pretty but it was functional and taught me a good bit about .NET.

What drove me absolutely stark raving mad for a period of time was drawing text on the screen. Because of the way the Z-Machine works I needed to DrawString one character at a time. The output looked terrible. Words were spaced badly, and text flowed much further to the right at times than it should have. I was sure this was a bug somewhere in the rendering routines of my code and spent a fair amount of time trying to find the problem.

Finally, I poured over the documentation again and noticed the StringFormat enumeration. Viola! Passing StringFormat.GenericTypographic to the Graphic object’s DrawString and MeasureString methods solved everything. I later found out the text rendering routines in GDI+ make a lot of effort to render in a “resolution independent layout”, and the link just provided is a good document if you are writing terminal emulation type software and need control over the appearance of text in a window.

My Pluralsight Courses

K.Scott Allen OdeToCode by K. Scott Allen
What JavaScript Developers Should Know About ECMAScript 2015
The Podcast!