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

在Delphi中创建TCustomComboBox子体

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

    我试图在Delphi 2007中基于TCustomComboBox创建一个自定义控件,但我遇到了第一个障碍。

    我试图重写下拉菜单的显示方式,主要是显示的文本,查看stdctrls.p中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;
    

    任何帮助都将不胜感激。

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

    还有一个必须设置的属性,从内存中设置为DrawingStyle:=dsCustomDraw

        2
  •  1
  •   Daniel Rikowski    17 年前

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