4

With Direct2D, I can use ID2D1RenderTarget::DrawText to draw text, but how can I get the text extent before I draw? Note that I wish to do this under Windows 8 RT.

Thanks

zdd
  • 8,258
  • 8
  • 46
  • 75
John Gaby
  • 1,500
  • 3
  • 19
  • 33
  • Maybe `Font.MeasureString` ([doc](http://msdn.microsoft.com/en-us/library/windows/desktop/bb296736%28v=vs.85%29.aspx)) will help you out, but I've never tested it. – Gnietschow Dec 20 '12 at 21:56
  • I think this can help you, http://stackoverflow.com/questions/13587532/determine-text-boundary-box-with-direct2d-directwrite – zdd Dec 21 '12 at 01:10

1 Answers1

6

Thanks for the feedback. I have found the solution. I needed to create a IDWriteTextLayout for the text block and then call it's GetMetrics method which will return the extent of the text.

Thanks again for the help.

John Gaby
  • 1,500
  • 3
  • 19
  • 33
  • 3
    Yes, CreateTextLayout + GetMetrics yields the DWRITE_TEXT_METRICS::width and height field. Note that if you create an IDWriteTextLayout, you should then just pass that created IDWriteTextLayout to DrawTextLayout instead of calling DrawText again (which would unnecessarily need to lay out the text a second time). – Dwayne Robinson Sep 21 '13 at 00:33