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;
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;
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;
序号 按钮1(图片) 按钮2(图片) 按钮3(图片)我要能响应后面几个图片,而不是序号那里放的图片。
{ 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;