如何在Win2D

时间:2015-06-07 18:12:09

标签: c# windows win2d

我正在使用Win2D编写一个Windows 10应用程序,我正在尝试绘制一个动态缩放的形状,以适应其中的任何文本。

我想要做的是弄清楚给定CanvasTextFormat的特定字符串有多大,然后用它来设置形状的大小。

我的问题是我似乎无法找到解决字符串大小的方法吗?

2 个答案:

答案 0 :(得分:12)

请参阅下面的代码来计算所需的尺寸(查找" theRectYouAreLookingFor")

private void CanvasControl_Draw(CanvasControl sender, CanvasDrawEventArgs args)
{
    CanvasDrawingSession drawingSession = args.DrawingSession;
    float xLoc = 100.0f;
    float yLoc = 100.0f;
    CanvasTextFormat format = new CanvasTextFormat {FontSize = 30.0f, WordWrapping = CanvasWordWrapping.NoWrap};        
    CanvasTextLayout textLayout = new CanvasTextLayout(drawingSession, "Hello World!", format, 0.0f, 0.0f);
    Rect theRectYouAreLookingFor = new Rect(xLoc + textLayout.DrawBounds.X, yLoc + textLayout.DrawBounds.Y, textLayout.DrawBounds.Width, textLayout.DrawBounds.Height);
    drawingSession.DrawRectangle(theRectYouAreLookingFor, Colors.Green, 1.0f);
    drawingSession.DrawTextLayout(textLayout, xLoc, yLoc, Colors.Yellow);
}

答案 1 :(得分:0)

如果使用CanvasTextLayout的{​​{1}}创建requestedWidth,就像0的示例一样,则可能要在Win2D 1.23中禁用自动换行。喜欢:

Michael Vach

(不允许发表评论)