procedure TForm1.ListBox1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var i: integer; begin i := ListBox1.ItemAtPos(Point(x,y),True); if i>=0 then ListBox1.ItemIndex := i; if Button = mbRight then PopupMenu1.Popup(Mouse.CursorPos.X, Mouse.CursorPos.Y); end;
procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var aPoint: TPoint; begin if Button = mbRight then begin aPoint.x := x; aPoint.y := y; ListBox1.ItemIndex := ListBox1.ItemAtPos(aPoint,true); end; end;
onMouseDown: with TListBox(Sender) do begin ItemIndex:=ItemAtPos(Point(X,Y),True); if (ItemIndex<>-1) and (Button =mbRight) then PopupMenu:=PopupMenu1 else PopupMenu:=nil; end;
后面的菜单事件直接使用此变量
procedure TForm1.ListBox1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
i: integer;
begin
i := ListBox1.ItemAtPos(Point(x,y),True);
if i>=0 then
ListBox1.ItemIndex := i; if Button = mbRight then
PopupMenu1.Popup(Mouse.CursorPos.X, Mouse.CursorPos.Y);
end;
Shift: TShiftState; X, Y: Integer);
var
aPoint: TPoint;
begin
if Button = mbRight then
begin
aPoint.x := x;
aPoint.y := y;
ListBox1.ItemIndex := ListBox1.ItemAtPos(aPoint,true);
end;
end;
begin
ItemIndex:=ItemAtPos(Point(X,Y),True);
if (ItemIndex<>-1) and (Button =mbRight) then
PopupMenu:=PopupMenu1
else
PopupMenu:=nil;
end;