刘华世的官方博客

C++语言 文件对话框的调用

void CTestDlg::OnOpen() 
{
    // TODO: Add your control notification handler code here
    //bOpenFileDialog(TRUE):'打开'对话框
    //lpszDefExt:扩展名
    //lpszFileName:文件名称
    //dwFlags:自定义文件对话框
    //lpszFilte:用于指定对话框过滤的文件类型(文件类型说明和扩展名间用"|"分隔,每种文件类型间用"|"分隔,末尾用"||"结束.)
    //pParentWnd:标识文件对话框的父窗口指针
    //CFileDialog dlg(TRUE, NULL, NULL, NULL, NULL, NULL);
    CFileDialog dlg(TRUE);
    CString strPath, strText = "";
    if(dlg.DoModal()==IDOK)
    {
        strPath = dlg.GetPathName();
        SetDlgItemText(IDC_STATIC1, strPath);
        CFile file(strPath, CFile::modeRead);
        char read[10000];
        file.Read(read, 10000);
        for(int i=0;i<file.GetLength();i++)
        {
            strText+=read[i];
        }
        file.Close();
        SetDlgItemText(IDC_EDIT1, strText);
    }
}

void CTestDlg::OnSave() 
{
    // TODO: Add your control notification handler code here
    CFileDialog dlg(FALSE);
    CString strPath, strText="";
    char write[10000];
    if(dlg.DoModal()==IDOK)
    {
        strPath = dlg.GetPathName();
        if(strPath.Right(4)!=".TXT")
            strPath += ".TXT";
        SetDlgItemText(IDC_STATIC2, strPath);
        CFile file(_T(strPath), CFile::modeCreate|CFile::modeWrite);
        GetDlgItemText(IDC_EDIT1, strText);
        strcpy(write, strText);
        file.Write(write,strText.GetLength());
        file.Close();
    }
    
}
posted @ 2012-11-21 17:09  pythonschool  阅读(1038)  评论(0编辑  收藏  举报
刘华世的官方博客