[Mono-winforms-list] Winforms under WinXP Pro -> bug+solution

Jay Bertrand jay.bertrand@free.fr
Thu, 1 Jul 2004 21:04:32 +0200

Hi guys

Don't know if it's the right place to post but anyway ...

I tried to use Winforms under Windows XP Pro build 2600 SP1 with no success
(with Beta3 and v1.0) ;-(

Unhandled Exception: System.ArgumentException: Invalid Parameter. A null
reference or invalid value was found.

in <0x00073> System.Drawing.GDIPlus:CheckStatus (System.Drawing.Status)
in <0x00182> System.Drawing.Font:FromHfont (intptr)
in <0x00015> System.Windows.Forms.Control:get_DefaultFont ()
in <0x001af> System.Windows.Forms.Control:.ctor ()
in <0x00010> System.Windows.Forms.ScrollableControl:.ctor ()
in <0x00012> System.Windows.Forms.ContainerControl:.ctor ()
in <0x00016> System.Windows.Forms.Form:.ctor ()
in <0x00017> TestWinforms.Form1:.ctor ()
in <0x0004c> (wrapper remoting-invoke-with-check) TestWinforms.Form1:.ctor
in <0x0001b> TestWinforms.Form1:Main ()

After browsing the code and some debugging, i found that the problem was
GdipGetLogFontA do not accept a "null" Graphics param. Here's a workaround:

In System.Drawing.Font.FromHFont:

lock (typeof (Font))
hdc=GDIPlus.GetDC (IntPtr.Zero);
oldFont=GDIPlus.SelectObject (hdc, Hfont);
GDIPlus.CheckStatus (GDIPlus.GdipCreateFontFromDC (hdc, out newObject));
/* add this line */ Graphics g = Graphics.FromHdc(hdc);
/* comment this one */ /*GDIPlus.CheckStatus (GDIPlus.GdipGetLogFontA
(newObject, IntPtr.Zero, ref lf));*/
/* add this line */ GDIPlus.CheckStatus (GDIPlus.GdipGetLogFontA (newObject,
g.NativeObject, ref lf));
GDIPlus.SelectObject (hdc, oldFont);
GDIPlus.ReleaseDC (hdc);
/* add this line */ g.Dispose();

The same patch should be applied in the ToHFont() function.

Hope this will help :-)
Best regards