Solved

Setting width of ComboBox dropdown list

Posted on 1998-09-15
4
419 Views
Last Modified: 2013-11-20
I have a problem when resizing the drop-down box in a combobox.  I calculate the length of the longest string that I am inserting with GetTextExtentPoint32(), then set the size with a CB_SETDROPPEDWIDTH message.  The list size does change, but it's always significantly wider than the longest string in the list.  To test, on the CBN_DROPDOWN notification, I check the width with CB_GETDROPPEDWIDTH, then iterate through each string in the list to check size.  They are the same, respectively, as when I set them; CB_GETDROPPEDWIDTH returns the same number as the longest string in the list, but the displayed list box is much wider than that string.

This is the code to set the width:
hDC = GetDC(m_handle);
GetTextExtentPoint32(hDC, pszText, strlen(pszText), &size);
ReleaseDC(m_handle, hDC);
SendMessage(m_handle, CB_SETDROPPEDWIDTH, (WPARAM) size.cx, 0L);
0
Comment
Question by:joeslow
  • 2
4 Comments
 

Author Comment

by:joeslow
ID: 1322168
Adjusted points to 60
0
 
LVL 4

Accepted Solution

by:
piano_boxer earned 60 total points
ID: 1322169
The calculation of the width is wrong because the current selected font is wrong.

Just after the GetDC() call, set the font to the same as the parent:

SelectObject(hDC, GetFont(GetParent(m_handle)));

or

Just select a stock font:

SelectObject(hDC, GetStockObject(ANSI_VAR_FONT));
0
 
LVL 1

Expert Comment

by:Grailman
ID: 1322170
'size.cx' will probably not be accurate for very much text. A quick & dirty way to help is to subtract 1 unit per character when you set the box width:

SendMessage(m_handle, CB_SETDROPPEDWIDTH, (WPARAM) size.cx-strlen(pszText), 0L);

This seems to work pretty well if the string is not too long.

0
 
LVL 1

Expert Comment

by:Grailman
ID: 1322171
'size.cx' will probably not be accurate for very much text. A quick & dirty way to help is to subtract 1 unit per character when you set the box width:

SendMessage(m_handle, CB_SETDROPPEDWIDTH, (WPARAM) size.cx-strlen(pszText), 0L);

This seems to work pretty well if the string is not too long.

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Login Script to Copy Folders 12 21
countEvens challenge 2 58
either24  challenge 19 84
zeroFront challenge 7 71
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

758 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now