如何计算 Metro 中字符串的宽度(不显示)? [英] How can I calculate the width of a string in Metro (without displaying it)?

查看:60
本文介绍了如何计算 Metro 中字符串的宽度(不显示)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我未能找到与以下 WPF 代码等效的 Windows 运行时来测量字符串的宽度:

I have failed to find a Windows Runtime equivalent to the following WPF code to measure the width of a string:

FormattedText formattedText = new FormattedText(in_string,in_culture,in_flowdir,in_font,in_sz,in_color);
string_width = formattedText.WidthIncludingTrailingWhitespace);

有人知道地铁能不能办?

Does anybody know if it can be done in Metro?

推荐答案

有可能,我找到了一种提供有用测量的方法,但我不确定这是最好的方法:

It is possible, I've found one method that gives useful measurements, but I am not sure it is the best way of doing it:

// Setup the TextBlock with *everything* that affects how it 
// will be drawn (this is not a complete example)
TextBlock^ tb = ref new TextBlock; 
tb->VerticalAlignment = Windows::UI::Xaml::VerticalAlignment::Top; 
tb->HorizontalAlignment = Windows::UI::Xaml::HorizontalAlignment::Left; 
tb->Height = in_height; 
tb->Text = text;

// Be sure to tell Measure() the correct dimensions that the TextBox 
// have to draw in!
tb->Measure(SizeHelper::FromDimensions(Parent->Width,Parent->Height)); 
text_width = tb->DesiredSize.Width;

我的直觉是,在某些情况下,这段代码会产生意想不到的结果.

My gut feeling is that there are situations in which this code will give an unexpected result.

这篇关于如何计算 Metro 中字符串的宽度(不显示)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆