如何使用win32API在列表视图中选择一行

时间:2014-03-04 16:34:40

标签: c winapi listview visual-c++

我正在使用C和WIN32API进行编程。我想创建一个列表,然后单击选择一行。我编程了the link。我的程序可以获得点击消息,但该行没有获得焦点。我正在使用WM_NOTIFY来获取消息,我定义了一个函数来部署更多操作。我不想使用MFC。接下来我该怎么办?

ENV:

WIN7 VC6.0 C WINAPI

CODES

int SearchMember_OnNotify(HWND hwnd, int id, LPNMHDR lParam);

|

HANDLE_MSG(hWnd, WM_NOTIFY, SearchMember_OnNotify);

|

int SearchMember_OnNotify(HWND hwnd, int id, LPNMHDR lParam)
{
    switch(id)
    {
        case IDC_LIST_MEMBER: 
        {

            if(lParam->code == NM_DBLCLK)
            {
                MessageBox(hwnd,TEXT("CHANGE"),TEXT(""),MB_OK);

                char Text[255]={0};  
                char Temp[255]={0};
                char Temp1[255]={0};
                int iSlected=0;
                int j=0;

                iSlected=SendMessage(hList,LVM_GETNEXTITEM,-1,LVNI_FOCUSED);

                if(iSlected==-1)
                {
                    MessageBox(hwnd,"No Items in ListView","Error",MB_OK|MB_ICONINFORMATION);
                    break;
                }

                memset(&LvItem,0,sizeof(LvItem));
                LvItem.mask=LVIF_TEXT;
                LvItem.iSubItem=5;
                LvItem.pszText=Text;
                LvItem.cchTextMax=256;
                LvItem.iItem=iSlected;

                SendMessage(hList,LVM_GETITEMTEXT, iSlected, (LPARAM)&LvItem);

                sprintf(Temp1,Text);

                for(j=1;j<=5;j++)
                {
                    LvItem.iSubItem=j;
                    SendMessage(hList,LVM_GETITEMTEXT, iSlected, (LPARAM)&LvItem);
                    sprintf(Temp," %s",Text);
                    lstrcat(Temp1,Temp);
                }

                MessageBox(hwnd,Temp1,"test",MB_OK);

            }

            if(lParam->code == NM_CLICK)
            {
                iSelect=SendMessage(hList,LVM_GETNEXTITEM,-1,LVNI_FOCUSED);

                if(iSelect==-1)
                {                      
                    break;
                }
                index=iSelect;
                flag=1;

                MessageBox(hwnd,TEXT("CLICK"),TEXT("CLICKONCE"),MB_OK);
            }


        }
        break;

        default:
        break;
    }
    return id;
}

预期

enter image description here

点击后该行应该得到焦点。

修改

我使用过ListView_SetItemState。但它不起作用。

        if(lParam->code == NM_CLICK)
        {

        //  iSelect=SendMessage(hList,LVM_GETNEXTITEM,-1,LVNI_FOCUSED);
            iSelect=ListView_GetNextItem(hList,-1,LVNI_FOCUSED);

            if(iSelect==-1)
            {                      
                break;
            }

            ListView_SetItemState(hList,iSelect,LVIS_SELECTED ,LVIS_SELECTED);
            ListView_SetItemState(hList,iSelect,LVIS_FOCUSED ,LVIS_FOCUSED);
            flag=1;
            char a[] = "abc";
            //wsprintf(a,"%s",itoa(iSelect);
            itoa(iSelect, a, 10);

            MessageBox(hwnd,TEXT("CLICK"),TEXT(a),MB_OK);
        }

1 个答案:

答案 0 :(得分:3)

您可以使用ListView_SetItemState更改列表视图的状态。它将更新项目的状态标志。