CAcUiFileDialog打開和關閉操作

CFileDialog打開文件和關閉文件很簡單。重點注意文件名過濾器的使用格式,MSDN官方對CFileDialog的定義如下格式:

explicit CFileDialog(
   BOOL bOpenFileDialog,
   LPCTSTR lpszDefExt = NULL,
   LPCTSTR lpszFileName = NULL,
   DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
   LPCTSTR lpszFilter = NULL,
   CWnd* pParentWnd = NULL,
   DWORD dwSize = 0,
   BOOL bVistaStyle = TRUE
);

其他參數參考MSDN文檔,現將文件過濾器部分的定義列出。

[in] lpszFilter

A series of string pairs that specify filters you can apply to the file. If you specify file filters, only files that match filter criteria will appear in the Files list. See the Remarks section for more information about how to work with file filters.

The lpszFilter parameter is used to determine the type of file name a file must have to be displayed in the file list. The first string in the string pair describes the filter; the second string indicates the file name extension to use. Multiple extensions may be specified by using a semicolon (the ';' character) as the delimiter. The string ends with two '|' characters, followed by a NULL character. You can also use a CString object for this parameter.

For example, Microsoft Excel allows users to open files that have extensions .xlc (chart) or .xls (worksheet), among others. The filter for Excel could be written as:

static TCHAR BASED_CODE szFilter[] = _T("Chart Files (*.xlc)|*.xlc|")
   _T("Worksheet Files (*.xls)|*.xls|Data Files (*.xlc;*.xls)|")
   _T("*.xlc; *.xls|All Files (*.*)|*.*||");

按照此說明編寫如下程序:

    TCHAR szFilter[] = TEXT("文本文件(*.NC)|*.NC|文本文件(*.CNC)|*.CNC|所有文件(*.*)|*.*||");
    CAcUiFileDialog fileDlg(FALSE, TEXT("NC"), NULL, 0, szFilter, NULL);
    CString filePath = TEXT("D:\\");
    if(fileDlg.DoModal() == IDOK)
    {
        filePath = fileDlg.GetPathName();
        CFile file;
        file.Open(filePath,CFile::modeCreate | CFile::modeWrite);
        file.Write(GCodeBuf,strlen(GCodeBuf));
        file.Close();
        acutPrintf(TEXT("\nProgram Generate Success"));
        AfxMessageBox(TEXT("程序生成成功"));
    }

打開文件的效果如圖所示:

其中.CNC在文件文件(*.NC)下一條。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章