Solved

GetTextExtentPoint32() is slow

Posted on 2000-04-17
9
688 Views
Last Modified: 2013-12-03
I have an app that takes a user batch and generates a dialog.  In building the display  GetTextExtentPoint32() is called to determine if the next item (button/text/combo box/custom control)will fit on the current line.  In one particularly long batch this is getting called 4043 times.  I ran MSDevStudio profiler, and it indicates that this call is the main slowdown for the display generation.

Is there a way to speed this up, or is there a more efficient way to determine the horizontal extent of a string?

I have forced the function inline with little improvement.
Thank You
0
Comment
Question by:marvinm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 12

Accepted Solution

by:
pjknibbs earned 100 total points
ID: 2724299
GetTextExtentPoint32() does an awful lot of calculation to find the length of a string--it includes kerning between characters and other such esoteric items. Forcing it inline won't make the slightest difference because the function code is actually contained in the GDI and therefore can't physically be made inline, and it's the body of the function which is taking the time, not the call to it.

Unfortunately, your only way round this is to sacrifice the accuracy of GetTextExtentPoint32() and use an approximation. I had an application which had to format 200 pages of text; in order to make it do this quickly I wrote my own extent function which just added together all the widths of the characters making up the string: it built the character width table using GetTextExtentPoint32(), obviously, but once it had got them it worked out about 20 times faster! The small loss of accuracy wasn't noticeable for the fonts (Times, Arial, Courier) I was using for the app.
0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 2724303
Oh, BTW, for a monospaced font like Courier you can just use the width of a single character and multiply by the number of characters in the string, but I'm assuming you're using a variable font like Arial or Times.
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 2724457
Could you explain why do you use GetTextExtentPoint32 ?

May be, DrawText or clipping will be faster.
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 31

Expert Comment

by:Zoppo
ID: 2725821
Hi marvinm,

you can use GetCharWidth32() to recieve the width of each char of the font in an array which can later be used to simply calculate the length by yourself. This is much faster then GetTextExtentPoint32()

ZOPPO
0
 
LVL 22

Expert Comment

by:nietod
ID: 2726293
The "problem" with pjknib's and Zoppo's idea of doing the math yourself is that it doesn't take care of kerning calculations.  (Actually you could get the kerning information and use it, but then you probably woudl be running at the speed of GetTextExtentPoint32().)   If the string is reasonabley short and you can survive small innacuracies, this might not matter.  

However, it is possible that an alternative approach would be better.  What are you trying to do that you need to use GetTextExtent() so many times?
0
 
LVL 1

Author Comment

by:marvinm
ID: 2726572
The user created batch generates a display with checkboxes, text, buttons, combo boxes, and some other custom controls.  The display fits within our app.  When I generate the display, I need to know if the next item will fit on the curront line, or if I need to start a new line.  I use GetTextExtentPoint32() to determine the width of the next item to be placed and whether or not it fits on the current line.  I also use to break up large blocks of text so that as much as possible fits on the current line.
I think that the small innaccuracy involed in pjknibbs' and Zoppo's suggestion will not be a problem.  I will be working with today to see for sure.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2727048
Does your algorithm perform GetTextExtent() repeatedly in sutuations where it might not have to?  Like does it repeatedly measure the current line.  (like it might measure the line, add onto it, measure the new line again etc)  you might be able to avoid some occurances by storing some of the results from GetTextExtent()  (Allthough because of kerning, you theoretically can't add the length of two strings to get the length of the resultant string, but again that small difference might not matter.)
0
 
LVL 1

Author Comment

by:marvinm
ID: 2727091
It only calls it for the next item to add to the display.  I keep track of the current position and the window width.  So far it looks like getting the individual char widths once, and then adding up in place of GetTextExtentPoint32() will work fine and provide a substantial speed increase.
Thank You
0
 
LVL 1

Author Comment

by:marvinm
ID: 2728444
Thanks to all.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question