CListCtrl 使用

初始化 在 OnInitDialog中

    //设置字体,以免出现乱码
    ListFont.CreateFont(0, 0, 0, 0, FW_NORMAL, 0, 0, 0, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Fixedsys");  
    FileNameList.SetFont(&ListFont);


    FileNameList.InsertColumn(0, _T("No"), LVCFMT_LEFT, 34);
    FileNameList.InsertColumn(1, _T("Size"), LVCFMT_LEFT, 68);
    FileNameList.InsertColumn(2, _T("Name"), LVCFMT_LEFT, 180);

    //比较平的滚动条
    FileNameList.SetExtendedStyle(FileNameList.GetExtendedStyle() | LVS_EX_FLATSB);
    //选中一整行: 
    FileNameList.SetExtendedStyle(FileNameList.GetExtendedStyle() | LVS_EX_FULLROWSELECT);
    //绘制表格
    // FileNameList.SetExtendedStyle(FileNameList.GetExtendedStyle() | LVS_EX_GRIDLINES);  
    //带复选框:  
    FileNameList.SetExtendedStyle(FileNameList.GetExtendedStyle() | LVS_EX_CHECKBOXES);  
    //自动切换
    //FileNameList.SetExtendedStyle(FileNameList.GetExtendedStyle() | LVS_EX_TRACKSELECT);

 

 

 

事件处理函数

 

 

    void CS320Dlg::OnBtnAddFile()
{
    int nCount = FileNameList.GetItemCount();
    int;
    CFile file;

    CString Name, tmp;
    CFileDialog dlg(TRUE, NULL, NULL, OFN_ALLOWMULTISELECT, NULL, NULL);
    dlg.m_ofn.nMaxFile = 100 * MAX_PATH;        //文件路径和文件名存储缓冲
    char* pc = new char[100 * MAX_PATH];
    dlg.m_ofn.lpstrFile = pc;
    dlg.m_ofn.lpstrFile[0] = NULL;

    int iReturn = dlg.DoModal();
    if( iReturn == IDOK )
    {
        POSITION pos = dlg.GetStartPosition();
        while( pos != NULL )
        {
            Name = dlg.GetNextPathName(pos);

            tmp.Format("%d", nCount + selected + 1);
            FileNameList.InsertItem(nCount + selected, tmp);                            //写第一列

            file.Open(Name, CFile::modeRead);
            tmp.Format("%.2fK", (float) (file.GetLength()) / 1024);
            file.Close();

            FileNameList.SetItemText(nCount + selected, 1, tmp);                       //第三列;
            FileNameList.SetItemText(nCount + selected, 2, Name);   //第二列
            selected++;
        }
        tmp.Format("Selected files:%d/r/n", selected);
        DownloadInfo += tmp;
    }

    if( CommDlgExtendedError() == FNERR_BUFFERTOOSMALL )
    {
        tmp.Format("Selected files are too large!");
        DownloadInfo += tmp;
    }
    delete[]pc;


    FileNameList.EnsureVisible(FileNameList.GetItemCount() - 1, TRUE);   //自动滚动
    UpdateData(false);
}

void CS320Dlg::OnBtnDelFile()
{
    CString str;
    int i = FileNameList.GetItemCount();
    for( ;i >= 0;i-- )
    {
        if( FileNameList.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED || FileNameList.GetCheck(i) )
            FileNameList.DeleteItem(i);
    }
    for( i = FileNameList.GetItemCount();i >= 0;i-- )//更新序号
    {
        str.Format("%d", i + 1);
        FileNameList.SetItemText(i, 0, str);                       //第三列;
    }
}

void CS320Dlg::OnBtnDelAllFile()
{
    while( FileNameList.DeleteItem(0) );//删除了第一列后,后面的列会依次向上移动。
}

    UpdateData(false);

你可能感兴趣的:(File,null,delete,Path,character,float)