none
关于CFileDialog类 RRS feed

  • 问题

  • 在VS2008下学用CFileDialog类打开文件(基于MFC的对话框编程),但是遇到了一个问题,查找多时得不到解决,来此请教:

    代码如下:CFileDialog openDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY| OFN_OVERWRITEPROMPT,_T("文件 (*.xls)|*.xls|所有文件(*.*)|*.*|"),
    (CWnd*)this);  
        CString path = NULL;  
        if (IDOK== openDlg.DoModal())  
       {  
         path=openDlg.GetPathName(); 
      CString fileName = openDlg.GetFileTitle ();
      程序运行后弹出打开文件的对话框,但是选择打开文件却什么都没有打开,为什么?
        

       } 

    • 已移动 Sheng Jiang 蒋晟Moderator 2011年4月26日 6:04 (发件人:Visual Studio 相关讨论(Visual Studio 2010以前版本))
    2011年4月26日 5:01

答案

  • // MSDN 上的例子

    void CMyClass::OnFileOpen()
    {
       // szFilters is a text string that includes two file name filters:
       // "*.my" for "MyType Files" and "*.*' for "All Files."
       TCHAR szFilters[]= _T("MyType Files (*.my)|*.my|All Files (*.*)|*.*||");

       // Create an Open dialog; the default file name extension is ".my".
       CFileDialog fileDlg(TRUE, _T("my"), _T("*.my"),
          OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilters);

       // Display the file dialog. When user clicks OK, fileDlg.DoModal()
       // returns IDOK.
       if(fileDlg.DoModal() == IDOK)
       {
          CString pathName = fileDlg.GetPathName();

          // Implement opening and reading file in here.

          //Change the window's title to the opened file's title.
          CString fileName = fileDlg.GetFileTitle();

          SetWindowText(fileName);
       }
    }


    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
    2011年4月26日 6:18
    版主

全部回复

  • 你好

    或者你可以參考以下URL

    有關CFILEDIALOG 的

    http://msdn.microsoft.com/zh-cn/library/dk77e5e7(v=VS.90).aspx#Y1320

    如果要打開文件..或者你需要以下的CODE

    http://msdn.microsoft.com/en-us/library/e8zac0ca.aspx

     

    E.G.

    Process^ myProcess = gcnew Process;
    	try
    	{
    
    		// Get the path that stores user documents.
    		String^ myDocumentsPath = Environment::GetFolderPath( Environment::SpecialFolder::Personal );
    		// You can start any process, HelloWorld is a do-nothing example.
    		myProcess->StartInfo->FileName = "C:\\MyFile.doc";
    		myProcess->StartInfo->UseShellExecute = false;
    		myProcess->StartInfo->CreateNoWindow = true;
    		myProcess->Start();
    		// This code assumes the process you are starting will terminate itself. 
    		// Given that is is started without a window so you cannot terminate it 
    		// on the desktop, it must terminate itself or you can do it programmatically
    		// from this application using the Kill method.
    	}
    	catch ( Exception^ e ) 
    	{
    		Console::WriteLine( e->Message );
    	}

    Please correct me if my concept is wrong


    Chi
    2011年4月26日 5:23
  • CFileDialog类只负责文件的选择。你如果要打开选择文件,那么需要自己写代码。

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    2011年4月26日 6:05
    版主
  • 能否给一个实例说明,刚学,不太懂!
    2011年4月26日 6:17
  • // MSDN 上的例子

    void CMyClass::OnFileOpen()
    {
       // szFilters is a text string that includes two file name filters:
       // "*.my" for "MyType Files" and "*.*' for "All Files."
       TCHAR szFilters[]= _T("MyType Files (*.my)|*.my|All Files (*.*)|*.*||");

       // Create an Open dialog; the default file name extension is ".my".
       CFileDialog fileDlg(TRUE, _T("my"), _T("*.my"),
          OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilters);

       // Display the file dialog. When user clicks OK, fileDlg.DoModal()
       // returns IDOK.
       if(fileDlg.DoModal() == IDOK)
       {
          CString pathName = fileDlg.GetPathName();

          // Implement opening and reading file in here.

          //Change the window's title to the opened file's title.
          CString fileName = fileDlg.GetFileTitle();

          SetWindowText(fileName);
       }
    }


    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
    2011年4月26日 6:18
    版主