Void CsimpleDlg::OnFileOpen() { CFileDialog *lpszOpenFile; //定義一個CfileDialog對象 lpszOpenFile = new CFileDialog(TRUE,””,"",OFN_FILEMUSTEXIST |OFN_HIDEREADONLY , “文件類型(*.yourfiletype)|*.yourfiletype||”);//生成一個對話框 if(lpzsOpenFile->DoModal() == ID_OK)//假如點擊對話框確定按鈕 { CString szGetName; szGetName = lpszOpenFile->GetPathName(); //得到打開文件的路徑 SetWindowText(szGetName); //在窗口標(biāo)題上顯示路徑 } delete lpszOpenfile; //釋放分配的對話框 }
對一些函數(shù)的解釋: CFileDialog( BOOL bOpenFileDialog, //值為TRUE時表示"打開"對話框,值為FALSE時表"保存”對話框 LPCTSTR lpszDefExt = NULL, //默認(rèn)的文件名擴張 LPCTSTR lpszFileName = NULL, //打開對話框時默認(rèn)出現(xiàn)在文本框里的文件名,,為NULL時不顯示默認(rèn)文名 DWORD dwFlags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST //對文件屬性等定義的組合, LPCTSTR lpszFilter = NULL,//可以應(yīng)用的文件類型,如*.MP3 CWnd* pParentWnd = NULL, // A pointer to the file dialog-box object's parent or owner window. DWORD dwSize = 0 /* The size of the OPENFILENAME structure. This value is dependent on the operating system version, so MFC can determine the appropriate kind of dialog to create (for example, new Windows 2000 dialogs as opposed to NT4 dialogs). */ ); 保存文件對話框和上面代碼相似,,只要把CfileDialog(TRUE,””,""…..) 中的TRUE改為FALSE即可。
|