Mario.Tapilouw

Saturday, April 18, 2009

Selecting Multiple Files in Visual C++

Due to my job, I have to change the compiler to Visual C++. It's a big job and the first thing I have to do is to familiarize myself to the compiler. This is what I usually need in my program, selecting multiple files.

What you need to do is to use CFileDialog as the class to show the dialog box. So you need to initialize it like this:
CFileDialog * openFileDialog = NULL;

TCHAR strFilter[] = { TEXT("Picture Files (*.bmp)|*.bmp||") };
TCHAR strBuffer[MAX_PATH*10] = _T("");

openFileDialog = ( CFileDialog * )new CFileDialog( TRUE,
TEXT(".bmp"),NULL,
OFN_ALLOWMULTISELECT|OFN_LONGNAMES|OFN_ENABLESIZING,
strFilter, this );

openFileDialog->m_ofn.nMaxFile = sizeof(strBuffer);
openFileDialog->m_ofn.lpstrFile = strBuffer;

then what we need to do is to call this dialog as follow, we can use CStringArray or vector to save the array of filenames.

if(openFileDialog->DoModal() == IDOK)
{
POSITION currentPos;
currentPos = openFileDialog->GetStartPosition();

CString str;
CStringArray
strArray;
vector filenames;

strArray.SetSize(0);
filenames.clear();

while(currentPos != NULL)
{
str = openFileDialog->GetNextPathName(currentPos);
filenames.push_back(str);
m_ListBoxLog.AddString(str);
strArray.Add(str);
}
}

Then if you want to show it, just use listbox:

for(filenamesit=filenames.begin();filenamesit!=filenames.end();filenamesit++)
{
m_ListBoxLog.AddString(*filenamesit);
}

/*
int strCount = strArray.GetCount();

for(int i=0;i<strcount;i++)><
{
m_ListBoxLog.AddString(strArray.ElementAt(i));
}

-2009/4/18-

Labels: ,

0 Comments:

Post a Comment

<< Home