CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2014
    Posts
    205

    Question width of combo box

    I have small space on Sheet page and lots of controls. I have combo boxes, which take place, and I need to make them slim, so if I click on it the items should expand. I need to auto-change the width of the items which expand to fit the width of the longest item. Is there some style for this or way how to do it?

    These styles I have applied:
    CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP
    Last edited by crazy boy; July 5th, 2015 at 07:40 AM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: width of combo box

    You want to change the size of the combo-box list box so that it fits the width of the longest item?

    This is possible. First you need to determine the required width of the list box. Then you process CBN_DROPDOWN notification (https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx) which happens when the list box of a combo control is about to become visible. Then use CB_GETCOMBOBOXINFO message (https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx) to obtain the HWND of the list box, then use GetWindowRect() and MoveWIndow() to resize the list box.

    This is an extract from some code (so won't compile but will give the general idea)
    Code:
    COMBOBOXINFO cbinfo;
    RECT rdraw;
    
    //Get handles to combo windows
    cbinfo.cbSize = sizeof(COMBOBOXINFO);
    SendMessage(hwndItem, CB_GETCOMBOBOXINFO, 0, (LPARAM) (COMBOBOXINFO*) &cbinfo);
    
    //Get position of current list box so new one starts at same position
    GetWindowRect(cbinfo.hwndList, &rdraw);
    
    //Make list window new required size
    MoveWindow(cbinfo.hwndList, rdraw.left, rdraw.top, comwidth, rdraw.bottom - rdraw.top, TRUE);
    
    //Set font for new list window
    SendMessage(cbinfo.hwndList, WM_SETFONT, (WPARAM)GetStockObject(ANSI_FIXED_FONT), TRUE);
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: width of combo box

    There is a CB_SETDROPPEDWIDTH message that can be used.
    Before sending it, it is necessary to obtain the max width of all the combo items. you could use GetTextExtentPoint32 or similat function...
    Victor Nijegorodov

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: width of combo box

    Quote Originally Posted by VictorN View Post
    There is a CB_SETDROPPEDWIDTH message that can be used.
    Note that this requires Vista or higher.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: width of combo box

    Quote Originally Posted by 2kaud View Post
    Quote Originally Posted by VictorN View Post
    There is a CB_SETDROPPEDWIDTH message that can be used.
    Note that this requires Vista or higher.
    Interesting!
    I didn't know such a requirement and used MFC CComboBox::SetDroppedWidth() which internally sends CB_SETDROPPEDWIDTH message) under Window98/NT more than a decade back...
    Victor Nijegorodov

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: width of combo box

    From my MSDN of October 2000:
    CB_SETDROPPEDWIDTH
    An application sends the CB_SETDROPPEDWIDTH message to set the maximum allowable width, in pixels, of the list box of a combo box with the CBS_DROPDOWN or CBS_DROPDOWNLIST style.
    ...
    Requirements
    Windows NT/2000: Requires Windows NT 4.0 or later.
    Windows 95/98: Requires Windows 95 or later.
    Header: Declared in Winuser.h; include Windows.h.
    Victor Nijegorodov

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: width of combo box

    Well that's just Microsoft! The current documentation states the minimum requirement is Vista. https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx Doh!!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: width of combo box

    I guess there was the "work" of some young MSFT employee that never heard about any system preceding Vista.
    Sure he/she does not know about punch cards either!

    PS: I have now "updated" this site:
    https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx
    Last edited by VictorN; July 6th, 2015 at 07:06 AM.
    Victor Nijegorodov

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: width of combo box

    Quote Originally Posted by VictorN View Post
    I guess there was the "work" of some young MSFT employee that never heard about any system preceding Vista.
    Sure he/she does not know about punch cards either!

    PS: I have now "updated this site:
    https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx
    The employee has been busy! eg CreateFile() is stated as minimum Windows XP and CreateWindow() as Windows 2000! Doh!!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured