Friday, January 18, 2008

Faith no More?

Microsoft has launched the .NET Reference Source project. They provide the source code of some parts of the .NET framework for reference and debugging for everybody. You can read more about it here.

With a lot of enthusiasm I went to experiment with it ...

... and about the first lines of code that crossed my way were the following ones:

try {
    using( Graphics graphics = Graphics.FromHwndInternal(IntPtr.Zero /*screen*/) ) { 
        string magicString = "The quick brown fox jumped over the lazy dog.";
        double magicNumber = 44.549996948242189; // chosen for compatibility with older versions of windows forms, but approximately magicString.Length
        float stringWidth = graphics.MeasureString(magicString, font).Width;
        width = (float) (stringWidth / magicNumber); 
    }
} 
catch { // We may get an bogus OutOfMemoryException 
        // (which is a critical exception - according to ClientUtils.IsCriticalException())
        // from GDI+. So we can't use ClientUtils.IsCriticalException here and rethrow. 
}
That is productive code from System.Windows.Forms.Form.

1 comment:

Related Posts Plugin for WordPress, Blogger...