CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jul 2007
    Posts
    20

    How to use the openFileDialog?

    I create a button and openFileDialog to open file, how to i write my code to open the file dialog?

  2. #2
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968

    Re: How to use the openFileDialog?

    Example code:

    Code:
    int BrowseForFile(HWND hParent, LPTSTR szDir, LPTSTR szFile, UINT nIDTitle, UINT nIDFilters)
    {
    	//TestLongFileBrowse();
        OPENFILENAME ofn;
        LPTSTR szPlainName = NULL;
        TCHAR szInitialDir[MAX_PATH * 10] = _T("");
        TCHAR szFileName[MAX_PATH * 10] = _T("");
        TCHAR szFilters[MAX_PATH * 10] = _T("");
        TCHAR szTitle[MAX_PATH * 10] = _T("");
        BOOL bResult = FALSE;
    
        // Load the title
        LoadString(g_hInst, nIDTitle, szTitle, _tsize(szTitle));
    
        // Resolve the initial dir
        if(szDir != NULL)
        {
            if((DWORD_PTR)szDir <= 0x10000)
                GetDlgItemText(hParent, (UINT)(UINT_PTR)szDir, szInitialDir, _tsize(szInitialDir));
            else
                _tcscpy(szInitialDir, szDir);
    
            // If an NT native name, clear it
            if(!_tcsncmp(szInitialDir, _T("\\\\?\\"), 4))
                memmove(szInitialDir, &szInitialDir[4], (sizeof(szInitialDir) - 4 * sizeof(TCHAR)));
        }
    
        // Resolve the file name
        if(szFile != NULL)
        {
            if((DWORD_PTR)szFile <= 0x10000)
                GetDlgItemText(hParent, (UINT)(UINT_PTR)szFile, szFileName, _tsize(szFileName));
            else
                _tcscpy(szFileName, szFile);
    
            // If an NT native name, clear it
            if(!_tcsncmp(szFileName, _T("\\\\?\\"), 4))
                memmove(szFileName, &szFileName[4], (sizeof(szFileName) - 4 * sizeof(TCHAR)));
        }
    
        // Load the filters. They must be separated by "|"
        LoadString(g_hInst, nIDFilters, szFilters, _tsize(szFilters));
        for(int i = 0; szFilters[i] != 0; i++)
        {
            if(szFilters[i] == _T('|'))
                szFilters[i] = 0;
        }
        
        ZeroMemory(&ofn, sizeof(OPENFILENAME));
        ofn.lStructSize     = sizeof(OPENFILENAME);
        ofn.hwndOwner       = hParent;
        ofn.hInstance       = g_hInst;
        ofn.lpstrInitialDir = szInitialDir;
        ofn.lpstrFilter     = szFilters;
        ofn.lpstrFile       = szFileName;
        ofn.nMaxFile        = _tsize(szFileName);
        ofn.lpstrTitle      = szTitle;
        ofn.nMaxFileTitle   = _tsize(szTitle);
        ofn.Flags           = OFN_ENABLESIZING | OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_NOCHANGEDIR | OFN_NONETWORKBUTTON | OFN_PATHMUSTEXIST;
        bResult = GetOpenFileName(&ofn);
    
        if(bResult)
        {
            szPlainName = szFileName;
    
            // Store the initial dir
            if(szDir != NULL)
            {
                _tcscpy(szInitialDir, szFileName);
                szPlainName = GetPlainName(szInitialDir);
                *(szPlainName - 1) = 0;
    
                if((DWORD_PTR)szDir <= 0x10000)
                    SetDlgItemText(hParent, (UINT)(UINT_PTR)szDir, szInitialDir);
                else
                    _tcscpy(szDir, szInitialDir);
            }
    
            // Store the file name
            if(szFile != NULL)
            {
                if((DWORD_PTR)szFile <= 0x10000)
                    SetDlgItemText(hParent, (UINT)(UINT_PTR)szFile, szPlainName);
                else
                    _tcscpy(szFile, szPlainName);
            }
        }
    
        return bResult;
    }
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  3. #3
    Join Date
    Jul 2007
    Posts
    20

    Re: How to use the openFileDialog?

    Thank your sample code. I using the Visual C++ 2005 Express Edition. My button called button1 and my openFileDialog called openFileDialog1. So the sample code can use???

  4. #4
    Join Date
    Jul 2007
    Posts
    17

    Cool Re: How to use the openFileDialog?

    I am using Visual Studio C++ 6.0
    here you can use the open file dialog in this manner
    i think it will be similar


    Code:
    void CProperties::OnButtonTargetbrowse()
    {
    CFileDialog opendialog(TRUE,"*.JPEG;*.BMP","Untitled.bmp",OFN_HIDEREADONLY,"*.jpeg;*.bmp\0*.jpeg;*.bmp\0",NULL);
    opendialog.DoModal();
    }

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: How to use the openFileDialog?

    Quote Originally Posted by Vidya.p.nair
    I am using Visual Studio C++ 6.0
    here you can use the open file dialog in this manner
    i think it will be similar


    Code:
    void CProperties::OnButtonTargetbrowse()
    {
    CFileDialog opendialog(TRUE,"*.JPEG;*.BMP","Untitled.bmp",OFN_HIDEREADONLY,"*.jpeg;*.bmp\0*.jpeg;*.bmp\0",NULL);
    opendialog.DoModal();
    }
    Nope, that is incorrect. CFileDialog is an MFC class. You cannot write MFC applications with VisualC++ 2005 Express Edition.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Jul 2007
    Posts
    20

    Thumbs up Re: How to use the openFileDialog?

    I can open file dialog in my project. There is my sample code:

    Code:
    OpenFileDialog CommonDialog;
    CommonDialog.Title = "Select File";
    CommonDialog.Multiselect = true;
    CommonDialog.CheckFileExists = true;
    CommonDialog.Filter = "All File(*.*) | *.*";
    CommonDialog.FilterIndex = 2;
    
    //if user has clicked on the OK button
    if (CommonDialog.ShowDialog() == System::Windows::Forms::DialogResult::OK)
    {
       //do something here
    }

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to use the openFileDialog?

    Quote Originally Posted by CooLWong
    I can open file dialog in my project. There is my sample code:
    Your code is off-topic for this forum.

    You've made several posts, where each one deals with Managed C++. Your code is not traditional C++. It is CLR or Managed C++, and is off-topic for this forum. There is a managed C++ forum elsewhere here.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Jul 2007
    Posts
    20

    Re: How to use the openFileDialog?

    Quote Originally Posted by Paul McKenzie
    Your code is off-topic for this forum.

    You've made several posts, where each one deals with Managed C++. Your code is not traditional C++. It is CLR or Managed C++, and is off-topic for this forum. There is a managed C++ forum elsewhere here.

    Regards,

    Paul McKenzie

    Sorry, which forum i can discuss??? I think visual c++ question that can ask here.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to use the openFileDialog?

    Quote Originally Posted by CooLWong
    Sorry, which forum i can discuss??? I think visual c++ question that can ask here.
    Your code is not "traditional" Visual C++. It uses classes that only exist in the Managed environment.

    Create a Win32 console or GUI project, and you will see that your code will not compile. And no, it isn't a matter of including some header files -- the syntax used in these programs are not "traditional" C++ syntax.
    Code:
    System::Windows::Forms::DialogResult::OK
    Classes such as this, code that uses "^" in weird places (which I've seen in this forum lately), etc. are *not* C++ or MFC or ATL, but part of the managed extensions.

    If you don't know the difference between the two, then it is highly important that you do get more familiar with the compiler tools and languages that you're using to create programs.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 16th, 2007 at 09:38 PM.

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to use the openFileDialog?

    Quote Originally Posted by CooLWong
    Sorry, which forum i can discuss??? I think visual c++ question that can ask here.
    Post your managed C++ question in the Managed C++ forum.

  11. #11
    Join Date
    Jul 2007
    Posts
    20

    Re: How to use the openFileDialog?

    Quote Originally Posted by Arjay
    Post your managed C++ question in the Managed C++ forum.
    ok, thank you.

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