-.自己修改组件,可以吗?如果可以,请给出示例代码
2.其他方法

解决方案 »

  1.   

    unit Unit2;interfaceuses
      Windows, SysUtils, Classes, Controls, ExtCtrls, Graphics, Messages, StdCtrls;type
      TMyListBox = class(TListBox)
      private
        procedure CNDrawItem(var Message: TWMDrawItem); message CN_DRAWITEM;
      protected
        procedure DrawItem(Index: Integer; Rect: TRect;
          State: TOwnerDrawState); override;
      public
        constructor Create(AOwner: TComponent); override;
      end;procedure Register;implementationprocedure Register;
    begin
      RegisterComponents('WGYKING', [TMyListBox]);
    end;{ TMyListBox }procedure TMyListBox.CNDrawItem(var Message: TWMDrawItem);
    var
      State: TOwnerDrawState;
    begin
      with Message.DrawItemStruct^ do
      begin
        State := TOwnerDrawState(LongRec(itemState).Lo);
        Canvas.Handle := hDC;
        Canvas.Font := Font;
        Canvas.Brush := Brush;
        if (Integer(itemID) >= 0) and (odSelected in State) then
        begin
          Canvas.Brush.Color := clHighlight;
          Canvas.Font.Color := clHighlightText
        end;
        if Integer(itemID) >= 0 then
          DrawItem(itemID, rcItem, State) else
          Canvas.FillRect(rcItem);
        //if odFocused in State then DrawFocusRect(hDC, rcItem);
        Canvas.Handle := 0;
      end;
    end;constructor TMyListBox.Create(AOwner: TComponent);
    begin
      inherited;
      Style := lbOwnerDrawFixed;
    end;procedure TMyListBox.DrawItem(Index: Integer; Rect: TRect;
      State: TOwnerDrawState);
    var
      Flags: Longint;
      Data: String;
    begin
      inherited;
      if Assigned(OnDrawItem) then OnDrawItem(Self, Index, Rect, State) else
      begin
        Canvas.FillRect(Rect);
        if Index < Count then
        begin
          Flags := DrawTextBiDiModeFlags(DT_SINGLELINE or DT_VCENTER or DT_NOPREFIX);
          if not UseRightToLeftAlignment then
            Inc(Rect.Left, 2)
          else
            Dec(Rect.Right, 2);
          Data := '';
          if (Style in [lbVirtual, lbVirtualOwnerDraw]) then
            Data := DoGetData(Index)
          else
            Data := Items[Index];
          DrawText(Canvas.Handle, PChar(Data), Length(Data), Rect, Flags);
        end;
      end;
    end;end.
      

  2.   

    测试:unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Menus, Grids, ExtCtrls, Unit2;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure FormCreate(Sender: TObject);
      private
        ListBox: TMyListBox;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    begin
      ListBox := TMyListBox.Create(Self);
      ListBox.Parent := Self;
      ListBox.Items.Add('AAAAAAAAAAA');
      ListBox.Items.Add('BBBBBBBBBBB');
      ListBox.Items.Add('CCCCCCCCCCC');
      ListBox.Items.Add('DDDDDDDDDDD');
    end;end.
      

  3.   

    你可以象上面那样使用或将它安装在面板上使用时注意:
    Style不能为lbStandard型
    可以为其他类型没有其他限制了^_^