CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2002
    Location
    United Kingdom
    Posts
    491

    Win32 Open/Save Dialog Box

    Hi,

    I have used the MFC classes for the windows common dialog boxes numerous times. For a college project I am using a standard Win32 application without the use of MFC. How can I display the file selection dialog box (CFileDialog in open mode) and then retrieve the selected file and path into a char[] array?

    Any help would be greatly appreciated!

    Regards,
    Lea Hayes

  2. #2
    Join Date
    Nov 2002
    Location
    Israel
    Posts
    182
    BOOL SelectFileName(LPCTSTR lpctstrInitFileName, LPTSTR lptstrSelectedFileName, DWORD dwCapacity)
    {
    BOOL bOut = ((lptstrSelectedFileName != NULL) && (dwCapacity > 0));
    if (bOut)
    {
    ZeroMemory(lptstrSelectedFileName, dwCapacity * sizeof(TCHAR));
    }

    OPENFILENAME ofn;

    TCHAR strFileName[MAX_PATH];
    TCHAR strFileTitle[MAX_PATH];
    TCHAR strInitialDir[MAX_PATH];

    ZeroMemory(&ofn, sizeof(OPENFILENAME));
    ZeroMemory(strFileName, MAX_PATH * sizeof(TCHAR));
    ZeroMemory(strFileTitle, MAX_PATH * sizeof(TCHAR));
    ZeroMemory(strInitialDir, MAX_PATH * sizeof(TCHAR));

    ::GetModuleFileName(::GetModuleHandle(NULL), strInitialDir, MAX_PATH);

    if (lpctstrInitFileName != NULL)
    {
    _tcsncat(strFileName, lpctstrInitFileName, MAX_PATH);
    }

    ofn.lStructSize = sizeof(ofn);
    ofn.lpstrFile = strFileName;
    ofn.nMaxFile = MAX_PATH;
    ofn.lpstrDefExt = ".txt";
    ofn.lpstrFileTitle = strFileTitle;
    ofn.nMaxFileTitle = MAX_PATH;
    ofn.Flags |= OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLEHOOK | OFN_ENABLESIZING;
    ofn.lpstrFilter = "Report\0*.rep\0All Files (*.*)\0*.*\0";
    ofn.hInstance = ::GetModuleHandle(NULL);
    ofn.hwndOwner = GetAppMainWindow();
    ofn.lpstrInitialDir = strInitialDir;

    BOOL bRet = ::GetSaveFileName(&ofn);

    if ((bRet)&&(bOut))
    {
    _tcsncat(lptstrSelectedFileName, strFileName,
    MAX_PATH > dwCapacity - 1 ? dwCapacity - 1 : MAX_PATH);
    }

    return (bRet);
    }

    Sorry, that's for file saving.
    Last edited by Caprice; December 3rd, 2003 at 01:21 PM.
    Good luck

  3. #3
    Join Date
    Dec 2003
    Location
    United States
    Posts
    9
    The function I would use is... GetOpenFileName()...

    To Quote MSDN...
    The GetOpenFileName function creates an Open dialog box that lets the user specify the drive, directory, and the name of a file or set of files to open.
    As for placing the filename in a char array...

    The GetOpenFileName() function takes 1 parameter, an 'OPENFILENAME' structure. That structure has a member 'LPTSTR lpstrFile' which you would set to your array. Then you would call GetOpenFileName()...

    Some sample code below...

    Code:
    OPENFILENAME ofn={0};
    char array[MAX_PATH]={0};
    
    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = hWnd; // Handle to parent window
    ofn.lpstrFile = array;
    ofn.nMaxFile = MAX_PATH;
    
    GetOpenFileName(&ofn);

  4. #4
    Join Date
    Jan 2002
    Location
    United Kingdom
    Posts
    491
    Hi,

    Thanks for your help!

    Regards,
    Lea Hayes

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