1.在MFC控件中选择列表控件,放在合适位置。
2.在列表控件右键,选择属性。
3.在属性中,设置ID为IDC_LIST。
4.建立类向导:
1)在列表控件右键,选择建立类向导。
2)在类向导-Member Variables中选择IDC_LIST,然后点击右侧的Add Variable。在下拉列框中:
Member Variable name:m_list;Category:Control;Variable type:CListCtrl。
5.添加代码
1)在在OnInitDialog()中添加下面的代码:
//应用LVS_ICON模式:
LONG lStyle;
lStyle= GetWindowLong(m_list.m_hWnd, GWL_STYLE);//获取当前窗口style
lStyle |= LVS_ICON;
lStyle &= ~LVS_SINGLESEL;
SetWindowLong(m_list.m_hWnd, GWL_STYLE, lStyle);//设置style
DWORD dwStyle =m_list.GetExtendedStyle();
//dwStyle=dwStyle| LVS_EX_FULLROWSELECT;//选中某行使整行高亮(只适用与report风格的listctrl)
//dwStyle= dwStyle|LVS_EX_GRIDLINES;//网格线(只适用与report风格的listctrl)
//dwStyle=dwStyle| LVS_EX_CHECKBOXES;//item前生成checkbox控件
m_list.SetExtendedStyle(dwStyle); //设置扩展风格
//载入ico
m_pImageList.Create(64, 64, ILC_COLOR24, 4, 4); //设置行高以及行宽
m_pImageList.Add(::LoadIcon(AfxGetApp()->m_hInstance,(LPCTSTR)IDI_ICON_FOLD)); //文件夹
m_pImageList.Add(::LoadIcon(AfxGetApp()->m_hInstance,(LPCTSTR)IDI_ICON_MP4));//MP4
m_pImageList.Add(::LoadIcon(AfxGetApp()->m_hInstance,(LPCTSTR)IDI_ICON_JPG));//JPG
m_list.SetImageList(&m_pImageList,LVSIL_NORMAL);
2)list列出所有内容
/*函数的功能:列出网址下的所有内容(文件夹、MP4和jpg)
参数解释:网址
*/
void CVPlayDlg::ListItems(CString urlitem)
{
char *FILEPATH= "temp.txt" ;
char* url = (LPSTR)(LPCTSTR)urlitem;
if ( getUrl(url, FILEPATH) )
{
printf( "OK\n" );
}
//获得所有的jpg以及MP4和文件夹
stritems.clear();
string filename="temp.txt";
fstream out;
out.open(filename.c_str(), ios::in);
string content;
string str_sel1="alt=\"[DIR]\"";
string str_sel2="alt=\"[IMG]\"";
string str_sel3="alt=\"[VID]\"";
while (getline(out, content))
{
string str_sels="href=";
int c_strlabel1=content.find(str_sel1); //文件夹
int c_strlabel2=content.find(str_sel2);//jpg
int c_strlabel3=content.find(str_sel3);//mp4
if(c_strlabel1!=content.npos)
{
string c_str=content.substr(c_strlabel1,content.size()-c_strlabel1-1);
int c_strlabel1s=c_str.find(str_sels);
if(c_strlabel1s!=c_str.npos)
{
string c_strs=c_str.substr(c_strlabel1s,c_str.size()-c_strlabel1s-1);
int c_str2label=c_strs.find("\">");
string str=c_strs.substr(6,c_str2label-7);
if(str.size()>0)
{
stritems.push_back(str);
}
}
}
else if(c_strlabel2!=content.npos)
{
string c_str=content.substr(c_strlabel2,content.size()-c_strlabel2-1);
int c_strlabel2s=c_str.find(str_sels);
if(c_strlabel2s!=c_str.npos)
{
string c_strs=c_str.substr(c_strlabel2s,c_str.size()-c_strlabel2s-1);
int c_str2label=c_strs.find("\">");
string str=c_strs.substr(6,c_str2label-6);
if(str.size()>3)
{
stritems.push_back(str);
}
}
}
else if(c_strlabel3!=content.npos)
{
string c_str=content.substr(c_strlabel3,content.size()-c_strlabel3-1);
int c_strlabel3s=c_str.find(str_sels);
if(c_strlabel3s!=c_str.npos)
{
string c_strs=c_str.substr(c_strlabel3s,c_str.size()-c_strlabel3s-1);
int c_str3label=c_strs.find("\">");
string str=c_strs.substr(6,c_str3label-6);
if(str.size()>3)
{
stritems.push_back(str);
}
}
}
}
out.close();
while(m_list.DeleteItem(0))
{
;
}
int item_num=stritems.size();
for(int i=0;i<stritems.size();i++)
{
string str=stritems[i];
LVITEM vItem={0};
vItem.iItem = 0;
vItem.iSubItem = 0;
vItem.mask = LVIF_TEXT | LVIF_IMAGE;
CString strText(str.c_str());
strText = str.c_str();
int l_mp4=strText.Find(".mp4");
int l_jpg=strText.Find(".jpg");
LPTSTR lpszText =(LPTSTR)(LPCTSTR)strText;
vItem.pszText = lpszText;
if(l_mp4>0)
{
vItem.iImage=1; //mp4 index
}
else if(l_jpg>0){
vItem.iImage=2; //image index
}
else
{
vItem.iImage =0; //fold index
}
m_list.InsertItem(&vItem);
}
}
3)list选中内容
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
if(pNMListView->iItem != -1)
{
CString strtemp;
strtemp.Format("单击的是第%d行第%d列",
pNMListView->iItem, pNMListView->iSubItem);
CString str_item=m_list.GetItemText(pNMListView->iItem,0);
}
4)list内容清空
m_list.DeleteAllItems;