473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Font.FromLogFont producing strange results

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
1 4135
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

133
by: Philipp Lenssen | last post by:
Why is there no standardized and well-working way for a web-page to offer the font for download/embed it, in order to be displayed on the page? No matter what you think of the preferred font of a...
131
by: Peter Foti | last post by:
Simple question... which is better to use for defining font sizes and why? px and em seem to be the leading candidates. I know what the general answer is going to be, but I'm hoping to ultimately...
115
by: J | last post by:
I've run CSSCheck on my style sheets and I always get a warning similar to this: "font: bold 9pt/100% sans-serif Warning: Absolute length units should not generally be used on the Web ..." ...
0
by: Joshua T. Moore | last post by:
I must print with a non-TrueType font for the printer to do specific features. I P/Invoked CreateFont to get the (IntPtr) handle of the font, but System.Drawing.Graphics.DrawString's...
2
by: MattC | last post by:
Hi, I am developing a timesheet system that collects information about employees activities. In this I collect details about projects they do work for, what type of work, how long, when etc...
1
by: Justin Van Patten | last post by:
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...
7
by: carterweb | last post by:
This is how I do it now. 1. Determine the dimensions of the rectangle. 2. Set a my font size to a fixed maximum size. 3. Apply the font my string and measure the string using the graphics...
1
by: Roger Miller | last post by:
The following program gets a TextCtrl's text attributes and sets them back unchanged. However it reports that the font size is 124, and after resetting the attributes the text becomes that size....
16
by: carlbernardi | last post by:
Hi, I was wondering if there is way use a font that is somehow kept in the code instead of having to load it? I am working on a single script that so far can produce images and html but I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.