Font.FromLogFont producing strange results

  • Thread starter Thread starter Justin Van Patten
  • Start date Start date
J

Justin Van Patten

Hello,

I am having trouble converting a LOGFONT structure to a System.Drawing.Font
object.

I'm calling SystemParametersInfo to get the LOGFONT lfntSMCaptionFont from a
NONCLIENTMETRICS structure. I seem to be doing this correctly, but when I
try to create a System.Drawing.Font object with the Font.FromLogFont, the
new Font object has a different Name than the LOGFONT.

Here are the results of my test program (WinXP):

LOGFONT Name: Tahoma
Creating a System.Drawing.Font object from the LOGFONT...
Font Name: Arial

The FaceName of the LOGFONT is "Tahoma" (which is correct), but after using
the Font.FromLogFont method, the Font object's Name property yields "Arial"

Does anyone have any ideas?

Thanks for any insight, below is the C# source of my test program.

Regards,
-Justin Van Patten


// Test Program Source
using System;
using System.Runtime.InteropServices;
using System.Drawing;

class Test
{

[StructLayout(LayoutKind.Sequential)]
public struct LOGFONT
{
public int lfHeight;
public int lfWidth;
public int lfEscapement;
public int lfOrientation;
public int lfWeight;
public byte lfItalic;
public byte lfUnderline;
public byte lfStrikeOut;
public byte lfCharSet;
public byte lfOutPrecision;
public byte lfClipPrecision;
public byte lfQuality;
public byte lfPitchAndFamily;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string lfFaceName;
}

[StructLayout(LayoutKind.Sequential)]
public struct NONCLIENTMETRICS
{
public int cbSize;
public int iBorderWidth;
public int iScrollWidth;
public int iScrollHeight;
public int iCaptionWidth;
public int iCaptionHeight;
public LOGFONT lfntCaptionFont;
public int iSMCaptionWidth;
public int iSMCaptionHeight;
public LOGFONT lfntSMCaptionFont;
public int iMenuWidth;
public int iMenuHeight;
public LOGFONT lfntMenuFont;
public LOGFONT lfntStatusFont;
public LOGFONT lfntMessageFont;
}

[DllImport("User32.dll")]
public static extern bool SystemParametersInfo(
int Action, int uiParam,
ref NONCLIENTMETRICS pvParam, int WinIni);

static void Main()
{

const int SPI_GETNONCLIENTMETRICS = 41;

try
{

NONCLIENTMETRICS ncm = new NONCLIENTMETRICS();
ncm.cbSize = Marshal.SizeOf(ncm);
bool b = SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ncm.cbSize, ref ncm,
0);

if (b)
{

// Output The LOGFONT's FaceName
Console.WriteLine("LOGFONT Name: \t{0}", ncm.lfntSMCaptionFont.lfFaceName);

Console.WriteLine("Creating a System.Drawing.Font object from the
LOGFONT...");

// Try to create a System.Drawing.Font object from the LOGFONT
Font SMCaptionFont = Font.FromLogFont(ncm.lfntSMCaptionFont);

// Output the name
Console.WriteLine("Font Name: \t{0}", SMCaptionFont.Name);

}
else
{
Console.WriteLine("Failed");
}

}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
 
Back
Top