Mega Code Archive

 
Categories / Delphi / VCL
 

How to show multiline Text in a TCombobox (alternativ)

Title: How to show multiline Text in a TCombobox (alternativ) procedure TForm1.ComboBox1MeasureItem(Control: TWinControl; Index: Integer; var Height: Integer); var ItemString: string; MyRect: TRect; MyImage: TImage; MyCombo: TComboBox; begin // Don't waste time with this on Index = -1 if (Index -1) then begin MyCombo := TComboBox(Control); // Create a temporary canvas to calculate the height MyImage := TImage.Create(MyCombo); try MyRect := MyCombo.ClientRect; ItemString := MyCombo.Items.Strings[Index]; MyImage.Canvas.Font := MyCombo.Font; // Calc. using this ComboBox's font size Height := DrawText(MyImage.Canvas.Handle, PChar(ItemString), - 1, MyRect, DT_CALCRECT or DT_WORDBREAK); finally MyImage.Free; end; end; end; procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); var ItemString: string; begin TComboBox(Control).Canvas.FillRect(Rect); ItemString := TComboBox(Control).Items.Strings[Index]; DrawText(TComboBox(Control).Canvas.Handle, PChar(ItemString), - 1, Rect, DT_WORDBREAK); end;