但是 Checkbox 属性只支持 vsList 和 vsReport 模式...
那不正确。您正在查看 VCL文档,其中指出
当 ViewStyle 为 vsList 或 vsReport 时,将Checkboxes设置为 true 使复选框显示在列表项旁边。...
这是过时的信息。请参阅本机控件的文档:
版本 6.00 及更高版本除 ComCtl32.dll 版本 6 中引入的平铺视图模式外,复选框在所有列表视图模式下均可见且有效。...
实际上,如果您TListView在其中一种图标模式下在常规控件上尝试使用它,您会发现复选框没有问题。
然而,这不会帮助你。在这方面,您的问题格式不正确,它假设Checkboxes在列表和报告模式下使用虚拟列表视图控件可以正常工作。事实并非如此。
当您可以Checked在列表项上使用该属性时,复选框是很好的选择。在虚拟列表视图控件中,没有您可以检查的项目。我从LVM_SETITEMCOUNT消息中引用:
如果列表视图控件是在没有LVS_OWNERDATA样式的情况下创建的,则发送此消息会导致控件为指定数量的项目分配其内部数据结构。...
...
如果列表视图控件是使用LVS_OWNERDATA样式(虚拟列表视图)创建的,则发送此消息将设置控件包含的虚拟项目数。
所有控件都知道有这么多项目,没有每个项目的存储。VCL 反映 API 控件:当您请求一个项目并且您的控件已OwnerData设置时,将OnData在临时项目上调用事件处理程序以反映该项目的属性。
在虚拟列表视图中,您可以使用状态图像管理检查。从文档中引用:
... 您可以使用状态图像(例如选中和清除复选框)来指示应用程序定义的项目状态。状态图像显示在图标视图、小图标视图、列表视图和报告视图中。
您将在下面找到一个基本实现,它将项目状态信息保存在单独的数组中。要运行它,请创建一个空白的新表单,OnCreate为该表单创建一个处理程序并粘贴代码。
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.ImgList;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
FListView: TListView;
FListViewCheckStateArray: array of 0..1;
procedure ListViewData(Sender: TObject; Item: TListItem);
procedure ListViewMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
Bmp: TBitmap;
begin
FListView := TListView.Create(Self);
FListView.Parent := Self;
FListView.Align := alClient;
FListView.OwnerData := True;
FListView.ViewStyle := vsSmallIcon;
FListView.StateImages := TImageList.Create(Self);
Bmp := TBitmap.Create;
Bmp.PixelFormat := pf32bit;
Bmp.SetSize(16, 16);
DrawFrameControl(Bmp.Canvas.Handle, Rect(0, 0, 16, 16), DFC_BUTTON,
DFCS_BUTTONCHECK);
FListView.StateImages.Add(Bmp, nil);
DrawFrameControl(Bmp.Canvas.Handle, Rect(0, 0, 16, 16), DFC_BUTTON,
DFCS_BUTTONCHECK or DFCS_CHECKED);
FListView.StateImages.Add(Bmp, nil);
Bmp.Free;
FListView.Items.Count := 257;
SetLength(FListViewCheckStateArray, FListView.Items.Count);
FListView.OnData := ListViewData;
FListView.OnMouseDown := ListViewMouseDown;
end;
procedure TForm1.ListViewData(Sender: TObject; Item: TListItem);
begin
Item.Caption := Format('This is item %.2d', [Item.Index]);
Item.StateIndex := FListViewCheckStateArray[Item.Index];
end;
procedure TForm1.ListViewMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
Item: TListItem;
begin
if (Button = mbLeft) and
(htOnStateIcon in FListView.GetHitTestInfoAt(X, Y)) then begin
Item := FListView.GetItemAt(X, Y);
Assert(Assigned(Item));
FListViewCheckStateArray[Item.Index] :=
Ord(not Boolean(FListViewCheckStateArray[Item.Index]));
FListView.UpdateItems(Item.Index, Item.Index);
end;
end;
end.
Run Code Online (Sandbox Code Playgroud)
PS:绘制工件应该是另一个问题的主题。