C#,Delphi,Oracle,MSSQL 개발자블로그

델파이 콤보박스 아이템 사이즈(Item Widths) 늘리기. 본문

Programming/Delphi

델파이 콤보박스 아이템 사이즈(Item Widths) 늘리기.

19760323 2019. 6. 12. 19:31

콤보박스 사용 시 콤보박스 자체의 크기는 작지만, 내부 항목의 길이는 넓은 경우가 있다.

이 때, 방법은 콤보박스 자체의 크기를 늘리면 되지만, 콤보박스가 올라가는 폼에서 공간이 협소해 콤보박스

자체의 크기를 늘리기 어려울 때가 있다.

 

이 때, 콤보박스의 크기는 그대로 두고, 내부 아이템의 크기만 늘리는 방법이다.

적용 전과 후의 차이는 다음 캡쳐와 같다.

적용 전
적용 후

소스는 다음과 같다.

 

procedure TForm1.ComboBox_AutoWidth(const theComboBox: TAdvComboBox);
const
HORIZONTAL_PADDING = 15;
var
   itemsFullWidth: integer;
   idx: integer;
   itemWidth: integer;
begin
   itemsFullWidth := 0;
   // get the max needed with of the items in dropdown state
   for idx := 0 to -1 + theComboBox.Items.Count do
   begin
      itemWidth := theComboBox.Canvas.TextWidth(theComboBox.Items[idx]);
      Inc(itemWidth, 2 * HORIZONTAL_PADDING);
      if (itemWidth > itemsFullWidth) then itemsFullWidth := itemWidth;
   end;
   // set the width of drop down if needed
   if (itemsFullWidth > theComboBox.Width) then
      begin
      //check if there would be a scroll bar
      if theComboBox.DropDownCount < theComboBox.Items.Count then
      itemsFullWidth := itemsFullWidth + GetSystemMetrics(SM_CXVSCROLL);
      SendMessage(theComboBox.Handle, CB_SETDROPPEDWIDTH, itemsFullWidth, 0);
      end;
   end;

 

출처 : https://www.thoughtco.com/sizing-the-combobox-drop-down-width-1058301

Comments