difference between Graphics.DrawString and Win32 GDI ::DrawText

D

DrDevious

Maybe I am doing something wrong but has anyone else here noticed a
difference in the positioning of text between the Graphics.DrawString
method and the Win32 GDI DrawText function?

My text is getting shifted to the right by 3 pixels for some reason.
Everything else matches.

I am creating my fonts under C# .NET in a way that should produce the
same results as I am getting under C++ with the Win32 GDI API:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
class LOGFONT
{
public enum CharacterSet
{
ANSI_CHARSET = 0,
DEFAULT_CHARSET = 1,
SYMBOL_CHARSET = 2,
SHIFTJIS_CHARSET = 128,
HANGEUL_CHARSET = 129,
HANGUL_CHARSET = 129,
GB2312_CHARSET = 134,
CHINESEBIG5_CHARSET = 136,
OEM_CHARSET = 255,
JOHAB_CHARSET = 130,
HEBREW_CHARSET = 177,
ARABIC_CHARSET = 178,
GREEK_CHARSET = 161,
TURKISH_CHARSET = 162,
VIETNAMESE_CHARSET = 163,
THAI_CHARSET = 222,
EASTEUROPE_CHARSET = 238,
RUSSIAN_CHARSET = 204,
MAC_CHARSET = 77,
BALTIC_CHARSET = 186
}

public enum ClipPrecision
{
CLIP_DEFAULT_PRECIS = 0,
CLIP_CHARACTER_PRECIS = 1,
CLIP_STROKE_PRECIS = 2
}

public enum OutputPrecision
{
OUT_DEFAULT_PRECIS = 0,
OUT_STRING_PRECIS = 1,
OUT_CHARACTER_PRECIS = 2,
OUT_STROKE_PRECIS = 3,
OUT_TT_PRECIS = 4,
OUT_DEVICE_PRECIS = 5,
OUT_RASTER_PRECIS = 6,
OUT_TT_ONLY_PRECIS = 7,
OUT_OUTLINE_PRECIS = 8,
OUT_SCREEN_OUTLINE_PRECIS = 9
}

[Flags]
public enum PitchAndFamily
{
DEFAULT_PITCH = 0,
FIXED_PITCH = 1,
VARIABLE_PITCH = 2,
MONO_FONT = 8,
FF_DONTCARE = (0<<4),
FF_ROMAN = (1<<4),
FF_SWISS = (2<<4),
FF_MODERN = (3<<4),
FF_SCRIPT = (4<<4),
FF_DECORATIVE = (5<<4)
}

public enum Quality
{
DEFAULT_QUALITY = 0,
DRAFT_QUALITY = 1,
PROOF_QUALITY = 2,
NONANTIALIASED_QUALITY = 3,
ANTIALIASED_QUALITY = 4
}

public enum Weight
{
FW_DONTCARE = 0,
FW_THIN = 100,
FW_EXTRALIGHT = 200,
FW_LIGHT = 300,
FW_NORMAL = 400,
FW_MEDIUM = 500,
FW_SEMIBOLD = 600,
FW_BOLD = 700,
FW_EXTRABOLD = 800,
FW_HEAVY = 900,
FW_ULTRALIGHT = FW_EXTRALIGHT,
FW_REGULAR = FW_NORMAL,
FW_DEMIBOLD = FW_SEMIBOLD,
FW_ULTRABOLD = FW_EXTRABOLD,
FW_BLACK = FW_HEAVY
}

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;
}

...

LOGFONT lf = new LOGFONT();
lf.lfCharSet = (byte)LOGFONT.CharacterSet.DEFAULT_CHARSET;
lf.lfClipPrecision =
(byte)LOGFONT.ClipPrecision.CLIP_DEFAULT_PRECIS;
lf.lfEscapement = 0;
lf.lfFaceName = "Arial";
lf.lfHeight = -(int)((points * dpi.Height)/72.0F);
lf.lfItalic = 0;
lf.lfOrientation = 0;
lf.lfOutPrecision =
(byte)LOGFONT.OutputPrecision.OUT_DEFAULT_PRECIS;
lf.lfPitchAndFamily = (byte)(LOGFONT.PitchAndFamily.DEFAULT_PITCH
| LOGFONT.PitchAndFamily.FF_DONTCARE);
lf.lfQuality = (byte)LOGFONT.Quality.DEFAULT_QUALITY;
lf.lfStrikeOut = 0;
lf.lfUnderline = 0;
lf.lfWeight = (int)LOGFONT.Weight.FW_BOLD;
lf.lfWidth = 0;
Font font = Font.FromLogFont(lf);


I am using GraphicsUnit.Pixel, the default transform and the default
settings for all other Graphics settings. The results of other
graphics drawing operations via C# .NET are being offset from the
upper left hand corner of the device context the same # of pixels as
they were with the Win32 GDI API.


Thanks to anyone who replies. (Please reply to the newgroup instead of
to my yahoo e-mail account, which I never check.)

DrDevious
 
G

Guest

I notice that the Escapement property doesn't seem to mean anything anymore
if Graphics.DrawString is used. I have to TranslateTransform and
RotateTransform to place rotated text where I want it to go. Maybe I will
have to use the old Win32 DrawText method or re-write all my Text routines to
use the Transforms (much simpler).

Thanks for the LOGFONT sample!! I couldn't find the structure in the .NET
Framework and MSDN Library or On-line documentation.

DrDevious said:
Maybe I am doing something wrong but has anyone else here noticed a
difference in the positioning of text between the Graphics.DrawString
method and the Win32 GDI DrawText function?

My text is getting shifted to the right by 3 pixels for some reason.
Everything else matches.

I am creating my fonts under C# .NET in a way that should produce the
same results as I am getting under C++ with the Win32 GDI API:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
class LOGFONT
{
public enum CharacterSet
{
ANSI_CHARSET = 0,
DEFAULT_CHARSET = 1,
SYMBOL_CHARSET = 2,
SHIFTJIS_CHARSET = 128,
HANGEUL_CHARSET = 129,
HANGUL_CHARSET = 129,
GB2312_CHARSET = 134,
CHINESEBIG5_CHARSET = 136,
OEM_CHARSET = 255,
JOHAB_CHARSET = 130,
HEBREW_CHARSET = 177,
ARABIC_CHARSET = 178,
GREEK_CHARSET = 161,
TURKISH_CHARSET = 162,
VIETNAMESE_CHARSET = 163,
THAI_CHARSET = 222,
EASTEUROPE_CHARSET = 238,
RUSSIAN_CHARSET = 204,
MAC_CHARSET = 77,
BALTIC_CHARSET = 186
}

public enum ClipPrecision
{
CLIP_DEFAULT_PRECIS = 0,
CLIP_CHARACTER_PRECIS = 1,
CLIP_STROKE_PRECIS = 2
}

public enum OutputPrecision
{
OUT_DEFAULT_PRECIS = 0,
OUT_STRING_PRECIS = 1,
OUT_CHARACTER_PRECIS = 2,
OUT_STROKE_PRECIS = 3,
OUT_TT_PRECIS = 4,
OUT_DEVICE_PRECIS = 5,
OUT_RASTER_PRECIS = 6,
OUT_TT_ONLY_PRECIS = 7,
OUT_OUTLINE_PRECIS = 8,
OUT_SCREEN_OUTLINE_PRECIS = 9
}

[Flags]
public enum PitchAndFamily
{
DEFAULT_PITCH = 0,
FIXED_PITCH = 1,
VARIABLE_PITCH = 2,
MONO_FONT = 8,
FF_DONTCARE = (0<<4),
FF_ROMAN = (1<<4),
FF_SWISS = (2<<4),
FF_MODERN = (3<<4),
FF_SCRIPT = (4<<4),
FF_DECORATIVE = (5<<4)
}

public enum Quality
{
DEFAULT_QUALITY = 0,
DRAFT_QUALITY = 1,
PROOF_QUALITY = 2,
NONANTIALIASED_QUALITY = 3,
ANTIALIASED_QUALITY = 4
}

public enum Weight
{
FW_DONTCARE = 0,
FW_THIN = 100,
FW_EXTRALIGHT = 200,
FW_LIGHT = 300,
FW_NORMAL = 400,
FW_MEDIUM = 500,
FW_SEMIBOLD = 600,
FW_BOLD = 700,
FW_EXTRABOLD = 800,
FW_HEAVY = 900,
FW_ULTRALIGHT = FW_EXTRALIGHT,
FW_REGULAR = FW_NORMAL,
FW_DEMIBOLD = FW_SEMIBOLD,
FW_ULTRABOLD = FW_EXTRABOLD,
FW_BLACK = FW_HEAVY
}

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;
}

...

LOGFONT lf = new LOGFONT();
lf.lfCharSet = (byte)LOGFONT.CharacterSet.DEFAULT_CHARSET;
lf.lfClipPrecision =
(byte)LOGFONT.ClipPrecision.CLIP_DEFAULT_PRECIS;
lf.lfEscapement = 0;
lf.lfFaceName = "Arial";
lf.lfHeight = -(int)((points * dpi.Height)/72.0F);
lf.lfItalic = 0;
lf.lfOrientation = 0;
lf.lfOutPrecision =
(byte)LOGFONT.OutputPrecision.OUT_DEFAULT_PRECIS;
lf.lfPitchAndFamily = (byte)(LOGFONT.PitchAndFamily.DEFAULT_PITCH
| LOGFONT.PitchAndFamily.FF_DONTCARE);
lf.lfQuality = (byte)LOGFONT.Quality.DEFAULT_QUALITY;
lf.lfStrikeOut = 0;
lf.lfUnderline = 0;
lf.lfWeight = (int)LOGFONT.Weight.FW_BOLD;
lf.lfWidth = 0;
Font font = Font.FromLogFont(lf);


I am using GraphicsUnit.Pixel, the default transform and the default
settings for all other Graphics settings. The results of other
graphics drawing operations via C# .NET are being offset from the
upper left hand corner of the device context the same # of pixels as
they were with the Win32 GDI API.


Thanks to anyone who replies. (Please reply to the newgroup instead of
to my yahoo e-mail account, which I never check.)

DrDevious
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top