如何沿轴旋转一个字符串列表?

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

但是它不起作用,因为图形同时旋转这两个字符串,导致一个字符串比另一个字符串高。我该如何使用字符串中心作为旋转轴来单独旋转它们?


不确定如何解决您的问题,但我猜使用TranslateClip可能会有所帮助(在其他框架中,频繁地进行翻译有助于改变旋转中心)。 - luiscubal
2个回答

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

关于什么?

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

在旋转平面后,您将在指定位置绘制字符串。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接