delphi - 在 Delphi 中创建 TCustomComboBox 后代

标签 delphi

我正在尝试在 Delphi 2007 中创建基于 TCustomComboBox 的自定义控件,但我陷入了第一个障碍。

我试图覆盖下拉列表的显示方式,主要是显示的文本,查看 stdctrls.pas 中 TCustomComboBox 的源代码,看起来我只需要覆盖 DrawItem 但它不起作用,因为我的重写方法中的代码永远不会执行。

我查看了几个开源组件的源代码,看看它们是如何做到的,但我仍然一头雾水。

这是我到目前为止所拥有的(诚然不多)

type
  TKeyValueComboBox = class(TCustomComboBox)
  private
    { Private declarations }
    //FColumns:Integer;
  protected
    { Protected declarations }
    procedure DrawItem(Index: Integer; Rect: TRect; State: TOwnerDrawState);override; 
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
end;

procedure TKeyValueComboBox.DrawItem(Index: Integer; Rect: TRect;
  State: TOwnerDrawState);
begin
  TControlCanvas(Canvas).UpdateTextFlags;
  if Assigned(OnDrawItem) then OnDrawItem(Self, Index, Rect, State)
  else
  begin
    Canvas.FillRect(Rect);
    Canvas.TextOut(Rect.Left + 2, Rect.Top, Items[Index]+'-HELLO');
  end;
end;

有谁知道我需要使用什么方法来获得我的火覆盖版本?或者我做错了什么?

如有任何帮助,我们将不胜感激。

最佳答案

还有一个属性需要设置,根据内存,它是 DrawingStyle := dsCustomDraw 将其放入构造函数或 Loaded 中。

关于delphi - 在 Delphi 中创建 TCustomComboBox 后代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/642165/

相关文章:

delphi - 如何使用 Delphi 制作 rocketdock/OSX 风格的启动栏

ios - Delphi XE6 firemonkey组件在运行时添加时对齐问题

delphi - TStringList和SaveToFile:字符串完成后,如何告诉我换行?

delphi - 从 2 层应用程序发送电子邮件通知的建议,客户端可能未连接到 Internet

oracle - 在Delphi中相当于Oracle NLS_UPPER

delphi - 我能否找出在第 3 方 VCL 组件中,当我关闭包含该组件的项目时,导致 Delphi IDE 停止响应的原因?

android - 德尔福xe10安卓服务gps

multithreading - Delphi多线程中如何避免CPU 100%?

delphi - 有没有使用 Delphi 的 MVVM 教程?

delphi - Delphi 7中TDateTime的内部格式?