树控制中的Allocsysstring [英] Allocsysstring in tree control

查看:58
本文介绍了树控制中的Allocsysstring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





i有关于树控件中插入项的查询,如下所述:





m_cTreeCtrl.InsertItem(TVIF_TEXT | TVIF_IMAGE |

TVIF_SELECTEDIMAGE

| TVIF_PARAM,strTemp,0,0,0,0,(LPARAM)( LPCTSTR)(LPCTSTR)strParamval,m_hMatNode,TVI_LAST);



插入strparamval



时iam检索数据我无法获得如下所述的数据:



TVITEMEX项目;

item.mask = TVIF_PARAM;

item.hItem = hChildItem;

TreeView_GetItem(m_cTreeCtrl,& item);

CString strParam =(LPCTSTR)item.lParam;





这是一个for循环所以很多次调用该函数。当我保持下面的代码时,它的工作原理如下:



m_cTreeCtrl.InsertItem(TVIF_TEXT | TVIF_IMAGE |

TVIF_SELECTEDIMAGE | TVIF_PARAM,strTemp,0 ,0,0,0,

(LPARAM)strParamval.AllocSysString(),

m_hMatNode,TVI_LAST);





但是这个逻辑是循环的,当我分配和sysfreestring上次给出错误的值时。



我的怀疑是什么?我们必须在这里做sysfreestring,因为它在for循环中?



我尝试过:



m_cTreeCtrl.InsertItem(TVIF_TEXT | TVIF_IMAGE |

TVIF_SELECTEDIMAGE | TVIF_PARAM,strTemp,0,0,0,0,

(LPARAM)strParamval。 AllocSysString(),

m_hMatNode,TVI_LAST);

Hi,

i have query regarding the insert item in tree control as mentioned below:


m_cTreeCtrl.InsertItem(TVIF_TEXT | TVIF_IMAGE |
TVIF_SELECTEDIMAGE
| TVIF_PARAM, strTemp, 0, 0, 0, 0, (LPARAM)(LPCTSTR)(LPCTSTR)strParamval, m_hMatNode, TVI_LAST);

the strparamval is inserted here

when iam retrieving the data iam not able to get the data as mentioned below:

TVITEMEX item;
item.mask = TVIF_PARAM;
item.hItem = hChildItem;
TreeView_GetItem(m_cTreeCtrl, &item);
CString strParam = (LPCTSTR)item.lParam;


this is a for loop so lot of times the function will be called. when i kept below code it worked as mentioned below:

m_cTreeCtrl.InsertItem(TVIF_TEXT | TVIF_IMAGE |
TVIF_SELECTEDIMAGE| TVIF_PARAM, strTemp, 0, 0, 0, 0,
(LPARAM)strParamval.AllocSysString(),
m_hMatNode, TVI_LAST);


but as this logic is loop when i allocate and sysfreestring its giving wrong value for last time.

my doubt is wether we have to do sysfreestring here as it is in for loop ?

What I have tried:

m_cTreeCtrl.InsertItem(TVIF_TEXT | TVIF_IMAGE |
TVIF_SELECTEDIMAGE| TVIF_PARAM, strTemp, 0, 0, 0, 0,
(LPARAM)strParamval.AllocSysString(),
m_hMatNode, TVI_LAST);

推荐答案

如果将指针存储到列表或控件中的数据项,然后释放它,然后它不再存在。因此,当您稍后尝试访问该指针时,您将获得垃圾或空引用异常。您必须将字符串保存在已分配的内存中,并且在不再需要它之前不要将其释放。
If you store the pointer to a data item in a list or control and then free it, then it no longer exists. So when you later try to access that pointer you will get garbage or null reference exception. You must save the string in an allocated piece of memory and not free it until it is no longer required.


这取决于您要在树控件中存储的内容。如果要设置项目文本,请使用 lpszItem 参数.aspx #ctreectrl__insertitem> CTreeCtrl :: InsertItem [ ^ ]方法。这将分配内存来存储传递的文本的副本,并在删除项目时处理释放(或完整的树控件)。要在以后获取该文本,请使用 GetItemText 方法,该方法返回 CString



lParam 参数用于存储有关项目的其他信息。如果那不适合整数,它必须是指向已分配内存的指针(或固定内存,如const字符串)。使用已分配的内存,您负责在删除项目和删除树控件时删除。



如果您想要传递 CString 作为特定于应用程序的参数,使用 new 分配:

It depends on what you want to store in the tree control. If you want to set the item text, use the lpszItem parameter of the CTreeCtrl::InsertItem[^] method. That will allocate memory to store a copy of the passed text and handles freeing when the item is deleted (or the complete tree control). To get that text later use the GetItemText method which returns a CString.

The lParam parameter is provided for storing additional information about an item. If that that does not fit in an integer it must be a pointer to allocated memory (or fixed memory like a const string). With allocated memory, you are responsible for deleting when removing the item and deleting the tree control.

If you want for example to pass a CString as application-specific parameter, allocate that using new:
CString *pStrParam = new CString(strParamval);
m_cTreeCtrl.InsertItem(TVIF_TEXT | TVIF_IMAGE |
TVIF_SELECTEDIMAGE| TVIF_PARAM, strItemText, 0, 0, 0, 0,
    (LPARAM)pStrParam, m_hMatNode, TVI_LAST);

稍后删除该项:

To delete the item later:

TVITEMEX item;
item.mask = TVIF_PARAM;
item.hItem = hChildItem;
TreeView_GetItem(m_cTreeCtrl, &item);
CString *pStrParam = (CString*)item.lParam;
delete pStrParam;

删除控件后,必须对每个项目进行以上操作。

When the control is deleted, the above has to be done for each item.


这篇关于树控制中的Allocsysstring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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