dlg.m_ofn.lStructSize = sizeof(OPENFILENAME);
if(dlg.DoModal() == IDOK )
{
m_sourcePath=dlg.GetPathName();
}
UpdateData(FALSE);
}
2、浏览文件夹
3、ListView的使用
(1)、对其初始化
// TODO: You may populate your ListView with items by directly accessing
// its list control through a call to GetListCtrl().
int nColumns = 20;//列数
int nRows = 50;//行数
CListCtrl *ctl;
//获取列表视所对应的列表控件
ctl=&GetListCtrl();
//设置列表控件的北京颜色
ctl->SetTextBkColor(RGB(192,192,192));
//设置列的题头
ctl->InsertColumn (0, _T("#"), LVCFMT_LEFT, 20);
//插入列头
for (int nColumn = 1; nColumn < nColumns - 1; nColumn++)
{
ctl->InsertColumn (nColumn, CString ((TCHAR)(_T('A') + /
nColumn - 1)), LVCFMT_LEFT, 70);
}
//插入列表记录
for (int i = 0; i < nRows; i++)
{
CString str;
str.Format ("%d", i);
ctl->InsertItem (i, str);
ctl->SetItemData (i, i);
for (nColumn = 1; nColumn < nColumns - 1; nColumn++)
{
str.Format ("Item (%d, %d)", nColumn - 1, i);
ctl->SetItemText (i, nColumn, str);
}
}
//设置列表控件风格
DWORD dwStyle;
dwStyle=::GetWindowLong(ctl->m_hWnd,GWL_STYLE);
dwStyle|=LVS_REPORT|LVS_SHOWSELALWAYS|LVS_EDITLABELS;
::SetWindowLong(ctl->m_hWnd,GWL_STYLE,dwStyle);
dwStyle=ctl->GetExtendedStyle();
dwStyle|= LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT|/
LVS_EX_HEADERDRAGDROP|LVS_EX_TRACKSELECT;
ctl->SetExtendedStyle(dwStyle);
CListView::OnInitialUpdate();
}
(2)、对其进行操作
strfilename=myFileDlg.GetFileName();
CString strSub;
CString str1;
//定义CStdioFile对象
CStdioFile ExportFile;
ExportFile.Open(strfilename,CFile::modeCreate|CFile::modeWrite,NULL);
//访问列表控件成员
int iHdrItemCount;//字段数
int iListItemCount;//记录数
//获取列表头控件
CHeaderCtrl* pHdrCtl=ctl->GetHeaderCtrl();
iHdrItemCount=pHdrCtl->GetItemCount();
iListItemCount=ctl->GetItemCount();
CString strCount;
strCount.Format("共有记录:%d条。",iListItemCount);
CString time;
//GetCurrentTime() 属于类的静态成员函数,因此直接调用
CTime t=CTime::GetCurrentTime();
CString strTime="CStdioFile文件建立于:20%y-%m-%d %H:%M:%S ";
strTime=t.Format(strTime);
strTime+=strCount;
ExportFile.WriteString(strTime+"/n/n");
//提示信息
strHint.Format("开始写文件%s",strfilename);
AfxMessageBox(strHint);
//控件标签文字
LVCOLUMN lvcom;
lvcom.mask = LVCF_TEXT;
lvcom.cchTextMax = _MAX_PATH;
char strCaption[_MAX_PATH];
lvcom.pszText =strCaption;
// lvcom.pszText = new char[_MAX_PATH];
for(int k=0;k<iHdrItemCount;k++)
{
ctl->GetColumn(k,&lvcom);
str1.Format("%s",lvcom.pszText);
strSub+=MakeStr(str1,15);
}
ExportFile.WriteString(strSub+"/n");
strSub="";
int j=ctl->GetItemCount();
int jj=ctl->GetHeaderCtrl()->GetItemCount();
for(int i=0;i<j;i++)
{
for(int k=0;k<jj;k++)
{
str1=ctl->GetItemText(i,k);
strSub+=MakeStr(str1,15);
}
ExportFile.WriteString(strSub+"/n");
//清空更新
strSub="";
}
ExportFile.Close();
//提示信息
strHint.Format("文件写到%s完毕",strfilename);
AfxMessageBox(strHint);
}
CString CListViewView::MakeStr(CString &str, short size)
{
CONST int STR_LEN = 20;
char buf[STR_LEN];
//将内存全置' '
for(int i=0;i<size;++i)
buf[i]=' ';
CString str1;
str1.Format("%s",buf);
str+=str1;
return str.Left(size);
}