在SharpDX / DirectWrite中获取ABC字符宽度

时间:2017-06-16 16:41:29

标签: c# sharpdx directwrite

在GDI中获取一个角色的ABC宽度(左方位,右方位等),我会称之为GetCharABCWidths

如何使用SharpDX或DirectWrite实现相同的测量,我是否应该期望这个值和GDI之间的值匹配相同的字符?

到目前为止我做了什么?因缺少Direct *和SharpDX的文档而迷失方向。

1 个答案:

答案 0 :(得分:4)

/// <summary>
///  Takes a string, text format, and associated constraints, and produces an object that represents the fully analyzed and formatted result.
/// </summary>
/// <param name="factory">an instance of <see cref="T:SharpDX.DirectWrite.Factory" /></param>
/// <param name="text">An array of characters that contains the string to create a new <see cref="T:SharpDX.DirectWrite.TextLayout" /> object from. This array must be of length stringLength and can contain embedded NULL characters.</param>
/// <param name="textFormat">A pointer to an object that indicates the format to apply to the string.</param>
/// <param name="maxWidth">The width of the layout box.</param>
/// <param name="maxHeight">The height of the layout box.</param>
/// <unmanaged>HRESULT CreateTextLayout([In, Buffer] const wchar* string,[None] UINT32 stringLength,[None] IDWriteTextFormat* textFormat,[None] FLOAT maxWidth,[None] FLOAT maxHeight,[Out] IDWriteTextLayout** textLayout)</unmanaged>
public TextLayout(Factory factory, string text, TextFormat textFormat, float maxWidth, float maxHeight)

示例代码:

using (var dwFactory = new SharpDX.DirectWrite.Factory())
{
  var textLayout = new TextLayout(dwFactory, "ABC", textFormat, float.PositiveInfinity, float.PositiveInfinity);
  var width = textLayout.Metrics.Width
  ...
}
相关问题