D7中原版的TreeView就很好用,但是却没有一个属性可以直接设置节点的CheckBox,经过摸索,找到一个较好的解决方案。
const TVS_CHECKBOXES = $00000100; procedure TForm1.SetComCtrlStyle(WinCtrl: TWinControl; Value: Integer; UseStyle: Boolean); var Style: Integer; begin if WinCtrl.HandleAllocated then begin Style := GetWindowLong(WinCtrl.Handle, GWL_STYLE); if not UseStyle then Style := Style and not Value else Style := Style or Value; SetWindowLong(WinCtrl.Handle, GWL_STYLE, Style); end; end;
const TVS_CHECKBOXES = $00000100; procedure TForm1.SetComCtrlStyle(WinCtrl: TWinControl; Value: Integer; UseStyle: Boolean); var Style: Integer; begin if WinCtrl.HandleAllocated then begin Style := GetWindowLong(WinCtrl.Handle, GWL_STYLE); if not UseStyle then Style := Style and not Value else Style := Style or Value; SetWindowLong(WinCtrl.Handle, GWL_STYLE, Style); end; end;
然后在Form.Create事件中调用即可:
另外,也可以把上述语句直接简化成一句,只不过对于不熟悉API的人来说,可读性是差了一些:
.Handle, GWL_STYLE, GetWindowLong(TreeView1.Handle, GWL_STYLE) or $00000100);
SetWindowLong(TreeView1.Handle, GWL_STYLE, GetWindowLong(TreeView1.Handle, GWL_STYLE) or $00000100); SetWindowLong(TreeView1
经过这样处理后的TreeView就带有了CheckBox的效果,可是大家可能会发现,CheckBox的下边框线不见了。这是由于默认的Node行距太小,可以对此进行修改。
(须要引用CommCtrl单元)
.Handle, 20);
TreeView_SetItemHeight(TreeView1