利用TreeView.StateImages,往ImageList中加入两幅图片,
一张表示Checked = True, 假设Index = 0
另一表示Checked = False, 假设Index = 1
然后:
procedure TForm1.TreeView1MouseUp(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
HitTests: THitTests;
begin
HitTests := TreeView1.GetHitTestInfoAt(X, Y);
if htOnStateIcon in HitTests then
begin
if TreeView1.Selected.StateIndex = 0 then
TreeView1.Selected.StateIndex = 1;
if TreeView1.Selected.StateIndex = 1 then
TreeView1.Selected.StateIndex = 0;
if Assigned(...) then ... // 触发自定义事件
end;
end;
当然如果TreeView.Images没有用上,以上StateImages可用Images代替,
StateIndex --> ImageIndex
htOnStateIcon --> htOnIcon