CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2004
    Location
    Cornwall UK
    Posts
    144

    Need help with creating 'standard' file open dialog

    Hi, I am aware there are several articles on "modifying" and "Enhancing" the standard fileOpen Dialog but I havent found anything on creating a fileopen dialog in the first place.

    I dont know if you have to create all the combobox controls etc yourself or there is some pre-defined template.

    Any pointers*?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396
    MSDN -> Search for CFileDialog Enhancing.
    If this will be not enouph then be more clear which additional features you want to implement in the standard File open/close dialog

  3. #3
    Join Date
    Jun 2002
    Posts
    395
    It's easier than you think if you are using MFC:

    Code:
      CFileDialog fileDlg(TRUE, L"txt", L"foo.txt");
      // Display the file dialog. When user clicks OK, fileDlg.DoModal() 
      // returns IDOK.
      if( fileDlg.DoModal ()==IDOK )
      {
         CString filename =  fileDlg.GetPathName();
         ...
      }
    For non-MFC it is slightly trickier, but straightforward:

    Code:
    OPENFILENAME ofn;       // common dialog box structure
    char szFile[260];       // buffer for file name
    HWND hwnd;              // owner window
    HANDLE hf;              // file handle
    
    // Initialize OPENFILENAME
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = hwnd;
    ofn.lpstrFile = szFile;
    
    // Set lpstrFile[0] to '\0' so that GetOpenFileName does not 
    // use the contents of szFile to initialize itself.
    ofn.lpstrFile[0] = '\0';
    ofn.nMaxFile = sizeof(szFile);
    ofn.lpstrFilter = "Text\0*.TXT\0";
    ofn.nFilterIndex = 1;
    ofn.lpstrFileTitle = NULL;
    ofn.nMaxFileTitle = 0;
    ofn.lpstrInitialDir = NULL;
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
    
    // Display the Open dialog box. 
    if (GetOpenFileName(&ofn)==TRUE) {
        ...
        ofn.lpstrFile contains the file name
    }

  4. #4
    Join Date
    Feb 2004
    Location
    Cornwall UK
    Posts
    144
    Cool, its working now

    It doesent like This part:
    OFN_FILEMUSTEXIST| szFilters, this);

    taken from

    // szFilters is a text string that includes two file name filters:
    // "*.my" for "MyType Files" and "*.*' for "All Files."
    TCHAR CChildFrame::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| szFilters, this);

    but i dont mind just using

    map_open fileDlg (TRUE, _T("my"), _T("*.jpg"));// OFN_FILEMUSTEXIST| szFilters, this); and commenting out the "Type" filter

  5. #5
    Join Date
    Jun 2002
    Posts
    395
    Originally posted by Vonny232
    It doesent like This part:
    OFN_FILEMUSTEXIST| szFilters, this);
    Should be OFN_FILEMUSTEXIST , szFilters

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured