Weiwei
Weiwei

Reputation: 55

How can I rotate text or a number 90° in Windows Forms/C++?

I want to rotate a block of text or a number 90° in a Windows Forms application, Visual Studio 2010 (C++). Is there an easy way to do this?

Upvotes: 0

Views: 2111

Answers (2)

user1233963
user1233963

Reputation: 1490

You must create a LOGFONT and play with the values of lfEscapement and lfOrientation, like so:

SetGraphicsMode(hdc, GM_ADVANCED);

LOGFONT font          = {0};

font.lfHeight         = 0;
font.lfWidth          = 0;
font.lfEscapement     = 900; // here
font.lfOrientation    = 900; // and here
font.lfWeight         = 0;
font.lfItalic         = false;
font.lfUnderline      = false;
font.lfStrikeOut      = false;
font.lfCharSet        = DEFAULT_CHARSET;
font.lfOutPrecision   = OUT_DEFAULT_PRECIS;
font.lfClipPrecision  = CLIP_DEFAULT_PRECIS;
font.lfQuality        = CLEARTYPE_QUALITY;
font.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;

auto newfont = CreateFontIndirect(&font);
auto oldfont = SelectObject(hdc, newfont);

/* do actual drawing here */

SelectObject(hdc, oldfont);

SetGraphicsMode(hdc, GM_COMPATIBLE);

DeleteObject(newfont);

Upvotes: 1

Pankaj
Pankaj

Reputation: 2754

String theString = "90 Degree Rotated Text";
SizeF sz = e.Graphics.VisibleClipBounds.Size;
//Offset the coordinate system so that point (0, 0) is at the center of the desired area.
e.Graphics.TranslateTransform(sz.Width / 2, sz.Height / 2);

//Rotate the Graphics object.
e.Graphics.RotateTransform(90);
sz = e.Graphics.MeasureString(theString, this.Font);

//Offset the Drawstring method so that the center of the string matches the center.
e.Graphics.DrawString(theString, this.Font,
Brushes.Black, -(sz.Width/2), -(sz.Height/2));

//Reset the graphics object Transformations.
e.Graphics.ResetTransform();

Copied from here

Upvotes: 0

Related Questions