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

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();
    }
}

 

posted @ 2020-02-17 11:05  htj10  阅读(314)  评论(0编辑  收藏  举报
TOP