C# LOGFONT does not rotate

  • Thread starter Thread starter Tom Kazimiers
  • Start date Start date
T

Tom Kazimiers

Hi there,

I need to rotate some Text and want to use LOGFONT for this. The
reason for this decision is that the App is compiled for compact
framwork but should also be compatible to the desktop. This means I
cannot use RotateTransform. So I took the LOGFONT-Structore from
pinvoke.net:
http://www.pinvoke.net/default.aspx/Structures/LOGFONT.html

I could instantiate a new Font like this:

LOGFONT lf = new LOGFONT();

int curDPI = (int)context.DpiY;
lf.lfHeight = (int)(-10f * context.DpiY / curDPI);
lf.lfCharSet = FontCharSet.DEFAULT_CHARSET;
lf.lfOutPrecision = FontPrecision.OUT_DEFAULT_PRECIS;
lf.lfClipPrecision = FontClipPrecision.CLIP_DEFAULT_PRECIS;
lf.lfQuality = FontQuality.DEFAULT_QUALITY;
lf.lfPitchAndFamily = FontPitchAndFamily.FF_DONTCARE;
lf.lfFaceName = "Tahoma\0";
lf.lfEscapement = 2700;
lf.lfOrientation = lf.lfEscapement;

font = Font.FromLogFont((object)lf);

This gets me a new font, but if I use DrawString() with this font it
gets not rotated.

If I P/Invoke the GetFontIndirect-API the same results happen:

IntPtr handle = CreateFontIndirect(lf);

if (handle == IntPtr.Zero)
throw new ApplicationException("Could not create logical font.");

font = Font.FromHfont(handle);

In the compact framwork there is a already a LOGFONT class that I can
use, and with the same settings there it works.

Have you any hint what could be wrong with my approach?

Thanks in advance,
Tom
 
For your interest, it seems that DrawString just ignors the
orientation on the desktop framework.

If you have any other hints ... it would be nice :)

Thanks,
Tom
 
Whilst I haven't written specifically for the compact framework, I have
previously had to draw rotated text via GDI and came up against the same
problems that you are facing. I found that some Fonts would rotate using
LOGFONT but others would not. I resolved my problem by drawing text to a
memory bitmap, rotating the bitmap and drawing the rotated bitmap to the
graphics object.
 
Thanks for the hint and the information :)

Whilst I haven't written specifically for the compact framework, I have
previously had to draw rotated text via GDI and came up against the same
problems that you are facing. I found that some Fonts would rotate using
LOGFONT but others would not. I resolved my problem by drawing text to a
memory bitmap, rotating the bitmap and drawing the rotated bitmap to the
graphics object.
 
Back
Top