开发者

C#: Create a font from a logical font (LOGFONT) - quality setting is ignored

开发者 https://www.devze.com 2023-01-27 08:49 出处:网络
I\'m trying to create a System.Drawing.Font from a LOGFONT using P/Invoke and Font.FromLogFont. The requested font have been created, however it always have the same rendering quality - no matter whic

I'm trying to create a System.Drawing.Font from a LOGFONT using P/Invoke and Font.FromLogFont. The requested font have been created, however it always have the same rendering quality - no matter which value I have assigned to the lfQuality member of the LOGFONT struct.

here is relevant code:


//LOGFONT struct
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public class LOGFONT
    {
        public const int LF_FACESIZE = 32;
        public int lfHeight;
        public int lfWidth;
        public int lfEscapement;
        publ开发者_StackOverflowic 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 = LF_FACESIZE)]
        public string lfFaceName;
    }
Then import for CreateFontIndirect:

[DllImport("gdi32.dll", CharSet=CharSet.Auto)]
        public static extern IntPtr CreateFontIndirect(
            [In, MarshalAs(UnmanagedType.LPStruct)]
            LOGFONT lplf   // characteristics
            );
Now create the font:

LOGFONT lf = new LOGFONT();
lf.lfFaceName = "DejaVu Sans";
lf.lfHeight = 36;
lf.lfQuality = 5;
IntPtr handle = CreateFontIndirect(lf);

Font f = Font.FromLogFont(lf);

So it seems that the managed code ignores the lfQuality member. Any way fixing that? I would like to control the way some fonts are being rendered, ignoring the system global settings.


Since, you're using CLEARTYPE_QUALITY (5), you may be dependent on Windows system ClearType settings.

See this http://support.microsoft.com/kb/306527 and this http://www.howtogeek.com/howto/windows-vista/why-do-my-windows-vista-fonts-look-horrible/


Try with different fonts. Not all fonts support all quality modes.

The issue shouldn't be to do with managed code as you are calling the underlying API directly. (Unless the managed FromLogFont call is changing things. I guess that's possible, although it'd surprise me.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号