unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ImgList;type
  TForm1 = class(TForm)
    friendListBox: TListBox;
    ImageListfriend: TImageList;
    procedure FriendListBoxFormCreate(Sender: TObject);
    procedure FriendListBoxDrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    procedure FriendListBoxMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
  private
    PrevRect: TRect; //上次鼠标画过的区域 
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FriendListBoxFormCreate(Sender: TObject);
begin  friendListBox.Style := lbOwnerDrawVariable; //自己画每一项,项高度可调
  friendListBox.ItemHeight := 40;
  friendListBox.Clear;
  //名称+#9+备注
  friendListBox.Items.Add('yujinfree'#9 + '昨日足迹');
  friendListBox.Items.Add('blazingfire'#9 + '...该充电了...');
  friendListBox.Items.Add('yujinfree'#9 + '昨日足迹');
  friendListBox.Items.Add('blazingfire'#9 + '...该充电了...');
end;procedure TForm1.friendListBoxDrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  ImgRect, MemoRect: TRect;
  Cnvs: TCanvas;
  B: TBitmap;
  S, Memo: string;
  iPos: Integer;
  MousePt: TPoint;
begin
  Cnvs := friendListBox.Canvas;
  ImgRect := Rect;
  ImgRect.Right := ImgRect.Left + (ImgRect.Bottom - ImgRect.Top); //画图区域
  Inc(Rect.Left, 3);//
  DrawText(Cnvs.Handle, PChar(S), -1,
    Rect, DT_SingleLine or DT_VCENTER); //  Cnvs.Font.Color := clGrayText;
  Inc(MemoRect.Left, 1);//+++
  DrawText(Cnvs.Handle, PChar(Memo), -1,
    MemoRect, DT_SingleLine or DT_VCENTER); //这个函数里边添加了您给我的代码
  MousePt := friendListBox.ScreenToClient(Mouse.CursorPos); ///&&鼠标位置
  if odSelected in State then
  begin
    Cnvs.Brush.Color := TColor($00FFF4DD);
    Cnvs.Pen.Color := TColor($00F3D6B2);
  end
  else if PtInRect(Rect, MousePt) then ///&&鼠标选中项目
  begin
    Cnvs.Brush.Color := TColor($00FFF4DD);
    Cnvs.Pen.Color := TColor($00F3D6B2);
  end
  else
  begin
    Cnvs.Brush.Color := friendListBox.Color;
    Cnvs.Pen.Color := friendListBox.Color;
  end;
  Cnvs.Brush.Style := bsSolid;
  Cnvs.Rectangle(Rect);  Cnvs.Brush.Style := bsClear;
  if ImageListfriend.Count > 0 then
  begin
    B := TBitmap.Create;
    try
      ImageListfriend.GetBitmap(0, B);
      B.PixelFormat := pf24bit;
      B.Transparent := True;
      InflateRect(ImgRect, -2, -2);
      Cnvs.StretchDraw(ImgRect, B);
    finally
      B.Free;
    end;
  end;  Rect.Left := ImgRect.Right;
  MemoRect := Rect;
  MemoRect.Top := (Rect.Top + Rect.Bottom) div 2;
  Rect.Bottom := MemoRect.Top;
  S := friendListBox.Items[Index];  iPos := Pos(#9, S);
  Memo := Copy(S, iPos + 1, Length(S)); //提取备注
  S := Copy(S, 1, iPos - 1); //提取名称
  Cnvs.Font.Assign(friendListBox.Font);
  DrawText(Cnvs.Handle, PChar(S), -1,
    Rect, DT_SingleLine or DT_VCENTER); //垂直、水平居中 画名字  Cnvs.Font.Color := clGrayText;
  DrawText(Cnvs.Handle, PChar(Memo), -1,
    MemoRect, DT_SingleLine or DT_VCENTER); //垂直、水平居中 画名字
end;procedure TForm1.friendListBoxMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  ARect: TRect;
  i: Integer;
begin  i := friendListBox.ItemAtPos(Point(X, Y), True);
  if i > -1 then
  begin
    friendListBox.Perform(LB_GETITEMRECT, i, Longint(@ARect));
    if not(
      (PrevRect.Left = ARect.Left) and (PrevRect.Right = ARect.Right) and
      (PrevRect.Top = ARect.Top) and (PrevRect.Bottom = ARect.Bottom)) then
    begin
      InvalidateRect(friendListBox.Handle, @ARect, True);
      InvalidateRect(friendListBox.Handle, @PrevRect, True);
      PrevRect := ARect;
    end;
  end
  else
  begin
    InvalidateRect(friendListBox.Handle, @PrevRect, True);
    PrevRect := Rect(0, 0, 0, 0);
  end;
end;end.关于间距的问题,您给我发了一个代码加入,说是加入到两个函数前面。哪两个函数?我在friendListBoxDrawItem中加入了,但是没有效果,希望您能多帮忙

解决方案 »

  1.   

    procedure TForm1.friendListBoxDrawItem(Control: TWinControl; Index: Integer; 
      Rect: TRect; State: TOwnerDrawState); 
    var 
      ImgRect, MemoRect: TRect; 
      Cnvs: TCanvas; 
      B: TBitmap; 
      S, Memo: string; 
      iPos: Integer; 
      MousePt: TPoint; 
    begin 
      Cnvs := friendListBox.Canvas; 
      ImgRect := Rect; 
      ImgRect.Right := ImgRect.Left + (ImgRect.Bottom - ImgRect.Top); //画图区域 
      Inc(Rect.Left, 3);// 
      DrawText(Cnvs.Handle, PChar(S), -1, 
        Rect, DT_SingleLine or DT_VCENTER); //   Cnvs.Font.Color := clGrayText; 
      Inc(MemoRect.Left, 1);//+++ 
      DrawText(Cnvs.Handle, PChar(Memo), -1, 
        MemoRect, DT_SingleLine or DT_VCENTER); //这个函数里边添加了您给我的代码 
      MousePt := friendListBox.ScreenToClient(Mouse.CursorPos); ///&&鼠标位置 
      if odSelected in State then 
      begin 
        Cnvs.Brush.Color := TColor($00FFF4DD); 
        Cnvs.Pen.Color := TColor($00F3D6B2); 
      end 
      else if PtInRect(Rect, MousePt) then ///&&鼠标选中项目 
      begin 
        Cnvs.Brush.Color := TColor($00FFF4DD); 
        Cnvs.Pen.Color := TColor($00F3D6B2); 
      end 
      else 
      begin 
        Cnvs.Brush.Color := friendListBox.Color; 
        Cnvs.Pen.Color := friendListBox.Color; 
      end; 
      Cnvs.Brush.Style := bsSolid; 
      Cnvs.Rectangle(Rect);   Cnvs.Brush.Style := bsClear; 
      if ImageListfriend.Count > 0 then 
      begin 
        B := TBitmap.Create; 
        try 
          ImageListfriend.GetBitmap(0, B); 
          B.PixelFormat := pf24bit; 
          B.Transparent := True; 
          InflateRect(ImgRect, -2, -2); 
          Cnvs.StretchDraw(ImgRect, B); 
        finally 
          B.Free; 
        end; 
      end;   Rect.Left := ImgRect.Right; 
      MemoRect := Rect; 
      MemoRect.Top := (Rect.Top + Rect.Bottom) div 2; 
      Rect.Bottom := MemoRect.Top; 
      S := friendListBox.Items[Index];   iPos := Pos(#9, S); 
      Memo := Copy(S, iPos + 1, Length(S)); //提取备注 
      S := Copy(S, 1, iPos - 1); //提取名称 
      Cnvs.Font.Assign(friendListBox.Font); 
      Inc(Rect.Left, 2);////+++在画S之前加,具体多少根据你自己的需要
      DrawText(Cnvs.Handle, PChar(S), -1, 
        Rect, DT_SingleLine or DT_VCENTER); //垂直、水平居中 画名字   Cnvs.Font.Color := clGrayText; 
      Inc(MemoRect.Left, 2);////+++在画Memo之前加,具体多少根据你自己的需要
      DrawText(Cnvs.Handle, PChar(Memo), -1, 
        MemoRect, DT_SingleLine or DT_VCENTER); //垂直、水平居中 画名字 
    end;