建设带有CheckBox的TreeView
当前位置:以往代写 > C/C++ 教程 >建设带有CheckBox的TreeView
2019-06-13

建设带有CheckBox的TreeView

建设带有CheckBox的TreeView

void __fastcall TForm1::FormCreate(TObject *Sender)
{
     SetWindowLong(TreeView1->Handle,GWL_STYLE,
       GetWindowLong(TreeView1->Handle,GWL_STYLE) | TVS_CHECKBOXES);
}
//判定一个节点是否处于Checked状态
bool GetNodeChecked(TTreeNode *ANode)
{
    TVITEM tvItem;
    tvItem.mask = TVIF_HANDLE | TVIF_STATE;
    tvItem.hItem = ANode->ItemId;
    tvItem.stateMask = TVIS_STATEIMAGEMASK;
    TreeView_GetItem(ANode->TreeView->Handle, &tvItem);
    return ((BOOL)(tvItem.state >> 12) -1);
}
//配置一个节点为Checked状态可能Unchecked状态,由参数IsSetToChecked来确定
void SetNodeChecked(TTreeNode *ANode,bool IsSetToChecked)
{
    TVITEM tvItem;
    tvItem.mask = TVIF_HANDLE | TVIF_STATE;
    tvItem.hItem = ANode->ItemId;
    tvItem.stateMask = TVIS_STATEIMAGEMASK;
    tvItem.state = INDEXTOSTATEIMAGEMASK(IsSetToChecked?2:1);
    TreeView_SetItem(ANode->TreeView->Handle, &tvItem);
}
void SetChildChecked(TTreeNode *ANode,bool yn)
{   // 把它的子孙都置成一样
    int count=ANode->Count;
    SetNodeChecked(ANode,yn);
    if (count!=0)
      for (int i=0;i<count;i++)  // 递归
        SetChildChecked(ANode->Item[i],yn);
}
//  TTreeNode *ClickNode;   // 生存点中的节点
//  bool   NodeChecked;  // 生存点中的节点checked
void __fastcall TForm1::TreeView1MouseMove(TObject *Sender,
    TShiftState Shift, int X, int Y)
{
    ClickNode=TreeView1->GetNodeAt(X,Y);
    if (ClickNode!=NULL)  // 生存原节点的Checked值
      NodeChecked=GetNodeChecked(ClickNode);
}
void __fastcall TForm1::TreeView1MouseDown(TObject *Sender,
    TMouseButton Button, TShiftState Shift, int X, int Y)
{
    if (ClickNode!=NULL)
      if (GetNodeChecked(ClickNode)!=NodeChecked)
       { // 把它的子孙都置成一样
         NodeChecked=!NodeChecked; // 更新它
         SetChildChecked(ClickNode,NodeChecked);
       }  
}

    关键字:

在线提交作业