2010-12-16 85 views
7

我需要能够旋转标签中的文本并将其与左侧,右侧或中心对齐。到目前为止,我可以在派生标签的onPaint方法中使用此代码进行旋转:在C中对齐旋转文本#

float width = graphics.MeasureString(Text, this.Font).Width; 
float height = graphics.MeasureString(Text, this.Font).Height; 

double angle = (_rotationAngle/180) * Math.PI; 
graphics.TranslateTransform(
    (ClientRectangle.Width + (float)(height * Math.Sin(angle)) - (float)(width * Math.Cos(angle)))/2, 
    (ClientRectangle.Height - (float)(height * Math.Cos(angle)) - (float)(width * Math.Sin(angle)))/2); 
graphics.RotateTransform(270f); 
graphics.DrawString(Text, this.Font, textBrush, new PointF(0,0), stringFormat); 
graphics.ResetTransform(); 

而且它工作正常。我可以看到文字旋转270度。

但是,当我尝试在stringFormat中设置对齐时,它变得疯狂,我无法弄清楚发生了什么。

我怎样才能让文字旋转270度并将其对齐?

+0

你设置什么定位? – Aliostad 2010-12-16 11:28:58

+0

近乎乍一看,但是当我改变图形时,我想改变为远和中心 – 2010-12-16 11:35:30

+0

,整个“世界”得到改变,所以附近不是很近。你能不能把它放在你想要的位置? – Aliostad 2010-12-16 13:51:44

回答

23

如果有人正在寻找提示,这里是解决方案0,90,180,270和360度旋转,其中StringAligment的作品。

有一件事是选择移动原点到正确的点,第二个是根据旋转修改显示矩形。

StringFormat format = new StringFormat(); 
format.Alignment = StringAlignment.Center; 

SizeF txt = e.Graphics.MeasureString(Text, this.Font); 
SizeF sz = e.Graphics.VisibleClipBounds.Size; 

//90 degrees 
e.Graphics.TranslateTransform(sz.Width, 0); 
e.Graphics.RotateTransform(90); 
e.Graphics.DrawString(Text, this.Font, Brushes.Black, new RectangleF(0, 0, sz.Height, sz.Width), format); 
e.Graphics.ResetTransform(); 

//180 degrees 
e.Graphics.TranslateTransform(sz.Width, sz.Height); 
e.Graphics.RotateTransform(180); 
e.Graphics.DrawString(Text, this.Font, Brushes.Black, new RectangleF(0, 0, sz.Width, sz.Height), format); 
e.Graphics.ResetTransform(); 

//270 degrees 
e.Graphics.TranslateTransform(0, sz.Height); 
e.Graphics.RotateTransform(270); 
e.Graphics.DrawString(Text, this.Font, Brushes.Black, new RectangleF(0, 0, sz.Height, sz.Width), format); 
e.Graphics.ResetTransform(); 

//0 = 360 degrees 
e.Graphics.TranslateTransform(0, 0); 
e.Graphics.RotateTransform(0); 
e.Graphics.DrawString(Text, this.Font, Brushes.Black, new RectangleF(0, 0, sz.Width, sz.Height), format); 
e.Graphics.ResetTransform(); 

如果您将此代码放在标签的OnPaint事件中,它将显示您的旋转表单的标题四次。

0

扩展阿德里安塞拉芬的回答,如果你需要在一个非0 X画,Y:

//90 degrees 
e.Graphics.TranslateTransform(sz.Width, 0); 
e.Graphics.RotateTransform(90); 
e.Graphics.DrawString(Text, this.Font, Brushes.Black, 
    new RectangleF(sz.ToPointF().Y, sz.ToPointF().X, sz.Height, sz.Width), format); 
e.Graphics.ResetTransform(); 
//180 degrees 
e.Graphics.TranslateTransform(sz.Width, sz.Height); 
e.Graphics.RotateTransform(180 this.Font, Brushes.Black, 
    new RectangleF(-sz.ToPointF().X, -sz.ToPointF().Y, sz.Width, sz.Height), format); 
e.Graphics.ResetTransform(); 
//270 degrees 
e.Graphics.TranslateTransform(0, sz.Height); 
e.Graphics.RotateTransform(270); 
e.Graphics.DrawString(Text, this.Font, Brushes.Black, 
    new RectangleF(-sz.ToPointF().Y, sz.ToPointF().X, sz.Height, sz.Width), format); 
//0 = 360 degrees 
e.Graphics.TranslateTransform(0, 0); 
e.Graphics.RotateTransform(0); 
e.Graphics.DrawString(Text, this.Font, Brushes.Black, 
    new RectangleF(sz.ToPointF().X, sz.ToPointF().Y, sz.Width, sz.Height), format); 
e.Graphics.ResetTransform();