[英]How to rotate a list of strings along an axis?
我有一個字符串列表,我在軸上繪制。 我想旋轉琴弦(再次,旋轉時它們應保持在同一水平軸上)。 我想使用以下代碼:
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();
}
}
}
}
但它不起作用,因為Graphics一次旋轉兩個字符串,導致一個字符串高於另一個字符串。 如何使用字符串的中心作為旋轉軸單獨旋轉它們?
聽起來你正在描述這樣的事情:
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軸上以相同的角度旋轉,但是源自不同的點。
關於什么
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();
}
旋轉平面后,您將繪制弦線的位置
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.