CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2002
    Posts
    924

    Help with file open from dialog based app.

    I tried searching the forum to see if I could find the answer, but I keep getting the following error: "Fatal error: Maximum execution time of 30 seconds exceeded in /www4/codeguru/codeguru/forum/search.php on line 398
    "
    I also tried searching the net and could not find the info I am looking for, and tried seraching this forum several times but kept getting the error I stated previously.

    Anyway, I want to retrieve the file name and path info using the standard Windows Open File dialog, in a dialog based app.
    I used the MFC app wizard and created the dialog based app skeleton. I then added the menu resources for File and Open to the dialog, and bound the Open command to an OnFileOpen() function. Now I don't now what to put in this function to get it to do what I want.
    I want it to bring up the standard windows Open File Dialog, so that I can browse the files and folders, select the file that I want, and have it return the path and file name info, so I can then open the file for reading and grab some information from the file, etc.

    Can anyone help me with this, or provide a link that has this information. And please remeber that I am a newbie to Visual C++, and take that into account when providing any information.

    Thanks in advance...

  2. #2
    Join Date
    Sep 2002
    Location
    St.Louis, MO
    Posts
    27
    take a look in the msdn. The api you want is GetOpenFileName


    Hope this helps
    Thanks
    Tom Wright

  3. #3
    Join Date
    Sep 2002
    Posts
    924
    Thanks for the reply. One more thing... I looked it up on MSDN and tried adding it to my code but it is not working (I click on File -> Open and nothing happens, except the form seems to flash once, real quick). I know the Menu item is not the problem, because I tried adding a messagebox to the code just to make sure, and the messagebox did pop up when I clicked on open.
    My OS is Windows XP Pro and here is what I did using the information from MSDN.
    Added WINDOWS.H, COMMDLG.H, and COMDLG32.LIB to my project.
    Added the following code to my OnOpenFile function:
    **********************************************
    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;
    ofn.nMaxFile = sizeof(szFile);
    ofn.lpstrFilter = "All\0*.*\0Text\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)
    hf = CreateFile(ofn.lpstrFile, GENERIC_READ,
    0, (LPSECURITY_ATTRIBUTES) NULL,
    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
    (HANDLE) NULL);
    **********************************************

    That's it. Is there something else that I must do, or something different because I am using Windows XP Pro?

  4. #4
    Join Date
    Sep 2002
    Location
    St.Louis, MO
    Posts
    27
    RussG1
    Try this sample out. Works fine on my end. Let me know if you still have problems. It's written using MFC.

    Hope this helps
    Attached Files Attached Files
    Thanks
    Tom Wright

  5. #5
    Join Date
    Sep 2002
    Posts
    924
    Hi tawright915,
    Thanks again for the quick reply. Your code works perfectly.
    I copied that code staright from MSDN, I wonder why they skipped the code that allows you to choose the file, and went straight to opening the file that you (never) chose.
    The link for that code is: MSDN Link Under "Opening a File" (Show Example).
    Thanks for correctling the code and adding your comments
    If you wouldn't mind one more small question.
    I noticed that you did not include WINDOWS.H, and COMMDLG.H and COMDLG32.LIB in your project, and it works fine. Are they automatically included or something? Sorry for all the questions, but I as I stated before, I am new to Visual C++ and am just trying to make sense of it all.

  6. #6
    Join Date
    Sep 2002
    Posts
    924
    Oops, never mind, I think figured it out. I looked at the StdAfx.h file, and noticed that one of the includes was "MFC support for Windows Common Controls", so I assume that the GetOpenFileName function and realted stuff are probably in there.
    Makes sense since it must use that function in the MFC SDI apps.
    Anyway, thanks again for the help...

  7. #7
    Join Date
    Sep 2002
    Location
    St.Louis, MO
    Posts
    27
    Glad is worked for ya. Let me know if you need help with anything else
    Thanks
    Tom Wright

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