Create a TreeView with a checkbox

Source: Internet
Author: User
Tags bool count

void __fastcall tform1::formcreate (tobject *sender)
{
SetWindowLong (Treeview1->handle,gwl_style,
GetWindowLong (Treeview1->handle,gwl_style) | Tvs_checkboxes);
}
Determine if a node is in a checked state
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);
}
Sets a node to be a checked state or unchecked state, determined by the parameter 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)
{//Put all its descendants in the same place.
int count=anode->count;
Setnodechecked (Anode,yn);
if (count!=0)
for (int i=0;i<count;i++)//recursive
Setchildchecked (Anode->item[i],yn);
}
Ttreenode *clicknode; Nodes in the Save point
BOOL nodechecked; Node checked in the Save point
void __fastcall Tform1::treeview1mousemove (TObject *sender,
Tshiftstate Shift, int X, int Y)
{
Clicknode=treeview1->getnodeat (X,y);
if (clicknode!=null)//Save the checked value of the original node
Nodechecked=getnodechecked (Clicknode);
}
void __fastcall Tform1::treeview1mousedown (TObject *sender,
Tmousebutton Button, tshiftstate Shift, int X, int Y)
{
if (clicknode!=null)
if (getnodechecked (clicknode)!=nodechecked)
{//Put all its descendants in the same place.
nodechecked=! nodechecked; Update it
Setchildchecked (clicknode,nodechecked);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.