0

TComboBox在西雅图德尔福有一个业主自画的,它忽略了该DropDownCount物业的任何设置。单个项目的高度为 59 像素,宽度为 311 像素。我在字符串列表中有 5 个条目Items。将值设置为低于 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.
4

1 回答 1

0

即使国家的文件ItemHeightTComboBox

... 如果 Style 是 csOwnerDrawVariable,ItemHeight 是绘图列表项的默认高度,但该值可以被 OnMeasureItem 事件处理程序覆盖。...

VCL 的内部计算依赖于FItemHeight. 设置ItemHeight为 59DropDownCount并将被兑现(它已经兑现,但默认为 16)。如果您在设计时设置该值,则组合也会将其大小调整为运行时的大小。

于 2018-10-09T19:57:51.750 回答