2013-08-23 61 views
0

我想在Windows窗體應用程序,Visual Studio 2010(C++)中旋轉一段文字或數字90°。是否有捷徑可尋?如何在Windows Forms/C++中旋轉文本或數字90°?

+0

* [C#標籤的垂直的可能的複製在Windows窗體](http://stackoverflow.com/questions/1371943/c-sharp-vertical-label-in-a-windows-forms)*。 –

回答

0
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(); 

here

+0

這不是C++ –

+0

OOPS,我只注意到了Winforms標籤.. 我這個應該有幫助.. http://www.codeproject.com/Articles/740/Simple-text-rotation – Pankaj

+0

嗨,非常感謝你爲了您的答案,我在哪裏放置代碼?我在這個事件private:System :: Void label37_Paint(System :: Object^sender,System :: Windows :: Forms :: PaintEventArgs^e){}中試過它,它沒有工作。 Windows窗體不明白e。非常感謝你! – Weiwei

1

複製您必須創建一個LOGFONT並與lfEscapement的價值觀和lfOrientation玩,就像這樣:

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