• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 357
  • Last Modified:

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?  
0
henry3
Asked:
henry3
1 Solution
 
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
 
henry3Author Commented:
Thanks Everybody, and Happy New Year!
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now