1.先定义结构:
type
PItemCtrl = ^TItemCtrl;
TItemCtrl = record
viewCheckBox: TCheckBox;
markCheckBox: TCheckBox;
guidCheckBox: TCheckBox;
end;
2.在listview的CustomDrawSubItem事件中写如下代码;
var
Rect: TRect;
P: PItemCtrl;
begin
if SubItem in [1,2,3] then
begin
DefaultDraw:= False; // 不显示默认的文本.
Rect:= Item.DisplayRect(drBounds); // 获取Item显示的区域.
if Item.Data = nil then // 如果为空则创建CheckBox及Button.
begin
new(P); // 创建一个指针用于存储CheckBox及Button.
{ 创建并显示CheckBox }
P.viewCheckBox:= TCheckBox.Create(ListView1);
P.viewCheckBox.Parent:= ListView1;
P.viewCheckBox.Caption:= '';
P.viewCheckBox.Width:= 20;
P.viewCheckBox.Height:= 20;
P.viewCheckBox.Left:= Rect.Right - ListView1.Columns[2].Width
- ListView1.Columns[3].Width
- ((ListView1.Columns[1].Width + P.viewCheckBox.Width) div 2);
P.viewCheckBox.Top:= Rect.Top;
P.viewCheckBox.Visible:= True;
// showmessage(inttostr(SubItem));
{ SubItems[2 -1].Caption为0和1,直接转换为Boolean型并给CheckBox赋值. }
P.CheckBox.Checked:= StrToBool(Item.SubItems[SubItem-1]);
//创建并显示Button
{P.Button:= TRadioButton.Create(ListView1);
P.Button.Parent:= ListView1;
P.Button.Caption:= '...';
P.Button.Width:= 20;
P.Button.Height:= 20;
P.Button.Left:= Rect.Right - ((ListView1.Columns[3].Width
+ P.Button.Width) div 2);
P.Button.Top:= Rect.Top;
P.Button.Visible:= True;}
P.markCheckBox:= TCheckBox.Create(ListView1);
P.markCheckBox.Parent:= ListView1;
P.markCheckBox.Caption:= '';
P.markCheckBox.Width:= 20;
P.markCheckBox.Height:= 20;
P.markCheckBox.Left:= Rect.Right - ListView1.Columns[3].Width
- ((ListView1.Columns[2].Width + P.markCheckBox.Width) div 2);
P.markCheckBox.Top:= Rect.Top;
P.markCheckBox.Visible:= True;
////
P.guidCheckBox:= TCheckBox.Create(ListView1);
P.guidCheckBox.Parent:= ListView1;
P.guidCheckBox.Caption:= '';
P.guidCheckBox.Width:= 20;
P.guidCheckBox.Height:= 20;
P.guidCheckBox.Left:= Rect.Right
- ((ListView1.Columns[3].Width + P.guidCheckBox.Width) div 2);
P.guidCheckBox.Top:= Rect.Top;
P.guidCheckBox.Visible:= True;
Item.Data:= P; // 将CheckBox及Button的结构指针保存于Item.Data属性.
end;
end;
end;