GetTextExtent( ) returns inaccurate pixels

If you try the code, you'll find it.

extentName=pCDC->GetTextExtent(LongName,strlen(LongName));
if(strlen(LongName)>30){
  m_listCtrl.SetHorizontalExtent(extentName.cx);
      }
Note:30 is the characters listbox can hold.

The more long string is, the more obvious inaccuracy is.

Any idea?  
henry3Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cyberfrankCommented:
Hi!

The main problem is, that fonts can be different in CDC (through the pCDC pointer) and in the listbox.
Try to synchronize fonts and the problem will be solved.
Or You can try:
CListBox::SetColumnWidth(int cxWidth );
Also, maybe you have problem with scrollbar himself. Try to set scroll range. Use the CListBox's
void SetScrollRange( int nBar, int nMinPos, int nMaxPos, BOOL bRedraw = TRUE );
function where the nBar is:
SB_HORZ  

CF
0
migelCommented:
Hi!
Try sync DC ascyberfranc said and also try DrawText with DT_CALCRECT param
0
SteveGTRCommented:
Try this:

CFont* pOldFont = pCDC->SelectObject(m_listCtrl.GetFont());
extentName=pCDC->GetTextExtent(LongName,strlen(LongName));
pCDC->SelectObject(pOldFont);

This will size the text with the listbox's font.

Good luck,
Steve
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
henry3Author Commented:
Thanks Everybody, and Happy New Year!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.