Ttreeview is the tree list control provided in VCL, where each node of the tree is a Ttreenode class, and Ttreenode component properties and methods can refer to the help provided by Borland (although not as comprehensive as MSDN, but better than none). In practice we may need to disable a node (the effect of the interface response is: The node font is grayed out, nodes can not be selected, etc.). But VCL did not provide node->disable (), or node->enable=false; such a function, we had to do it ourselves. First we need to set a flag for each node to identify whether this node is available, there are many ways to identify, such as the text of the node, the absolute index of the node (ABSOLUTEINDEX), the Index (index) of the node (Indent) and so on, In this example, we use the data attribute of the node as the identifier (a void * type, which can actually store n many things). If you just use the data attribute in your application, think of another way to make a mark. :)
We write a custom function to enable/Disable a node:
//---------------------------------------------------------------------------
//函数功能:改变指定节点的启用/禁用状态
// bEnable:启用:true;禁用:false
// pNode:要改变状态的节点
void__fastcallCrnEnableTreeNode(boolbEnable,TTreeNode*pNode)
{
//设定规则,如果节点允许使用,Node的Data存放0(默认就是0),
//如果禁用,Data存放0xFFFF
pNode->Data=bEnable?NULL:(void*)0xFFFF;
//标志改变后重绘一下TreeView
// 本文转自 C++Builder 研究 - http://www.ccrun.com/article.asp?i=1015&d=r2tf61
pNode->TreeView->Invalidate();
}
Then consider how to achieve the effect of disabling the node, as we said earlier, we only need to achieve these two effects:
1. Node fonts appear dimmed
2. Node cannot be selected
The node font is grayed out through the Oncustomdrawitem event in the TreeView, in the design-time state, selecting the Treeview,events tab to double-click the Oncustomdrawitem event, adding the following code:
//---------------------------------------------------------------------------
void__fastcallTForm1::TreeView1CustomDrawItem(TCustomTreeView*Sender,
TTreeNode*Node,TCustomDrawStateState,bool&DefaultDraw)
{
//判断当前绘制节点是否被禁用
// 63 63 72 75 6E 2E 63 6F 6D
if(int(Node->Data)==0xFFFF)
{
//如果节点被禁用则用灰色字体显示,默认为黑色
Sender->Canvas->Font->Color=clGray;
}
}
Nodes cannot be checked, they can be handled through the changing event of the TreeView, at design-time state, select the Treeview,events tab to double-click the Onchanging event, and add the following code:
//---------------------------------------------------------------------------
void__fastcallTForm1::TreeView1Changing(TObject*Sender,TTreeNode*Node,
bool&AllowChange)
{
//欲选中这个节点时,判断如果当前节点被禁用则禁止改变原节点的选择状态
AllowChange=(int(Node->Data)!=0xFFFF);
}
Have the above realization, the effect basically comes out:
Test code:
//---------------------------------------------------------------------------
void__fastcallTForm1::Button1Click(TObject*Sender)
{
//禁用当前选中的节点
if(TreeView1->Selected)
CrnEnableTreeNode(true,TreeView1->Selected);
}
//---------------------------------------------------------------------------
void__fastcallTForm1::Button2Click(TObject*Sender)
{
//启用第二个节点
CrnEnableTreeNode(true,TreeView1->Items->Item[1]);
}
//---------------------------------------------------------------------------
To see better results, you can expand all nodes at test time:
Treeview1->fullexpand ();