TVITEM LPARAM 存储字符串 [英] TVITEM LPARAM to store string

查看:45
本文介绍了TVITEM LPARAM 存储字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个树视图列表文件放在上面.

I've got a treeview listing files that are dropped upon it.

当我创建一个新的树视图项目时,我想将文件的地址作为字符串存储在该项目中,并在稍后的时间点检索它以用于各种恶意目的.

When I make a new treeview item, I'd like to store the address of the file as a string in that item, and retrieve it for various nefarious purposes at a later point in time.

查看 TVITEM结构在微软文档中,显然 LPARAM 是存储值的地方:

Looking at the TVITEM structure in Microsoft docs, apparently LPARAM is the place to store a value:

lParam

Type: LPARAM

A value to associate with the item.

所以,我已经这样做了:

So, I have gone ahead and done that:

TVITEM tvi;
tvi.mask = TVIF_TEXT;
tvi.pszText = const_cast<char *> (str0.c_str());
tvi.cchTextMax = sizeof(tvi.pszText);
tvi.lParam = (LPARAM) foo;  // SETTING LPARAM HERE, foo IS A const char * 

TVINSERTSTRUCT tvis;
tvis.item = tvi;
tvis.hInsertAfter = 0;
tvis.hParent = hti0;

// Send message to update tree, and return tree item.
return TreeView_InsertItem(tvw_filelist_, &tvis);

然后,当我尝试检索我的价值时...

Then, when I try to retrieve my value...

HTREEITEM htiSel = TreeView_GetSelection(tvw_filelist_);

TVITEM tvItem;
tvItem.hItem = htiSel;

TreeView_GetItem(tvw_filelist_, &tvItem);
const char * info = (const char *) tvItem.lParam;
MessageBox(NULL, info, "Alert", MB_OK);

...我只是收到垃圾,表明我的指针超出范围或正在小睡或其他什么.该指针的大小始终为 4.

...I just get garbage, indicating my pointer went out of scope or is taking a nap or something. The size of that pointer is always 4.

这是做我想做的事情的正确方法吗?如果是这样,发生了什么?

Is this the right way to do what I'm trying to do? If so, what's going on?

推荐答案

当然,经过长时间的尝试,花点时间发布一个问题,答案会在几秒钟内出现.

Of course, take the time to post a question after a long time trying to figure it out, and the answer shows up in seconds.

原来 TVITEM 掩码需要包含 TVIF_PARAM,类似于这个问题.

Turns out the TVITEM mask needs to include TVIF_PARAM, similar to this question.

如果我把上面的代码改成:

If I change the above code to:

tvi.mask = TVIF_TEXT | TVIF_PARAM;

它按预期工作.

我仍然不确定这是否是 LPARAM 的推荐用途.

这篇关于TVITEM LPARAM 存储字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆