Solved

Setting width of ComboBox dropdown list

Posted on 1998-09-15
4
425 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
move a line in eclipse 3 107
ShiftLeft challenge 21 86
building a service to accessed via telnet client, want colors ? 4 94
Not needed 13 120
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 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