使用ownerdraw样式时,Delphi Seattle TCombobox Dropdowncount无法正常工作

时间:2016-02-01 04:22:09

标签: delphi delphi-10-seattle tcombobox

我在Delphi Seattle中有一个所有者绘制的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.

1 个答案:

答案 0 :(得分:0)

尽管ItemHeightTComboBox的{​​{3}}表示:

  

...如果Style为csOwnerDrawVariable,则ItemHeight为默认高度   用于工程图清单项目,但此值可以由   OnMeasureItem事件处理程序。 ...

VCL的内部计算取决于FItemHeight。将ItemHeight设置为59,DropDownCount将被接受(已经接受,但默认值为16)。如果您在设计时设置该值,则该组合还会将其大小调整为运行时的大小。