资料收集站

SDL

Wednesday
Dec 03rd
Text size
  • Increase font size
  • Default font size
  • Decrease font size

At the time this article is written, the CListBox::Dir() class member loads only the standard 8.3 type filenames into a list box. In order to access the longer filenames that are allowed under Windows 95 and Windows NT, you must "roll your own" and use the standard C library calls _findfirst() and _findnext(). 

This example assumes that you have created using resources a list box that has been assigned the ID of IDC_LISTBOX. The first thing that you will need is a pointer to the list box. 

	CListBox * pList;

	pList = (CListBox *)GetDlgItem(IDC_LISTBOX));

Make certain that the list box is empty. 

	pList->ResetContent();

Find the first instance of a matching filename, if any. 

	long hnd;

	struct _finddata_t filestruct;

	hnd = _findfirst("*.*",&filestruct);

	if((hnd == -1)) {

		pList->AddString("no files found");

		return; // this assumes that this code is called from a class member


	else if(::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_ARCHIVE) {

		iItem = pList->AddString(filestruct.name);


Get the remaining occurrences of filenames that match the search pattern (*.*). 

	while(!_findnext(hnd,&filestruct)) {		

		if(::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_ARCHIVE) {

			pList->AddString(filestruct.name);

		}


Close the specified search handle and release associated resources. 

	_findclose(hnd);

This particular example searches for ordinary files that match the *.* search pattern and that have the ARCHIVE bit set. This approach will ignore files that have, for example, only the READONLY bit set. If you want to include all files, you might change the logic to include everthing that is not a directory... 

By changing the FILE_ATTRIBUTE_ARCHIVE to FILE_ATTRIBUTE_DIRECTORY and the search pattern to *, you could fill the list box with directories rather than filenames. 
Comments (0)Add Comment

Write comment

busy
 

Google 搜索

在线用户

We have 56 guests online