gpt4 book ai didi

c# - 在 DrawText 中设置 VerticalAlignment

转载 作者:太空狗 更新时间:2023-10-29 23:07:36 26 4
gpt4 key购买 nike

我正在使用 DrawText 在可视层上绘制 FormattedText。现在,我使用下面的代码来定义格式化文本,并且能够将 TextAlignment 设置为 Center。但是 VerticalAlignment 呢?正如您在下图中看到的,文本的中心不在此处用红点显示的中心点

我定义 FormattedText 的部分:

var ft = new FormattedText("A",
CultureInfo.GetCultureInfo("en-us"),
FlowDirection.LeftToRight,
new Typeface("Verdana"),
36, Brushes.Yellow);

ft.TextAlignment = TextAlignment.Center;

我正在绘制文本的部分:

var centerpoint = new Point(0,0);
dc.DrawText(ft, centerpoint);

这是最终结果:

enter image description here

我希望文本的中间位于圆的中心。

最佳答案

好吧,看来我能够解决这个问题。这并不难。我会在这里发布答案以供将来引用。它也可能对其他人有帮助。

对于 FormattedText 似乎没有 VerticalAlignment 这样的东西,所以我们需要自己计算和定位它。由于我们可以获得格式化文本的 Height 属性。我们可以像这样轻松对齐文本:

dc.DrawText(ft, new Point(centerpoint.X, centerpoint.Y- ft.Height/2));

Here is the result

关于c# - 在 DrawText 中设置 VerticalAlignment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22780217/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com