CListCtrl操作大全

    技术2022-05-19  22

    大家都知道,ListControl只能点击第一列来选中想选中的项,而很多朋友想实现点击直接选择一行的功能,其实很简单,直接把它的ExtentedStyle设置一下就可以了,如下:

    m_listcontrol为控件CListCtrl*变量点击选中一行:m_listcontrol.SetExtendedStyle(m_listcontrol.GetExtendedStyle()|LVS_EX_FULLROWSELECT);

    而如何得到具体一项里面的值,又成了很多朋友的问题,其实也很简单,下面是我知道的一种方法:

    得到具体的某一项:CString str;int nId;//首先得到点击的位置POSITION pos=m_listcontrol.GetFirstSelectedItemPosition();if(pos==NULL){MessageBox("请至少选择一项","娃子理财",MB_ICONEXCLAMATION);return;}//得到行号,通过POSITION转化nId=(int)m_listcontrol.GetNextSelectedItem(pos);//得到列中的内容(0表示第一列,同理1,2,3...表示第二,三,四...列)str=m_listcontrol.GetItemText(nId,0);str=m_listcontrol.GetItemText(nId,1);

     

     

     

    【转】 CListCtrl操作大全 List Control控件使用说明 编辑List Control里面的任何子项 2011-03-01 17:56 转载自 anglecloudy 最终编辑 anglecloudy

    今天在网上发现一文很是精典,然其为英文。故浪费点时间翻译了一下,喜欢他写代码的风格,大师风格就是不一样。翻译的不好可以查看原文:

    www.codeproject.com/KB/list/editing_subitems_in_listcontrol.aspx

    另自己写了一篇关于一些ListControl的基本操作(如何获得一项的值,如何选中一行而不是一列等)请查看:hi.baidu.com/anglecloudy/blog/item/d969fd29138093f999250a97.html

    Editing Sub-Items in List Control

    By s.prabhakarreddy本文全面介绍了如何编辑List Control里面的任何子项

     

     

    介绍

    内容有点多,译出来也没多大意思,大致就是说一个VC程序员会常常碰到List Control,List Control有很多方法可以显示数据,可List Control里默认的没有编辑功能,故在此智短文里,那个牛叉的人教你怎么实现这个功能。这篇文章是基于VC MFC滴,用别的功具的娃们当然也可以看看,呵呵,不多说,先实现图上ok exit两个按钮:

    void CMultipleColumnsDlg::OK()

    {

        CDialog::EndDialog (0); // Add this line

     

    }

     

     

    void CMultipleColumnsDlg::OnExit()

    {

        CDialog::EndDialog (0); // Add this line

     

    }

     

    接下来添加一个ListCtrl控件,记得把ListCtrl的style设置成Report,这个是为了实现我们要多行显示的功能

    然后增加一个文本框Edit Box去掉它的Border style属性

     

     

     

     

     

    增加一个InsertItems() 成员函数,用来写入ListControl的显示内容

    void CMultipleColumnsDlg::InsertItems()

    {

        HWND hWnd = ::GetDlgItem(m_hWnd, IDC_LIST1);

     

        // Set the LVCOLUMN structure with the required

     

        // column information

     

        LVCOLUMN list;

        list.mask = LVCF_TEXT |LVCF_WIDTH|

            LVCF_FMT |LVCF_SUBITEM;

        list.fmt = LVCFMT_LEFT;

        list.cx = 50;

        list.pszText   = "S.No";

        list.iSubItem = 0;

        //Inserts the column

     

        ::SendMessage(hWnd,LVM_INSERTCOLUMN,

            (WPARAM)0,(WPARAM)&list);

     

        list.cx = 100;

        list.pszText   = "Name";

        list.iSubItem = 1;

        ::SendMessage(hWnd ,LVM_INSERTCOLUMN,

            (WPARAM)1,(WPARAM)&list);

     

        list.cx = 100;

        list.pszText   = "Address";

        list.iSubItem = 2;

        ::SendMessage(hWnd ,LVM_INSERTCOLUMN,

            (WPARAM)1,(WPARAM)&list);

     

        list.cx = 100;

        list.pszText   = "Country";

        list.iSubItem = 2;

        ::SendMessage(hWnd ,LVM_INSERTCOLUMN,

            (WPARAM)1,(WPARAM)&list);

     

        // Inserts first Row with four columns .

     

        SetCell(hWnd,"1",0,0);

        SetCell(hWnd,"Prabhakar",0,1);

        SetCell(hWnd,"Hyderabad",0,2);

        SetCell(hWnd,"India",0,3);

     

        // Inserts second Row with four columns .

     

        SetCell(hWnd,"2",1,0);

        SetCell(hWnd,"Uday",1,1);

        SetCell(hWnd,"Chennai",1,2);

        SetCell(hWnd,"India",1,3);

     

     

     

     

     

    }

     

     

    自定义的SetCell( ) 函数,用来实现插入数据用的

     

    void CMultipleColumnsDlg::SetCell(HWND hWnd1,

            CString value, int nRow, int nCol)

    {

        TCHAR     szString [256];

        wsprintf(szString,value ,0);

     

        //Fill the LVITEM structure with the

     

        //values given as parameters.

     

        LVITEM lvItem;

        lvItem.mask = LVIF_TEXT;

        lvItem.iItem = nRow;

        lvItem.pszText = szString;

        lvItem.iSubItem = nCol;

        if(nCol >0)

            //set the value of listItem

     

            ::SendMessage(hWnd1,LVM_SETITEM,

                (WPARAM)0,(WPARAM)&lvItem);

        else

            //Insert the value into List

     

            ListView_InsertItem(hWnd1,&lvItem);

     

    }

     

    //通过行和列得到项目里面的数据

     

     

    CString CMultipleColumnsDlg::GetItemText(

     

        HWND hWnd, int nItem, int nSubItem) const

    {

        LVITEM lvi;

        memset(&lvi, 0, sizeof(LVITEM));

        lvi.iSubItem = nSubItem;

        CString str;

        int nLen = 128;

        int nRes;

        do

        {

            nLen *= 2;

            lvi.cchTextMax = nLen;

            lvi.pszText = str.GetBufferSetLength(nLen);

            nRes = (int)::SendMessage(hWnd,

                LVM_GETITEMTEXT, (WPARAM)nItem,

                (LPARAM)&lvi);

        str.ReleaseBuffer();

        return str;

    }

     

     

     

    //为窗口类添加两个成员变量:

     

    int nItem, nSubItem;

     

    用Class wizard 添加 NM_CLICK 响应 ,当用户点击任何位置时,就会对应出现一个Edit Box,并可以修改数据

    void CMultipleColumnsDlg::OnClickList(

            NMHDR* pNMHDR, LRESULT* pResult)

    {

        Invalidate();

        HWND hWnd1 = ::GetDlgItem (m_hWnd,IDC_LIST1);

        LPNMITEMACTIVATE temp = (LPNMITEMACTIVATE) pNMHDR;

        RECT rect;

        //get the row number

        nItem = temp->iItem;

        //get the column number

        nSubItem = temp->iSubItem;

        if(nSubItem == 0 || nSubItem == -1 || nItem == -1)

            return ;

        //Retrieve the text of the selected subItem

        //from the list

        CString str = GetItemText(hWnd1,nItem ,

            nSubItem);

     

        RECT rect1,rect2;

        // this macro is used to retrieve the Rectanle

        // of the selected SubItem

        ListView_GetSubItemRect(hWnd1,temp->iItem,

            temp->iSubItem,LVIR_BOUNDS,&rect);

        //Get the Rectange of the listControl

        ::GetWindowRect(temp->hdr.hwndFrom,&rect1);

        //Get the Rectange of the Dialog

        ::GetWindowRect(m_hWnd,&rect2);

     

        int x=rect1.left-rect2.left;

        int y=rect1.top-rect2.top;

       

        if(nItem != -1)

        ::SetWindowPos(::GetDlgItem(m_hWnd,IDC_EDIT1),

            HWND_TOP,rect.left+x,rect.top+4,

            rect.right-rect.left - 3,

            rect.bottom-rect.top -1,NULL);

        ::ShowWindow(::GetDlgItem(m_hWnd,IDC_EDIT1),SW_SHOW);

        ::SetFocus(::GetDlgItem(m_hWnd,IDC_EDIT1));

        //Draw a Rectangle around the SubItem

        ::Rectangle(::GetDC(temp->hdr.hwndFrom),

           rect.left,rect.top-1,rect.right,rect.bottom);

        //Set the listItem text in the EditBox

        ::SetWindowText(::GetDlgItem(m_hWnd,IDC_EDIT1),str);

        *pResult = 0;

    }

    To handle the ENTER key we need to write the virtual function OnOk (响应ENTER键)

    afx_msg void OnOK();

     

    In MultipleColumnsDlg.cpp write the following code.

     

    // This function handles the ENTER key

    void CMultipleColumnsDlg::OnOK()

    {  

        CWnd* pwndCtrl = GetFocus();

        // get the control ID which is

        // presently having the focus

        int ctrl_ID = pwndCtrl->GetDlgCtrlID();

        CString str;

        switch (ctrl_ID)

        {   //if the control is the EditBox

            case IDC_EDIT1:

            //get the text from the EditBox

            GetDlgItemText(IDC_EDIT1,str);

     

            //set the value in the listContorl with the

            //specified Item & SubItem values

            SetCell(::GetDlgItem (m_hWnd,IDC_LIST1),

                str,nItem,nSubItem);

     

            ::SendDlgItemMessage(m_hWnd,IDC_EDIT1,

              WM_KILLFOCUS,0,0);

            ::ShowWindow(::GetDlgItem(m_hWnd,IDC_EDIT1),

                SW_HIDE);

                break;    

            default:

                break;

        }

    }

     

    最后一步在OnInitDialog 中设置List Control的样式       

    ListView_SetExtendedListViewStyle(::GetDlgItem

            (m_hWnd,IDC_LIST1),LVS_EX_FULLROWSELECT |

            LVS_EX_GRIDLINES);

     

    InsertItems();

    ::ShowWindow(::GetDlgItem(m_hWnd,IDC_EDIT1),SW_HIDE);

     

     


    最新回复(0)