在列表视图中打印选定的项目 [英] print selected item in listview

查看:74
本文介绍了在列表视图中打印选定的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

case WM_NOTIFY:
    LPNMHDR hdr;
    LPNMLISTVIEW nlv;
     
    hdr=(LPNMHDR)lParam;
    nlv=(LPNMLISTVIEW)lParam;
    if(hdr->hwndFrom==hMlb)
    {
        switch(hdr->code)
        {
        case LVN_ITEMCHANGED:
            if(nlv->uChanged==LVIF_STATE&&nlv->uNewState==(LVIS_SELECTED|LVIS_FOCUSED))
            {
            
                LI.iItem=nlv->iItem;
                LI.iSubItem=1;
                ListView_GetItem(hMlb,&LI);
                
                ListView_GetItemText(hMlb,nlv->iItem,1,buf,20);
                SetDlgItemText(hwnd,IDM_mtitle,buf);
            }
            return TRUE;
        }
    }
    break;





实际上,可以在列表视图中选择子项目.但是,在我添加了上面的代码之后,子项目变成了不可选择的行.我要做的是当用户单击子项"1"时在editbox(hMlb)中设置文本.此源代码怎么了?





Actually, subitem was selectable in listview. But, after I added above code, subitem changed into non-selectable line. What I gonna do is that set text in editbox(hMlb) when user click subitem ''1''. what''s wrong with this source code?

推荐答案

如果上面的代码完整,则看起来您在调用ListView_GetItem之前未指定mask .再加上您还没有"ZeroMemoried" LI变量,这意味着mask可以是任何东西.如果碰巧蒙版包含LVIF_TEXT标志,那么您很容易破坏内存.

因此,您的情况下的解决方案是像这样注释这三行(它们的目的是什么?):

If the code above is complete then it looks like you have not specified the mask before calling ListView_GetItem. This, together with the fact that you haven''t "ZeroMemoried" the LI variable means that mask can be anything. If it happens that mask contains LVIF_TEXT flag by any chance then you may easily corrupt your memory.

So, the solution in your case will be to comment these three lines like this (what''s the purpose of them anyway?):

//LI.iItem=nlv->iItem;
//LI.iSubItem=1;
//ListView_GetItem(hMlb,&LI);



如果确实需要调用ListView_GetItem,则需要指定掩码.在使用C结构之前,对它们进行ZeroMemory也是一个好主意.

在旁注中,似乎您分配nlv=(LPNMLISTVIEW)lParam;的时间过早.建议您在case LVN_ITEMCHANGED:行之后将此行进一步下移.



If you really need to call ListView_GetItem you need to specify mask. It is also always a good idea to ZeroMemory the C structs before using them.

On the side note, it looks like that you assigning nlv=(LPNMLISTVIEW)lParam; too early. I recommend you moving this line further down after case LVN_ITEMCHANGED: line.


这篇关于在列表视图中打印选定的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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