我在西雅图德尔福有一个所有者描述的TComboBox
,它忽略了DropDownCount
属性的任何设置。单个项目的高度为 59 像素,宽度为 311 像素。我在Items
字符串列表中有 5 个条目。将值设置为低于 8 会导致根本不显示下拉列表,而任何更高的值都会导致下拉列表中仅显示 1 个项目以及垂直滚动条。我需要能够在下拉列表中的项目列表中显示所有 5 个条目。
以下是重现问题的 DFM:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 223
ClientWidth = 527
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object ComboBox1: TComboBox
Left = 96
Top = 16
Width = 311
Height = 22
Style = csOwnerDrawVariable
ItemIndex = 0
TabOrder = 0
Text = 'ITEM ONE'
OnDrawItem = ComboBox1DrawItem
OnMeasureItem = ComboBox1MeasureItem
Items.Strings = (
'ITEM ONE'
'ITEM TWO'
'ITEM THREE'
'ITEM FOUR'
'ITEM FIVE')
end
end
这是 PAS 文件:
unit CBBoxBugUnit;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
ComboBox1: TComboBox;
procedure ComboBox1MeasureItem(Control: TWinControl; Index: Integer; var Height: Integer);
procedure ComboBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
var Working_String : String;
Text_Width ,
Text_Height ,
Output_X ,
Output_Y : Integer;
begin
ComboBox1.Canvas.Pen.Color := clWhite;
ComboBox1.Canvas.Brush.Color := clGreen;
ComboBox1.Canvas.Brush.Style := bsSolid;
ComboBox1.Canvas.Pen.Style := psSolid;
ComboBox1.Canvas.Pen.Width := 1;
ComboBox1.Canvas.Font.Color := clWhite;
ComboBox1.Canvas.Font.Name := 'ARIAL BLACK';
ComboBox1.Canvas.Font.Size := 14;
ComboBox1.Canvas.FillRect( Rect );
Working_String := self.ComboBox1.Items.Strings[ Index ];
Text_Width := self.ComboBox1.Canvas.TextWidth( Working_String );
Text_Height := self.ComboBox1.Canvas.TextHeight( Working_String );
Output_X := ( Rect.Width - Text_Width ) div 2;
Output_Y := ( Rect.Height - Text_Height ) div 2;
ComboBox1.Canvas.TextOut( Output_X , Output_Y , Working_String );
end;
procedure TForm1.ComboBox1MeasureItem(Control: TWinControl; Index: Integer; var Height: Integer);
begin
Height := 59;
end;
end.
尽管TComboBox
ItemHeight
的文档指出:
。 如果 Style 为 csOwnerDrawVariable,则 ItemHeight 为默认高度 对于图形列表项,但此值可由 OnMeasureItem 事件处理程序。...
VCL 具有取决于 FItemHeight
的内部计算。将 ItemHeight
设置为 59,将遵循DropDownCount
(它已被接受,但默认为 16)。如果在设计时设置该值,则组合还会将其大小调整为运行时的大小。