我找到了 GetCheckedItems() 函数,它可以知道项目是如何检查的,包括树控件中的子项目
I found GetCheckedItems() function that is could know how items checked included child items in Tree Control
我知道什么是移位运算符和位运算符,但这段代码是什么意思?不太懂
请简单解释一下。
UINT state = (tree.GetItemState(item, TVIS_STATEIMAGEMASK) >> 12) & 15;
原代码如下:
void GetCheckedItems(const CTreeCtrl& tree, CArray<HTREEITEM>
*checkedItems, HTREEITEMstartItem=NULL)
{
if (startItem == NULL)
startItem = tree.GetRootItem();
for (HTREEITEM item = startItem; item != NULL; item =
tree.GetNextItem(item, TVGN_NEXT))
{
// figure out if this item is checked or not
UINT state = (tree.GetItemState(item, TVIS_STATEIMAGEMASK) >> 12) &
15; // i Wonder this ( shift and bit operator )
if (state == 2)
checkedItems->Add(item);
// deal with children if present
HTREEITEM child = tree.GetNextItem(item, TVGN_CHILD);
if (child != NULL)
GetCheckedItems(tree, checkedItems, child);
}
}
简而言之:(state >> 12) & 15
将位 12 到 15 向下移动到位 0 到位 3,并清除从位 4 开始向上的所有内容。
在树视图控件的上下文中,这对于应用程序定义的图像状态是有意义的。如 Tree-View Item States Overview 中所述:
A state image is displayed next to an item's icon to indicate an application-defined state. State images are contained in a state image list that is specified by sending a TVM_SETIMAGELIST message. To set an item's state image, include the TVIS_STATEIMAGEMASK value in the statemask
member of the TVITEM structure. Bits 12 through 15 of the structure's state member specify the index in the state image list of the image to be drawn.
树视图控件项的检查状态存储在项状态中。按照惯例,索引0处的图像请求无状态图像,索引1是未检查状态,索引2是检查状态。
屏蔽掉与item的检查状态相关的状态图像索引,并将其与值2进行比较,从而确定树视图项目的状态是否被检查。
我知道什么是移位运算符和位运算符,但这段代码是什么意思?不太懂
请简单解释一下。
UINT state = (tree.GetItemState(item, TVIS_STATEIMAGEMASK) >> 12) & 15;
原代码如下:
void GetCheckedItems(const CTreeCtrl& tree, CArray<HTREEITEM>
*checkedItems, HTREEITEMstartItem=NULL)
{
if (startItem == NULL)
startItem = tree.GetRootItem();
for (HTREEITEM item = startItem; item != NULL; item =
tree.GetNextItem(item, TVGN_NEXT))
{
// figure out if this item is checked or not
UINT state = (tree.GetItemState(item, TVIS_STATEIMAGEMASK) >> 12) &
15; // i Wonder this ( shift and bit operator )
if (state == 2)
checkedItems->Add(item);
// deal with children if present
HTREEITEM child = tree.GetNextItem(item, TVGN_CHILD);
if (child != NULL)
GetCheckedItems(tree, checkedItems, child);
}
}
简而言之:(state >> 12) & 15
将位 12 到 15 向下移动到位 0 到位 3,并清除从位 4 开始向上的所有内容。
在树视图控件的上下文中,这对于应用程序定义的图像状态是有意义的。如 Tree-View Item States Overview 中所述:
A state image is displayed next to an item's icon to indicate an application-defined state. State images are contained in a state image list that is specified by sending a TVM_SETIMAGELIST message. To set an item's state image, include the TVIS_STATEIMAGEMASK value in the
statemask
member of the TVITEM structure. Bits 12 through 15 of the structure's state member specify the index in the state image list of the image to be drawn.
树视图控件项的检查状态存储在项状态中。按照惯例,索引0处的图像请求无状态图像,索引1是未检查状态,索引2是检查状态。
屏蔽掉与item的检查状态相关的状态图像索引,并将其与值2进行比较,从而确定树视图项目的状态是否被检查。