代码之家  ›  专栏  ›  技术社区  ›  Re0sless

在Delphi中创建tcustomcombobox子代

  •  2
  • Re0sless  · 技术社区  · 16 年前

    我试图在Delphi2007中创建一个基于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;
    

    有人知道我需要使用什么方法来获取我重写的fire版本吗?或者我做错了什么?

    任何帮助都将不胜感激。

    2 回复  |  直到 16 年前
        1
  •  5
  •   Henk Holterman    16 年前

    还有一个必须设置的属性,从内存来看,它的drawingStyle:=dsCustomDraw 将其放入构造函数或加载。

        2
  •  1
  •   Daniel Rikowski    16 年前

    是否启用了所有者绘图?默认情况下,它被停用。尝试使用标准组合框进行自定义绘图,然后创建自定义控件,应用所有必要的设置。