ListCtrl控件笔记

ExtendedStyle

 

m_list.SetExtendedStyle(m_list.GetExtendedStyle()|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|);

 

InsertColumn插入列,实际就是加个列头

InsertItem加入一行,实际是加一行的第一列元素,可以设置很多属性,最重要的有个lparam

SetItemText设置一行中其它元素的值

 

单击列头排序

int CALLBACK CmpFunc(LPARAM lParam0, LPARAM lParam1, LPARAM lParam)
{
    CListCtrl *pList = (CListCtrl*)lParam;
    LVCOLUMN stCol;
    LVFINDINFO stFindInfo;
    int iFindItem;
    CString str0;
    CString str1;

    stFindInfo.flags = LVFI_PARAM;
    stFindInfo.lParam = lParam0;

    /* 根据参数lParam找到要排的行 */

    iFindItem = pList->FindItem(&stFindInfo);

    /* 获取要排的元素 */
    str0 = pList->GetItemText(iFindItem, g_curSelCol);

    stFindInfo.flags = LVFI_PARAM;
    stFindInfo.lParam = lParam1;
    iFindItem = pList->FindItem(&stFindInfo);
    str1 = pList->GetItemText(iFindItem, g_curSelCol);

 

    /* 根据列选择排序算法 */

    if (g_curSelCol == 0)
    {
        return iSortDir*strcmp(str0, str1);
    }
    else
    {
        return iSortDir*(atoi(str0) - atoi(str1));
    }


}

 

void ListView_SetHeaderSortImage(HWND listView, int columnIndex, BOOL isAscending)
{
    HWND header = ListView_GetHeader(listView);

    int columnCount = Header_GetItemCount(header);
    for (int i = 0; i<columnCount; i++)
    {
        HDITEM hi = {0};

        //I only need to retrieve the format if i'm on
        //windows xp. If not, then i need to retrieve
        //the bitmap also.
        hi.mask = HDI_FORMAT;

        Header_GetItem(header, i, &hi);

        //Set sort image to this column
        if(i == columnIndex)
        {
            //Windows xp has a easier way to show the sort order
            //in the header: i just have to set a flag and windows
            //will do the drawing. No windows xp, no easy way.
            hi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
            hi.fmt |= isAscending ? HDF_SORTUP : HDF_SORTDOWN;
        }
        //Remove sort image (if exists)
        //from other columns.
        else
        {
            hi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
        }

        Header_SetItem(header, i, &hi);
    }
}

 

void CAboutDlg::OnColumnclickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
     NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
     // TODO: Add your control notification handler code here

    /* 获取要排序的列编号 */

    g_curSelCol = pNMListView->iSubItem;

    /* 排序 */
    m_list.SortItems(CmpFunc, (DWORD_PTR)&m_list);

 

    ListView_SetHeaderSortImage(m_list.GetSafeHwnd(), g_curSelCol, iSortDir>0?TRUE:FALSE);

    iSortDir *= -1;

}

 

关于排序的箭头显示

http://www.winapizone.net/tutorials/winapi/listview/columnsortimage.php

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MFC是Microsoft Foundation Classes的缩写,是微软为了简化Windows编程而开发的一套C++类库。ListCtrl是MFC中的一个控件,用于显示和编辑数据的列表。 要在MFC中实现可编辑的ListCtrl控件,可以按照以下步骤进行操作: 1. 在对话框资源中添加一个ListCtrl控件,并设置其属性,如样式、列数等。可以使用控件向导来简化这一过程。 2. 在对话框类的头文件中声明一个ListCtrl对象,用于对控件进行操作。例如,在CDialog派生类中添加如下成员变量: ```cpp CListCtrl m_listCtrl; ``` 3. 在OnInitDialog函数中获取ListCtrl控件的指针,并进行初始化操作。例如: ```cpp m_listCtrl.SubclassDlgItem(IDC_LISTCTRL, this); //将控件与IDC_LISTCTRL关联起来 m_listCtrl.InsertColumn(0, _T("列1"), LVCFMT_LEFT, 100); //插入列 ``` 4. 通过ListCtrl对象的成员函数,可以实现对控件中的数据进行操作。例如,添加一行数据的代码如下: ```cpp m_listCtrl.InsertItem(0, _T("数据1")); //在第0行插入一行数据 m_listCtrl.SetItemText(0, 1, _T("数据2")); //设置第0行、第1列的数据 ``` 5. 若要实现可编辑功能,可以通过响应鼠标双击、单击或其他事件,在合适的位置进行编辑操作。例如,在双击某一项时弹出对话框进行编辑: ```cpp int index = m_listCtrl.GetSelectionMark(); //获取当前选中的项 CString data = m_listCtrl.GetItemText(index, 1); //获取选中项的数据 CEditDialog editDlg; //自定义的对话框类 editDlg.m_editCtrl.SetWindowText(data); //将数据显示在对话框的编辑框中 if (editDlg.DoModal() == IDOK) { CString newData; editDlg.m_editCtrl.GetWindowText(newData); //获取对话框中编辑框的数据 m_listCtrl.SetItemText(index, 1, newData); //更新ListCtrl控件中的数据 } ``` 通过以上步骤,就可以实现在MFC中可编辑的ListCtrl控件。需要注意的是,在实际应用过程中可能还需要处理其他事件、进行数据校验等操作,具体实现可根据需求进行扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值