[英]Need help understanding LVITEM state and stateMask
I'm confused. 我糊涂了。 The LVITEM structure states: LVITEM结构指出:
state 州
Type: UINT 类型: UINT
Indicates the item's state, state image, and overlay image. 指示项目的状态,状态图像和覆盖图像。 The stateMask member indicates the valid bits of this member. stateMask成员指示此成员的有效位。
Bits 0 through 7 of this member contain the item state flags. 该成员的位0到7包含项目状态标志。 This can be one or more of the item state values. 这可以是一个或多个项目状态值。
So my question is, what are bits 0 through 7 for? 所以我的问题是,位0到7是做什么用的? They appear not to indicate what is used by the other bits, otherwise the stateMask wouldn't be needed. 它们似乎没有指示其他位使用的内容,否则将不需要stateMask 。
MSDN tells you exactly what the bits in state
are: MSDN告诉您确切的state
是:
Bits 0 through 7 of this member contain the item state flags. 该成员的位0到7包含项目状态标志。 This can be one or more of the item state values . 这可以是一个或多个项目状态值 。
Bits 8 through 11 of this member specify the one-based overlay image index. 该成员的第8位到第11位指定基于一个的叠加图像索引。 ... To isolate these bits, use the LVIS_OVERLAYMASK mask. ...要隔离这些位,请使用LVIS_OVERLAYMASK掩码。
Bits 12 through 15 of this member specify the state image index. 该成员的第12到15位指定状态图像索引。 To isolate these bits, use the LVIS_STATEIMAGEMASK mask. 要隔离这些位,请使用LVIS_STATEIMAGEMASK掩码。
It does not make sense to set the bottom bits to LVIS_*MASK
, only the other LVIS_*
states. 将最低位设置为LVIS_*MASK
,仅设置其他LVIS_*
状态没有意义。 stateMask
specifies which bits in state
are required/valid when you query or set the state. stateMask
指定查询或设置状态时state
中的哪些位是必需/有效的。
The bit layout of state
and stateMask
is the same and if someone hands you a LVITEM
you would calculate the valid bits as valid = lvi.state & lvi.stateMask
. state
和stateMask
的位布局是相同的,如果有人将您LVITEM
您将计算出有效位为valid = lvi.state & lvi.stateMask
。 If the state bits you care about are not set in stateMask
you would have to query the listview for those bits. 如果您不在乎的状态位未在stateMask
设置, stateMask
必须在列表视图中查询这些位。
In the source code for the listview the query code might look something like this: 在listview的源代码中,查询代码可能看起来像这样:
void ListView::GetItemState(LVITEM&lvi, int idx)
{
lvi.state = 0;
if ((lvi.stateMask & LVIS_CUT) && isItemInCutState(idx, lvi)) lvi.state |= LVIS_CUT;
if ((lvi.stateMask & LVIS_...) && ...
}
There are two bits of information you want to communicate: The final value of each flag, and the set of flags you want to adjust. 您想传达两点信息:每个标志的最终值以及要调整的标志集。 Those are represented by the state
and stateMask
members, respectively. 这些分别由state
和stateMask
成员表示。
The operation performed is: 执行的操作是:
auto flags = prev_flags & ~( state | stateMask ); // reset flags
flags = flags | ( state & stateMask ); // set flags
An example: Assuming prev_flags
is 101
and you wish to reset flag 0, set flag 1, and keep flag 2 unchanged, you'd pass 010
as the state
and 011
as the stateMask
. 例如:假设prev_flags
为101
并且您希望重置标志0,设置标志1,并使标志2保持不变,则将010
作为state
传递,将011
作为stateMask
。 Note, that the stateMask
denotes 0
for flag 2, to retain its current value. 注意, stateMask
表示标志2为0
,以保留其当前值。
state & stateMask
evaluates to 010
. state & stateMask
计算结果为010
。
~( state | stateMask )
evaluates to 101
. ~( state | stateMask )
值为101
。
flags = prev_flags & ~( state & stateMask )
evaluates to 101 &= 100
, ie 100
flags = prev_flags & ~( state & stateMask )
值为101 &= 100
,即100
flags | ( state & stateMask )
flags | ( state & stateMask )
evaluates to 100 | 010
flags | ( state & stateMask )
值为100 | 010
100 | 010
, ie 110
. 100 | 010
,即110
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.