listView设置成report模式,然后subitem都是些小图标,我希望这些小图标能够点击响应事件,如何办? 就是说listView的subitem怎么响应点击事件?

解决方案 »

  1.   

    private
        AllowClick : Boolean;
    procedure TForm1.LVMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    var
      Item : TListItem;
      R : TRect;
    begin
      AllowClick := False;
      Item := LV.GetItemAt(X, Y);
      if Item <> nil then begin
        R := Item.DisplayRect(drIcon);
        AllowClick := (X>=R.Left) and (X<=R.Right) and (Y>=R.Top) and (Y<=R.Bottom);
        {本来想画一下image来模拟CLICK效果,结果发现了一个bug,当触发selectitem时,
         mousedown事件居然在mouseup之后(我的D6,没打过补丁。)
         with LV.Canvas do begin
          Pen.Color := clWhite;
          Rectangle(R);
          LV.SmallImages.Draw(LV.Canvas,r.Left +1,R.Top+1,Item.ImageIndex);
        end;}
      end;
    end;procedure TForm1.LVClick(Sender: TObject);
    begin
      if AllowClick then begin
        ShowMessage('icon clicked');
      end;
    end;
      

  2.   

    不行啊,楼上的。 Item := LV.GetItemAt(X, Y);如果点的是subitem,这个item始终是空。
      

  3.   

    在OnSelectItem事件里写就行了吧
      

  4.   

    你不是只想让图标响应click吗?
      

  5.   

    问题是我说的图标不是第一个图标。是后面的。例:我用listView的report模式实现如下样子:
      序号     按钮1(图片)     按钮2(图片)    按钮3(图片)我要能响应后面几个图片,而不是序号那里放的图片。
      

  6.   

    private
        { Private declarations }
        AllowClick : Boolean;
        FSubIndex : Integer;
        Item : TListItem;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}function PointInRect(const P: TPoint; const R: TRect): Boolean;
    begin
      Result := (P.X>=R.Left) and (P.X<=R.Right) and (P.Y>=R.Top) and (P.Y<=R.Bottom);
    end;procedure TForm1.LVMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    var
      i, iLeft : Integer;
      R : array of TRect;
    begin
      SetLength(R, LV.Columns.Count);
      AllowClick := False;
      Item := LV.GetItemAt(5, Y);    //Get item just from Y
      if Item = nil then Exit;
      //get all area that can clicked
      if Item.ImageIndex <> -1 then begin
        R[0] := Item.DisplayRect(drIcon);
      end;
      iLeft := LV.Columns[0].Width;
      for i :=1 to LV.Columns.Count -1 do begin
        if (item.SubItems.Count >= i) and (Item.SubItemImages[i-1] <> -1) then begin
          R[i] := Item.DisplayRect(drLabel);
          R[i].Left := iLeft;
          R[i].Right := R[i].Left+ LV.SmallImages.Width;
        end;
        iLeft := iLeft + LV.Columns[i].Width;
      end;
      //justfy whether cursor is in those areas can clicked
      for i := 0 to LV.Columns.Count -1 do if PointInRect(Point(X,Y),R[i]) then begin
        FSubIndex := i;
        AllowClick := True;
        Break;
      end;
    end;procedure TForm1.LVClick(Sender: TObject);
    begin
      if AllowClick then begin
        if FSubIndex = 0 then
          ShowMessage(Item.Caption + ' clicked')
        else
          ShowMessage(Item.SubItems[FSubIndex-1] + ' clicked');
      end;
    end;