MFC--文件对话框

BOOL CSeriesDataDlg::OnInitDialog() 
{
    CDialog::OnInitDialog();
    // TODO: Add extra initialization here

    DWORD dwExtStyles = m_ListCtrl.GetExtendedStyle();
    m_ListCtrl.SetExtendedStyle(dwExtStyles | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
    
    m_ListCtrl.InsertColumn(0,"名称",LVCFMT_LEFT,100,0);  //设置列
    m_ListCtrl.InsertColumn(1,"数据",LVCFMT_LEFT,100,1);

    
    return TRUE;  // return TRUE unless you set the focus to a control
      
}


void CSeriesDataDlg::OnDblclkList1(NMHDR* pNMHDR, LRESULT* pResult) 
{
    // TODO: Add your control notification handler code here
    //双击    

    NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

    int nSubItem = pNMListView->iSubItem;    //选择所在列的索引值
    int nSelected = pNMListView->iItem; //选择所在行的索引值
    
    CString str  = m_ListCtrl.GetItemText(nSelected,1);

    if (pmap->GetCount() == 0  || pmap->GetCount() < nSelected || str == "")
    {
        return;        
    }

    
    CInPutDlg dlg;
    dlg.m_Edit1 = str ;
    
    if(IDOK == dlg.DoModal())
    {
        CString str = dlg.m_Edit1;
        //然后使用
        //m_ListCtrl.SetItemText(index,1,str);//0代表的是第一列,以此类推
    
        ModifyDatToData(nSelected, atoi(str));
        UpDataToLC();    

    }
    
    *pResult = 0;
}


void CSeriesDataDlg::OnButton3() 
{
    // TODO: Add your control notification handler code here

    ReMoveData();

    //在map中插入数据
     CString str;    
     int *num =NULL;    

    str = "窗框大面";
    num = new int(52);
    pmap->SetAt(str, num);

    str = "窗框小面";
    num = new int(30);
    pmap->SetAt(str, num);

    str = "中梃大面";
    num = new int(74);
    pmap->SetAt(str, num);

    str = "中梃小面";
    num = new int(30);
    pmap->SetAt(str, num);

    str = "附框压线";
    num = new int(1);
    pmap->SetAt(str, num);

    str = "压线高度";
    num = new int(22);
    pmap->SetAt(str, num);

    str = "固玻入框";
    num = new int(13);
    pmap->SetAt(str, num);

    str = "玻扇入框";
    num = new int(27);
    pmap->SetAt(str, num);

    str = "纱扇入框";
    num = new int(27);
    pmap->SetAt(str, num);

    str = "附框入框";
    num = new int(22);
    pmap->SetAt(str, num);

    str = "中梃入框";
    num = new int(22);
    pmap->SetAt(str, num);

    str = "扇玻入扇";
    num = new int(13);
    pmap->SetAt(str, num);

    UpDataToLC();
    this->SetWindowText("未命名系列");

}

void CSeriesDataDlg::OnButton1() 
{
    // TODO: Add your control notification handler code here

    //打开
    //CString    FilePathName;
    CFileDialog dlg(TRUE,"txt file(*.txt)|*.txt", NULL,
        
        OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
        
        "txt file(*.txt)|*.txt||");///TRUE为OPEN对话框,FALSE为SAVE AS对话框
    //dlg.m_ofn.lpstrInitialDir=_T("d:\\"); //这里就设置了对话框的默认目录d盘
    if(dlg.DoModal()==IDOK)
        FilePathName=dlg.GetPathName();
    else
        return;

    this->SetWindowText(FilePathName) ;
    
    //AfxMessageBox(FilePathName);
    
    FilePathName.Replace("\\","\\\\");

    FileToData();
    
    //AfxMessageBox(FilePathName);

    
}

void CSeriesDataDlg::OnButton2() 
{
    // TODO: Add your control notification handler code here

    //保存

    
    if (pmap->GetCount() == 0  )
    {
        return;        
    }
    
    CFileDialog dlg(FALSE,"txt file(*.txt)|*.txt", NULL,
        
        OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
        
        "txt file(*.txt)|*.txt||");    //TRUE为OPEN对话框,FALSE为SAVE AS对话框
    
    if(dlg.DoModal()==IDOK)
        FilePathName=dlg.GetPathName();
    else return;

    this->SetWindowText(FilePathName);
    
    FilePathName.Replace("\\","\\\\");
    
    if(FilePathName.GetLength() != 0)
    {
        DataToFile();
    }

    
}

void CSeriesDataDlg::DataToFile()
{
    FILE *fp=fopen(FilePathName,"w");        

    CString str ;
    int *num = NULL;
    
    POSITION pos = pmap->GetStartPosition();
    while (pos!=NULL) {
        pmap->GetNextAssoc(pos, str, num);        
        char* ch;
        ch=(char*)(LPCTSTR)str;
        
        //格式化输出 
        fprintf(fp,"%s %d\n",ch, *num);    
    }
    
    fclose(fp);        //关闭文件a,有打开就要有关闭
    //AfxMessageBox("写入成功...");    
    FilePathName = "";
}

void CSeriesDataDlg::UpDataToLC()
{
    m_ListCtrl.DeleteAllItems();

    CString str;    
     int *num =NULL;    

    int i = 0;
    
    POSITION pos = pmap->GetStartPosition();
    while (pos!=NULL) 
    {
        pmap->GetNextAssoc(pos, str, num);
        
        m_ListCtrl.InsertItem(i,   str);//插入行
        
        str.Format("%d", *num);
        m_ListCtrl.SetItemText(i,1, str);//设置该行的不同列的显示字符
        
        i++;
    }


}

void CSeriesDataDlg::FileToData()
{
    //先清空
    ReMoveData();

    //在map中插入数据
    CString str;    
    int *num =NULL;    

    
    FILE *fp=fopen(FilePathName,"r");        
    char a[24];
    int b;
    
    while(!feof(fp))/* feof(fp)文件指针到达文件末尾  !feof(fp) 表示没有到达末尾*/            
    {
        fscanf(fp,"%s %d\n",a,&b);
        
        str.Format("%s",a);
        num = new int(b);
    
        pmap->SetAt(str, num);        

    }
    
        fclose(fp);        //关闭文件a,有打开就要有关闭

        //AfxMessageBox("读入成功...");    
        FilePathName = "";
        UpDataToLC();

}

void CSeriesDataDlg::ReMoveData()
{
    CString str;    
    int *num =NULL;    
    
    POSITION pos = pmap->GetStartPosition();
    while (pos!=NULL) 
    {
        pmap->GetNextAssoc(pos, str, num);    
        
        delete num;
        }
    pmap->RemoveAll();

}

void CSeriesDataDlg::ModifyDatToData(int i, int num)
{
    CString str ;
    int *a = NULL;

    int m =i+1;
    
    POSITION pos = pmap->GetStartPosition();
    while (m)
    {
        pmap->GetNextAssoc(pos, str, a);        
        m--;
    }

    *a = num;

}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值