By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,261 Members | 2,662 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,261 IT Pros & Developers. It's quick & easy.

Font.FromLogFont producing strange results

P: n/a
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());
}
}
}
Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Justin,
Does anyone have any ideas?


Add CharSet=CharSet.Auto to the StructLayout and DllImport attributes.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.