C ++ Open file dialog in window

I have C ++ code to display a dialog selection file. I want the user to be able to specify only a file of type selection. The specified type file may be displayed in my dialog box, but the user can enter a file of a different type in the file name, for example, my image

enter image description here

So, how can I get the user to enter only the file name and file of the search type specified in lpstrFilter? OR Can I disable the file name?

This is my code :

const wchar_t* ChooserFile(const char* typeFile)
{
    try
    {
        ZeroMemory( &sfn , sizeof( sfn));
        sfn.lStructSize = sizeof ( sfn );
        sfn.hwndOwner = NULL ;
        wchar_t w_syFile[MAX_PATH];
        //mbstowcs(w_syFile, syFile, strlen(syFile)+1);//Plus null
        size_t convertedChars = 0;
        mbstowcs_s(&convertedChars, w_syFile, MAX_PATH, syFile, _TRUNCATE);
        sfn.lpstrFile = w_syFile ;
        sfn.lpstrFile[0] = _T('\0');
        sfn.nMaxFile = sizeof( syFile );

        //TypeFile
        sfn.lpstrFilter = TEXT("Microsoft Office Word Documents (*.xlsx)\0*.XLSX\0");

        sfn.nFilterIndex =1;
        sfn.lpstrFileTitle = NULL ;
        sfn.nMaxFileTitle = 0 ;
        sfn.lpstrInitialDir=NULL;

        //sfn.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT|OFN_EXPLORER | OFN_ENABLEHOOK ;
        sfn.Flags = OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST|OFN_NOVALIDATE|OFN_HIDEREADONLY  ;
        if (GetOpenFileName( &sfn ) != TRUE)
        {
            wstrPathFile = TEXT("");
            return wstrPathFile.c_str();
        }

        DWORD  retval=0;
        //BOOL   success; 
        TCHAR  buffer[BUFSIZE]=TEXT(""); 
        TCHAR  buf[BUFSIZE]=TEXT(""); 
        TCHAR** lppPart={NULL};

        wchar_t wstrPath[BUFSIZE];
        retval = GetFullPathNameW(sfn.lpstrFile,sfn.nMaxFile,wstrPath,lppPart);
        if (retval==0)
        {
            wstrPathFile = TEXT("");
            return wstrPathFile.c_str();
        }
        std::wstring s(wstrPath);
        wstrPathFile = s;
        wcout<<wstrPathFile<<endl;
        return wstrPathFile.c_str();
    }
    catch (...)
    {
        PrintToFile("ChooserFile","Error");
        wstrPathFile = TEXT("");
        return wstrPathFile.c_str();
    }
}
+4
source share
2 answers

I want the user to be able to specify only a file of type selection.

, , . , , .

:

  • , , , .
  • hook lpfnHook OPENFILENAME. CDN_FILEOK, . . , , .
+4

OFN_EXPLORER OFN_ENABLEHOOK, , , , CDN_FILEOK, / . :

UINT_PTR CALLBACK MyOFNHookProc(HWND hdlg, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
    if (uiMsg == WM_NOTIFY)
    {
        LPOFNOTIFY ofn = (LPOFNOTIFY) lParam;    
        if (ofn->hdr.code == CDN_FILEOK)
        {
            LPOPENFILENAMEW lpOFN = (LPOPENFILENAMEW) ofn->lpOFN;

            LPWSTR lpExt = PathFindExtensionW(lpOFN->lpstrFile);
            if (lstrcmpiW(lpExt, L".XLSX") != 0)
            {
                SetWindowLongPtr(hdlg, DWL_MSGRESULT, 1);
                return 1;
            }
        }
    }

    return 0;
}

std::wstring ChooserFile(const char* typeFile)
{
    OPENFILEAMEW sfn = {0};
    wchar_t w_syFile[MAX_PATH+1] = {0};
    size_t convertedChars = 0;

    sfn.lStructSize = sizeof(sfn);
    sfn.hwndOwner = NULL;
    mbstowcs_s(&convertedChars, w_syFile, MAX_PATH, syFile, _TRUNCATE);
    sfn.lpstrFile = w_syFile;
    sfn.nMaxFile = MAX_PATH;

    //TypeFile
    sfn.lpstrFilter = L"Microsoft Office Word Documents (*.xlsx)\0*.XLSX\0";

    sfn.nFilterIndex = 1;
    sfn.lpstrFileTitle = NULL;
    sfn.nMaxFileTitle = 0;
    sfn.lpstrInitialDir = NULL;

    sfn.lpfnHook = &MyOFNHookProc;

    sfn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOVALIDATE | OFN_HIDEREADONLY | OFN_EXPLORER | OFN_ENABLEHOOK;

    if (!GetOpenFileNameW(&sfn))
        return L"";

    WCHAR szPath[MAX_PATH+1] = {0};

    DWORD retval = GetFullPathNameW(sfn.lpstrFile, MAX_PATH, szPath, NULL);
    if ((retval == 0) || (retval > MAX_PATH))
        return L"";

    std::wstring wstrPath(szPath, retval);
    std::wcout << wstrPath << endl;
    return wstrPath;
}
0

All Articles