【问题标题】:Wpf text rotationWpf 文本旋转
【发布时间】:2017-10-23 18:54:12
【问题描述】:

我想在画布上添加一些文本并决定为此使用textBlock(设置字体等) 但我不知道如何旋转它。 我使用以下函数在myCanvas上添加myText

void text(double x_pos, double y_pos, string myText, double angle, Point rot_cen, Color color1)
        {
            TextBlock textBlock = new TextBlock()
            {
                Text = myText,
                FontFamily = new FontFamily("Verdana"),
                FontSize = 16,
                TextAlignment = TextAlignment.Center
            };
            textBlock.Foreground = new SolidColorBrush(color1);
            Canvas.SetLeft(textBlock, x_pos);
            Canvas.SetTop(textBlock, y_pos);

            textBlock.RenderTransform = new RotateTransform(angle, rot_cen.X, rot_cen.Y);

            myCanvas.Children.Add(textBlock);
        }

从我读到的rot_cen 是从(0,0)(左上角)到(1,1)(右下角)的一个点。但是当我将它设置为(0.5,0.5)时,它仍然围绕左上角旋转。我需要以某种方式更新它吗?

【问题讨论】:

    标签: wpf canvas rotatetransform


    【解决方案1】:

    RotateTransform 的 CenterXCenterY 属性使用绝对坐标。

    你可能想设置RenderTransformOrigin,它使用相对坐标:

    textBlock.RenderTransformOrigin = rot_cen;
    textBlock.RenderTransform = new RotateTransform(angle);
    

    【讨论】:

    • 非常感谢 - 这正是我想要的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 2021-05-11
    • 2013-05-18
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多