简体   繁体   中英

argument of type “int” is incompatible with parameter of type “HTREEITEM”

I'm getting error in line:

if (TreeView.GetItemState(item, TVIS_SELECTED) == TVIS_SELECTED)

It's not accepting item . New to using HTREEITEM , which is an opaque handle to a tree item in the default TreeView control on Window.

How can I get rid of the error? Only including part of the code with error:

int item, num;

for(item = 0; item < total; ++item)
{
    if (TreeView.GetItemState(item, TVIS_SELECTED) == TVIS_SELECTED)  //error here on item
    {
        num= item;
        break;
    }
}

The GetItemState() method expects an HTREEITEM handle to a specific tree node, but you are giving it a loop index instead. Tree nodes are not required to be sequential, and so indexing requires more work. The correct way to iterate a TreeView is to use methods like GetRootItem() , GetNextItem() , GetChildItem() , etc which all return HTREEITEM handles. For example:

HTREEITEM item = NULL;

HTREEITEM node = TreeView.GetRootItem();

for(int num = 0; num < total; ++num)
{
    if (TreeView.GetItemState(node, TVIS_SELECTED) == TVIS_SELECTED)
    {
        item = node;
        break;
    }

    node = TreeView.GetNextItem(node, TVGN_NEXT);
}

However, in your particular example, it would be easier to use the GetSelectedItem() method instead of a manual GetItemState() loop, eg:

HTREEITEM item = TreeView.GetSelectedItem();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM