2010-09-14 61 views
3

我有一個我在軸上繪製的字符串列表。我想旋轉字符串(同樣,旋轉時它們應該保持在同一水平軸上)。 我嘗試使用下面的代碼:如何沿一個軸旋轉字符串列表?

namespace DrawString 
{ 
    struct StringData 
    { 
     public string StringName; 
     public int X; 
     public int Y; 
    } 

    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     Font mFont = new Font("Arial", 10.0f, FontStyle.Bold); 
     List<StringData> data = new List<StringData> { new StringData() { StringName = "Label1", X = 10, Y = 30 }, 
                new StringData() { StringName = "Label2", X = 130, Y = 30 }}; 

     private void Form1_Paint(object sender, PaintEventArgs e) 
     { 
      foreach (var str in data) 
      { 
       e.Graphics.RotateTransform(30); 
       e.Graphics.DrawString(str.StringName, mFont, new SolidBrush(Color.Black), new Point(str.X, str.Y)); 
       e.Graphics.ResetTransform(); 
      } 
     } 
    } 
} 

但由於圖形旋轉兩個字符串的一次,造成一個String要高於其他這是行不通的。如何以字符串的中心作爲旋轉軸單獨旋轉它們?

+0

不知道如何解決你的問題,但我猜使用TranslateClip可以幫助的東西一點點(以其他框架,翻譯經常有助於改變旋轉的中心) – luiscubal 2010-09-14 18:33:29

回答

1

這聽起來像你所描述的是這樣的:

private void Form1_Paint(object sender, PaintEventArgs e) 
{ 
    foreach (var str in data) 
    { 
     e.Graphics.TranslateTransform(str.X, str.Y); 
     e.Graphics.RotateTransform(30); 
     e.Graphics.DrawString(str.StringName, mFont, new SolidBrush(Color.Black), new Point(0, 0)); 
     e.Graphics.ResetTransform(); 
    } 
} 

這段代碼首先轉換,然後旋轉,在轉化原點繪製的字符串,然後復位。兩個琴絃在相同的X軸上以相同的角度旋轉,但是起源於不同的點。

+0

非常感謝。有效。 – Ioga3 2010-09-15 23:40:29

0

什麼

 private void Form1_Paint(object sender, PaintEventArgs e) 
     { 
      e.Graphics.RotateTransform(30); 
      foreach (var str in data) 
      { 
       e.Graphics.DrawString(str.StringName, mFont, new SolidBrush(Color.Black), new Point(str.X, str.Y)); 
      } 
      e.Graphics.ResetTransform(); 
     } 

面旋轉後,你會得出在位置字符串