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;