现在的位置: 首页 > 自动控制 > 工业·编程 > 正文

CListCtrl控件点击修改数据

2012-09-08 09:41 工业·编程 ⁄ 共 2450字 ⁄ 字号 暂无评论

网上关于在CListCtrl控件里面点击直接修改值的东西比较少,其实是个很简单的东西,知识不太容易想到!在这里将代码写下!

void CAdminDialog::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
    // TODO: Add your control notification handler code here
    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
    str1 = 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 + y,rect.right-rect.left,rect.bottom-rect.top,NULL);
    //GetDlgItem(IDC_EDIT1)->SetWindowText(str1);//°ÑEditµÄ¿Ø¼þÖµÉèΪCListCtrlÀïÃæµÄÖµ
    m_List.SetItemText(nItem,nSubItem,"");//½«CListCtrlÀïÃæµÃÖµÉèÖóɲ»¿É¼ûµÄ
   
    ::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 + x,rect.top,rect.right,rect.bottom);
    GetDlgItem(IDC_EDIT1)->GetWindowText(str1);//µÃµ½Ð޸ĺóµÄÖµ£¬ÒÔ·½±ãÈ·¶¨Ð޸ĺ󽫴ËÖµÖ±½Óдµ½CListCtrlÉÏ
   
    //Set the listItem text in the EditBox
    //::SetWindowText(::GetDlgItem(m_hWnd,IDC_EDIT1),str1);   
   
    *pResult = 0;
}

CString CAdminDialog::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);
    } while (nRes == nLen-1);
    str.ReleaseBuffer();
    return str;
}

void CAdminDialog::OnChange()
{
    // TODO: Add your control notification handler code here
    //::SetWindowText(::GetDlgItem(m_hWnd,IDC_EDIT1),str1);
    GetDlgItem(IDC_EDIT1)->GetWindowText(str1);
    m_List.SetItemText(nItem,nSubItem,str1);
    //delete GetDlgItem(IDC_EDIT1);
    GetDlgItem(IDC_EDIT1)->SetWindowText("");
    GetDlgItem(IDC_EDIT1)->ShowWindow(SW_HIDE);
}

给我留言

留言无头像?