CJC Delphi 技巧馆's Archiver

cjc Posted at 2018-9-20 10:50

ListView 中修改 CheckBoxes, 使空行不显示Checkbox (cjc,2017.9.14)

ListView 中修改 CheckBoxes, 使空行不显示Checkbox (cjc,2017.9.14)

ListView 空行不显示Checkbox:

procedure TForm1.ListView1AdvancedCustomDrawItem(Sender: TCustomListView;
   Item: TListItem; State: TCustomDrawState; Stage: TCustomDrawStage;
   var DefaultDraw: Boolean);
   DefaultDraw := Item.Caption <> '';

ListView 中修改 CheckBoxes:
ListView With "Nice" CheckBoxes
One way to have nice check boxes in a list view control is to use your own custom graphics (16x16 px bitmap image) and draw it over the original check box.

The event to look for is named OnAdvancedCustomDrawItem - it fires even if OwnerDraw is set to false (default).

By handling the OnAdvancedCustomDrawItem event you can customize the drawing of individual items on the list view's canvas (using the Canvas property). OnAdvancedCustomDrawItem occurs at various stages in the rendering of a list item.

To have a nice check box displayed next to the item you can use 2 custom images - one for the checked state and one for the non-checked state.

Have a TImageList with two "check box" images for checked = true and checked = false states.

//listview OnAdvancedCustomDrawItem
procedure TListViewForm.listViewAdvancedCustomDrawItem(
   Sender: TCustomListView;
   Item: TListItem;
   State: TCustomDrawState;
   Stage: TCustomDrawStage;
   var DefaultDraw: Boolean) ;
   r : TRect;
   r := item.DisplayRect(drIcon) ;

   if stage = cdPostPaint then
   if item.Checked then
     stateImages.Draw(Sender.Canvas, r.Left - 16, r.Top, 1)
     stateImages.Draw(Sender.Canvas, r.Left - 16, r.Top, 2) ;

Unfortunately, the TListView does not raise an event when the "Checked" state changes for a TListItem (an item in the list view). Here's how to implement the On Item Checked Event for the TListView Control

1. make sure the ViewStyle property of the list view is set to vsReport. Of course, Checkboxes property must be set to true, also.
2. make sure yu read the Help on AdvancedCustomDrawItem event - to make familiar with Stages, States, DisplayRect etc.

procedure TListViewForm.FormCreate(Sender: TObject) ;
   listView.Checkboxes := true;
   listView.ViewStyle := vsReport;

That's it. Tricky but powerful.

Page: [1]
CJC好玩背单词最新版本 - 是目前最好的背单词软件,游戏好玩,功能强大,词库众多,还有真人语音,免费下载,免费试用 !

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.