15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


File name in CFileDialog

Bobi Gilburd -- bobi@netvision.net.il
Thursday, May 02, 1996

I am working on WFW 3.1, VC 1.52 (I know it's old, but our project is still 16 bits...)

In our application there is a file browse dialog.
I want to add a preview button for the file-open common dialog, and when
it pressed i want to preview the currently selected file.
I had no problems with adding all the required controls, but my problem is
to get the name of the currently selected file in the dialog.
I saw that MFC provides 'OnLBSelChangedNotify' handler, but still
I haven't menaged to find the name of the currently selected file.

Thanx in advance,

Bobi Gilburd,
Ramat-Gan, Israel
bobi@netvision.net.il




Rick Esterling -- rick@eco.twg.com
Sunday, May 05, 1996

[Mini-digest: 3 responses]

On  2 May 96 at 22:28, Bobi Gilburd wrote:

> I am working on WFW 3.1, VC 1.52 

Thanks.

[Moderator's note: Sheesh! What is this thing you folks have for
thanking people for info that I require in any question?]

> In our application there is a file browse dialog. I want to add a
> preview button for the file-open common dialog, and when it pressed
> i want to preview the currently selected file. I had no problems
> with adding all the required controls, but my problem is to get the
> name of the currently selected file in the dialog. I saw that MFC
> provides 'OnLBSelChangedNotify' handler, but still I haven't
> menaged to find the name of the currently selected file. 

Try this:

// Include the IDs of the controls on the FileOpen dialog
#include 

LPSTR GetSelFile( void ) {
   char lpszSelFile[ MAX_BUFF ];

   // Retrieve the current subdirectory name (selecting directories
   // in the directory listbox *does* update the value returned by
   // getcwd( )).
   getcwd( lpszSelFile, MAX_BUFF );

   /***
   Add code here to ensure a trailing backslash exists
   at the end of the string
   ***/

   // Get a pointer to the filename listbox (lst1 is defined
   // in dlgs.h)
   CListBox* pLstFiles = (CListBox*) GetDlgItem( lst1 );   
   int nCurSel = pLstFiles->GetCurSel( );

   // Make sure a file is actually selected
   if( nCurSel == LB_ERR ) {
      lpszSelFile = NULL;
   }
   else {
      // Get the selected item from the filename listbox.  If you're
      // filename listbox has multi-select enabled, this will need
      // to be a loop based on the return value of 
      // pLstFlies->GetSelCount( ).
      char lpszFileName[ MAX_BUFF ];
      pLstFiles->GetText( nCurSel, lpszFileName );

      // Put the two strings together
      strcat( lpszSelFile, lpszFileName );
   }

   return lpszSelFile;
}

Please note that this is just the general idea.  I've successfully
used the method described here, but the actual code snippet above is
off-the-cuff and untested. Since your target platform is 16-bit, you
may need to use _fstrcpy( ) and _fstrcat( ) depending on your memory
model and also please see the comments in the code; a couple of them
are very important with regard to trailing backslashes and
multi-select listboxes.

Hope that helps,
Rick

------------------------------------------------------------------------

Richard A. Esterling            Attachmate Internet Products Group (IPG)
Senior Software Engineer        McLean, VA                  703-847-4500
http://widget.eco.twg.com:1080  http://www.twg.com
-----From: "Grant Shirreffs (Great Elk)" 

Couldn't you get the name of the current file from the File Name edit   
box?

-----From: "Fredrik Gunne" 

There are at least two ways of doing this:

1. Override OnFileNameOK. When this function is called, the
OPENFILENAME struct has been filled in with the chosen filename(s).

2. Loop through the entries in the filename listbox, and check what
files have been chosen.
Like this:
   #include 
   pFileList = (CListBox*)GetDlgItem(lst1);  // lst1 is ID for 
the listbox
   for (int i = 0; i < pFileList->GetCount(); i++)
   {
	if (pFileList->GetSel(i))
	{
	     // Use GetText to extract filename
	}
   }

Note that only the filenames are in the listbox, not the complete path. 
Use GetCurrentDirectory to get the path.

Hope this helps

Fredrik Gunne


-- 

Fredrik Gunne
Agema Infrared Systems AB






| Вернуться в корень Архива |