본문 바로가기

MS/MFC

Manually set dropdown list for combobox on MFC


[VC++, MFC] How to set ComboBox DropDown Height

This is a good example of how messy working with MFC can be. Something apparently so easy could take a lot of time to figure out, so here you can find two easy ways out:


1) Designer - through the designer by default you can just resize the ComboBox width. If you want to resize the Drop Down List height you need to click on the dropdown arrow on the right, then you'll be able to resize the dropped control height. This seems so easy but if no-one tells you it's anything but intuitive. If you just need to statically fix height this'll save you a lot of bitchin'.


2) Programmatically - use the following function to programmatically resize you dropped control height. The function takes as input the number of items you want to display, but if you wanna modify it to take pixels or whatever as parameter it shouldn't be too hard (unless you're brain damaged as Scuffia is).



void SetDropDownHeight(CComboBox* pMyComboBox, int itemsToShow)

{

  //Get rectangles

  CRect rctComboBox, rctDropDown;

  //Combo rect

  pMyComboBox->GetClientRect(&rctComboBox); 

  //DropDownList rect

  pMyComboBox->GetDroppedControlRect(&rctDropDown); 


  //Get Item height

  int itemHeight = pMyComboBox->GetItemHeight(-1); 

  //Converts coordinates

  pMyComboBox->GetParent()->ScreenToClient(&rctDropDown); 

  //Set height

  rctDropDown.bottom = rctDropDown.top + rctComboBox.Height() + itemHeight*itemsToShow; 

  //apply changes

  pMyComboBox->MoveWindow(&rctDropDown); 

}


출처 : http://stackoverflow.com/questions/10920790/manually-set-mfc-ccombobox-dropdown-height-with-horizontal-scrollbar