你正在走這個錯誤的路。根本不要使用DragMode=dmAutomatic
,這並不意味着你如何使用它。相反,在OnMouseDown
事件中,當左按鈕關閉時設置一個標誌。在OnMouseMove
事件中,如果標誌已設置,請選擇鼠標下的當前項目。在OnMouseUp
事件中,清除標誌。例如:
var
Dragging: Boolean = False;
procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
If Button = mbLeft then
Dragging := True;
end;
procedure TForm1.ListBox1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
If Button = mbLeft then
Dragging := False;
end;
procedure TForm1.ListBox1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
Index: Integer;
begin
If not Dragging then Exit;
Index := ListBox1.ItemAtPos(Point(X, Y), True): Integer;
If Index <> -1 then
ListBox1.Selected[Index] := True;
end;