gpt4 book ai didi

wpf - 如何在 wpf 中计算 FormattedText 或 GlyphRun 的 'Size'?

转载 作者:bug小助手 更新时间:2023-10-28 10:52:03 27 4
gpt4 key购买 nike

在 WPF4 中,我如何为 drawingvisual 计算 FormattedTextGlyphRun'Size'

我在 Canvas 中使用绘图视觉。当我更改文本大小或文本时,会发生更改,但实际宽度和高度相同或未更新。

Using dc As DrawingContext = drawingvisual.RenderOpen                    

Dim ft As New FormattedText(...)

dc.DrawText(ft, New Point(0, 0))

dc.Close()

End Using

最佳答案

初始化 FormattedText 后,它的 Width 和 Height 成员等于给定参数的实际渲染大小。事实上,更改参数会立即更新它们,例如:

FormattedText ft = new FormattedText(cellString, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, fontFace, fontSize, fontBrush);
ft.Trimming = TextTrimming.CharacterEllipsis;
double originalHeight = ft.Height;
double width = ft.Width;
ft.MaxTextWidth = bCellRect.Width; // Set the width to get a new height
double height = ft.Height;

为 GlyphRun 编辑:当您创建 GlyphRun 时,您已经为每个字符指定了预先宽度,因此您添加了这些宽度。要获取高度,请使用 GlyphTypeface.Baseline * FontSize

关于wpf - 如何在 wpf 中计算 FormattedText 或 GlyphRun 的 'Size'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6717199/

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