Oleg Krylov
Oleg Krylov

Reputation: 11

Open file dialog with filter

I am using C++ and MFC. I need to modify Open File Dialog so only folders contains *.txt files must be visible. Any ideas?

Upvotes: 0

Views: 1863

Answers (2)

acraig5075
acraig5075

Reputation: 10756

Use GetOpenFileName and set the filter member of the OPENFILENAME struct instance appropriately.

char szFile[MAX_PATH];

OPENFILENAME ofn = {0};
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = m_hWnd;
ofn.lpstrFile = szFile;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = "Text\0*.TXT\0";
ofn.nFilterIndex = 0;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

if (GetOpenFileName(&ofn)) ...

Upvotes: -1

tcb
tcb

Reputation: 2824

I don't have any experience with programming with MFC, but these are my doubts.

There is no reason to do so especially if you are going to filter folders on all possible levels. Just imagine user opens C: and you program starts scanning the whole disk. It would be even worse if this disk is connected through network.

That's the reason why I doubt that there is such ability in MFC, so you will have to write such a dialog and it's possible only if you decide to filter folder using only some fixed level of nesting.

Upvotes: 2

Related Questions