Preselecting files in a CFileDialog

Discussion in 'MFC' started by ill_comms, Feb 16, 2011.

  1. ill_comms

    ill_comms New Member

    Joined:
    Feb 16, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Just wondering how and if you can pre-select multiple files as the dialog opens? Do you place a list of them in the Initial Filename area, as I've tried this already the answer seems to be, No.
    Am I missing something?

    Basically I'd like a user to be able to select multiple files, I then do some checking of the filenames and if one of these checks comes back false, I want to reload this dialog with the files that the user had previously selected.

    Any help would be greatly appreciated
    Hayden

    Code:
    CFileDialog dlg(FALSE/*Open=TRUE Save=False*/,NULL/*Filename Extension*/,""/*Initial Filename*/,OFN_ENABLESIZING|OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_ALLOWMULTISELECT/*Flags*/,"All Files(*.*)|*.*||"/*Filetype Filter*/);
    	
    	const int nBufferSize = 128*1024; // May be excessive?
    	TCHAR *szNewBuffer = new TCHAR[nBufferSize];
    	memset(szNewBuffer, 0, sizeof(TCHAR) * nBufferSize);
    	dlg.m_ofn.lpstrFile = szNewBuffer;
    	dlg.m_ofn.nMaxFile = nBufferSize - 1;
    	
    	if (dlg.DoModal() == IDOK)
    	{
    		POSITION pos = dlg.GetStartPosition();
    		CString fullpath;
    		CString filenametemp;
    		CString filename;
    		int nLastSlashPos;
    
    		filePaths = "";
    		fileNameList = "";
    		
    		while (pos)
    		{
    			fullpath = dlg.GetNextPathName(pos);
    
    			nLastSlashPos = fullpath.ReverseFind('\\');
    			if(nLastSlashPos >= 0)
    			{
    				filename = fullpath.Mid(nLastSlashPos+1);
    			}
    
    			if (filePaths != "")
    			{
    				filePaths = filePaths + "|";
    				fileNameList = fileNameList + " ";
    			}
    			filePaths = filePaths + fullpath;
    			fileNameList = fileNameList + '"' + filename + '"';
    		}
    	}
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice