VC++中对文件读取操作
获取文件的path
void getPath(){
char filepath[256], sDrive[256], sDir[256], sFname[256], sExt[256], pStrPath[256];
GetModuleFileName(AfxGetInstanceHandle(), filepath, 256);
_tsplitpath_s(filepath, sDrive, sDir, sFname, sExt);
_tcscpy_s(pStrPath, sDrive);
_tcscat_s(pStrPath, sDir);
long nLen = _tcslen(pStrPath);
if (pStrPath[nLen - 1] != '\\')
_tcscat_s(pStrPath, "\\");
g_BasePath = pStrPath;
}
分隔字符串
void split(CString source, CStringArray &dest, CString division){
if (source.IsEmpty())
{
//MessageBox(_T("文件内容为空"));
}
else
{
int pos = source.Find(division);
if (pos == -1)
{
dest.Add(source);
}
else
{
dest.Add(source.Left(pos));
source = source.Mid(pos + 1);
split(source, dest, division);
}
}
}
读取文件
void ReadFile(){
CStdioFile file;
CString str;
CStringArray dest = {};//用来获得数据
CString division = "\t";
str.Format("%sfilesave\\handshake.txt", g_BasePath);
if (file.Open(_T(str), CFile::modeRead | CFile::typeText)){
file.SeekToBegin();
int readline = 0;
while (file.ReadString(str)&(readline<16)){
readline++;
C_Handshake::split(str, dest, division);
if (readline == 1){
c_Delay = dest.GetAt(0);//读取延时粗调
all_Delay = dest.GetAt(1);//读取总延时
}
else if (readline == 2){
ct_Mode = dest.GetAt(0);
}
else{
for (int i = 0; i < dest.GetSize(); i++){
number[readline - 3][i] = dest.GetAt(i);
}
}
dest.RemoveAll();//清除dest
}
file.Close();
}
else{
MessageBox(_T("文件打开失败"));
}
}
ClistControl初始化:各路时钟信号控制
void C_Handshake::CList_OnInit(){
LONG lStyle;
lStyle = GetWindowLong(list_handshake.m_hWnd, GWL_STYLE);//获取当前窗口style
lStyle &= ~LVS_TYPEMASK; //清除显示方式位
lStyle |= LVS_REPORT; //设置style
SetWindowLong(list_handshake.m_hWnd, GWL_STYLE, lStyle);//设置style
CFont font; //在头文件中声明
font.CreatePointFont(200, _T("楷体"));
list_handshake.SetFont(&font, true);
DWORD dwStyle = list_handshake.GetExtendedStyle();
dwStyle |= LVS_EX_FULLROWSELECT;//选中某行使整行高亮(只适用与report风格的listctrl)
dwStyle |= LVS_EX_GRIDLINES;//网格线(只适用与report风格的listctrl)
list_handshake.SetExtendedStyle(dwStyle); //设置扩展风格
int row_weight = 105;//设置长度
CString column_name[10] = { "", "小周期数", "脉宽", "小周期值", "大周期数", "有信号数", "大周期值", "延时粗调", "延时细调", "总延时" };
list_handshake.InsertColumn(0, column_name[0], LVCFMT_CENTER, 90);
for (int i = 1; i < 10; i++){
list_handshake.InsertColumn(i, column_name[i], LVCFMT_CENTER, row_weight);
}
for (int row = 0; row < 13; row++){
list_handshake.InsertItem(row, number[row][0]);
for (int column=1; column < 10; column++){
list_handshake.SetItemText(row, column, number[row][column]);
}
}
}
对list添加双击响应事件
void C_Handshake::OnNMDblclkListhandshake(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
NM_LISTVIEW *pEditCtrl = (NM_LISTVIEW *)pNMHDR;
CRect EditRect;
int rowCount = list_handshake.GetItemCount();//获取行数
if (pEditCtrl->iSubItem == 0){
//当点击其他区域时,显示内容
m_Edit.ShowWindow(SW_HIDE);
return; };//点击其他内容内容不变
if (pEditCtrl->iItem < rowCount && pEditCtrl->iSubItem != 0 && pEditCtrl->iSubItem != 3
&& pEditCtrl->iSubItem != 6 && pEditCtrl->iSubItem != 9){
row = pEditCtrl->iItem; //行数
column = pEditCtrl->iSubItem; //列数
list_handshake.GetSubItemRect(row, column, LVIR_LABEL, EditRect); //获取单元格的空间
if (m_Edit.m_hWnd == NULL)//m_Edit.m_hWnd == NULL说明还没创建
{
//needSave = true;
m_Edit.Create(ES_AUTOHSCROLL | WS_CHILD | ES_LEFT | ES_WANTRETURN | WS_BORDER,CRect(0, 0, 0, 0), this, IDC_EDIT);
m_Edit.ShowWindow(SW_HIDE); //Edit创建完后隐藏
m_Edit.SetFont(this->GetFont(), FALSE);//设置字体
}
m_Edit.SetParent(&list_handshake); //将list control设置为父窗口,生成的Edit才能正确定位,这个也很重要,
EditRect.SetRect(EditRect.left, EditRect.top, EditRect.left + list_handshake.GetColumnWidth(column), EditRect.bottom);
CString strItem = list_handshake.GetItemText(row, column);
m_Edit.MoveWindow(&EditRect);
m_Edit.ShowWindow(SW_SHOW);
m_Edit.SetWindowText(strItem);
m_Edit.SetFocus();//设置为焦点
m_Edit.SetSel(0, -1); //0,-1表示单元格内容全选中
}
*pResult = 0;
}