首页 > 其他 > 详细

CFileDialog “打开”与“另存为”对话框

时间:2020-02-17 12:51:43      阅读:65      评论:0      收藏:0      [点我收藏+]

CFileDialog “打开”与“另存为”对话框

void CMFCDialogDlg::OnBnClickedButtonOpen()
{
    // 打开对话框
    //CFileDialog fileDlg(TRUE);
    //fileDlg.m_ofn.hwndOwner = this->GetSafeHwnd();//确定父窗口,若NULL,无父窗口
    //fileDlg.m_ofn.lpstrFilter = _T("Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0");//过滤器
    //fileDlg.m_ofn.lpstrDefExt = _T("TXT");//默认扩展名
    //fileDlg.m_ofn.lpstrTitle = NULL;//若NULL,title是“打开”
    //或者
    //static TCHAR szFiler[] = _T("Text Files(*.txt)|*.txt|All Files(*.*)|*.*||");//注意:|前后不要写空格
    CFileDialog fileDlg(TRUE, _T("txt"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
        _T("Text Files(*.txt)|*.txt|All Files(*.*)|*.*||"), this);
    if (IDOK == fileDlg.DoModal())
    {
        CFile file;
        if(!file.Open(fileDlg.GetFileName(), CFile::modeRead))
            return;
        UINT nSize = file.GetLength();
        //ifile.SeekToBegin();
        //int n = ifile.SeekToEnd();
        char* buf = new char[nSize];
        file.Read(buf, nSize);
        //do something ...
        std::string s(buf, nSize);
        delete[] buf;
        file.Close();
    }
}


void CMFCDialogDlg::OnBnClickedButtonSave()
{
    // "另存为"对话框
    CFileDialog fileDlg(FALSE);
    fileDlg.m_ofn.lpstrTitle = _T("我的文件保存对话框");//默认的Title是“另存为”
    fileDlg.m_ofn.lpstrFilter = _T("Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0");
    fileDlg.m_ofn.lpstrDefExt = _T("txt");
    if (IDOK == fileDlg.DoModal())
    {
        CFile file(fileDlg.GetFileName(), CFile::modeCreate | CFile::modeWrite);
        file.Write("http://www.sunxin.org", strlen("http://www.sunxin.org"));
        file.Close();
    }
}

 

CFileDialog “打开”与“另存为”对话框

原文:https://www.cnblogs.com/htj10/p/12320816.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!