2010-12-16 16 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

+0

最初は近寄りましたが、遠くに遠くに変えたいのですが、 –

+0

グラフィックスを変換すると、全体の「世界」が変換されて、近くは実際には同じではありません。あなたはあなたが望む位置にそれを設定できませんか? – Aliostad

答えて

23

誰かがヒントを探していた場合、ここではStringAligmentが動作する0度、90度、180度、270度、360度の回転のソリューションがあります。

原点を移動するための正しい点を選ぶことと、回転に応じて表示矩形を変更することがありました。

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イベントに配置すると、回転フォームのタイトルが4回表示されます。

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(); 
関連する問題