본문 바로가기
Delphi Tip/컴포넌트

델파이 콤보박스(TComboBox)의 팝업리스트 가로폭 늘리기

by MonoSoft 2023. 10. 26.
728x90
반응형

델파이 콤보박스(TComboBox)

 

728x90

 

 

procedure TMain_Form.ComboBox1DropDown(Sender: TObject);

begin

  SendMessage( TComboBox( Sender ).Handle, CB_SETDROPPEDWIDTH, 300, 0 );

end;

 

 

추가 TiP

콤보박스 글자까지 확인해서 글자의 최대값으로 컴보박스 크기를 조절하는것입니다.

 

procedure TForm1.AdjustDropDownWidth(Sender: TObject);

var

  j,i,ItemWidth, IW: Integer;

begin

  For j := ComponentCount-1 Downto 0 do

  Begin

    ItemWidth := 0;

    If (Components[j] is TCombobox) Then

    Begin

      for i := 0 to TComboBox(Components[j]).Items.Count - 1 do

      begin

        if GetTextWidth(TComboBox(Components[j]).Items[i]) > ItemWidth then

        Begin

          ItemWidth := GetTextWidth(TComboBox(Components[j]).Items[i]);

          IW := trunc(ItemWidth / 8 * TComboBox(Components[j]).Font.Size) + 10;

          //GetTextWidth는 글자 폰트가 8포인트일때 크기 값을 줍니다.

          // 그래서 현 컴보박스의 글짜 크기를 다시 받아서 재계산한것임.

          TComboBox(Components[j]).Perform(CB_SETDROPPEDWIDTH, IW, 0);

        End;

      end;

    end;

  end;

end;

 

function TForm1.GetTextWidth(S: String): Integer;

begin

  Result := Canvas.TextWidth(S);

end;

 

Create시 1회 실행!!!!!

 

추가 TIP

어느 콤보가 눌렸는지 확인하기 위해서 폼에 있는 모든 콤보박스를 다 확인할 필요는 없다.

이벤트 발생시 넘어오는 Sender 객체를 이용하면 된다.

 

var

  i, ItemWidth, MaxWidth : Integer;

begin

    ItemWidth := 0;

 

  with Sender as TComboBox do

  begin

    for i := 0 to Items.Count - 1 do

    begin

      ItemWidth := Canvas.TextWidth(Items[i]) + 30;

      if MaxWidth < ItemWidth then MaxWidth := ItemWidth;

    end;

 

    Perform(CB_SETDROPPEDWIDTH, ItemWidth, 0);

  end;

end;

 

728x90
반응형

댓글