2

When using the Windows IFileDialog interface to launch File browser dialog, I face an issue if the default filename provided exceeds certain number of characters.

The filename appears truncated, although it is simply wrapped around so that we can only see last few characters. It seems the issue lies with the Windows file browser dialog. Whenever the default filename provided exceeds 12-13 characters, it gets wrapped around.

Has anyone encountered such an issue? Is there any workaround?

OS detail:
Windows 10, Version 1709 (OS Build 16299.1625)

Dialog snapshot: File browser dialog with wrapped default filename

Code snippet shared below:
This is the function that gets called from an MFC application when a button - "BrowseFile" is clicked.

void CCustomFileBrowserNewDlg::OnBnClickedBrowseFile()
{
    IFileDialog* pfd = nullptr;
    IID id = CLSID_FileSaveDialog;

    const COMDLG_FILTERSPEC c_rgSaveTypes[] =
    {
        {L"Word Document (*.doc)",       L"*.doc"},
        {L"Web Page (*.htm; *.html)",    L"*.htm;*.html"},
        {L"Text Document (*.txt)",       L"*.txt"},
    };


    HRESULT hr = CoCreateInstance(id, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd));
    if (SUCCEEDED(hr))
    {
        hr = pfd->SetFileTypes(ARRAYSIZE(c_rgSaveTypes), c_rgSaveTypes);
        if (SUCCEEDED(hr))
        {
            hr = pfd->SetFileTypeIndex(1);
            if (SUCCEEDED(hr))
            {
                //pfd->SetFileName(L"Filename.txt");       // This is okay
                pfd->SetFileName(L"SomeLongFilename.txt"); // This name gets wrapped around
                pfd->Show(::GetActiveWindow());
            }
        }

        pfd->Release();
    }
}
Saket Sharad
  • 392
  • 2
  • 11
  • 3
    It works for me. Could you show a mini, complete and reproducible code sample and your OS version? – Rita Han Feb 12 '20 at 08:15
  • @RitaHan-MSFT Microsoft Windows 10 – Saket Sharad Feb 12 '20 at 09:08
  • What's the build version? – Rita Han Feb 12 '20 at 09:09
  • @RitaHan-MSFT Windows 10, Version 1709 (OS Build 16299.1625) – Saket Sharad Feb 12 '20 at 09:11
  • 1
    Yeah, I also think we need to see a [mcve] – David Heffernan Feb 12 '20 at 10:23
  • @DavidHeffernan This snippet is part of an MFC application. The code shown is the function that gets called when "Browse" button is clicked from the MFC application. – Saket Sharad Feb 12 '20 at 10:45
  • That's nice to know. But who wants to spend time recreating that? The onus is on you to make it as simple as possible for us to reproduce the issue. If you make the [mcve] then multiple people here benefit, and furthermore, we know that we are running the same code. If I try to reproduce this and fail because there is some key part missing in your post (because you didn't realise it was significant) that's just a waste of time. – David Heffernan Feb 12 '20 at 10:58
  • @SaketSharad You can test a Win32 C++ application of Visual Studio project template to see if it can reproduce the same issue. It works for me on both Windows 7 and Windows 10. – Rita Han Feb 12 '20 at 10:58
  • @RitaHan-MSFT Yes I have already tested that on Visual Studio. Functionality wise there is no issue, problem lies in the fact that if you provide a long filename to IFileDialog::SetFileName() API, then it appears truncated when the file dialog shows up. No issues if filename is short. – Saket Sharad Feb 12 '20 at 11:43
  • Why won't you make a [mcve]? It's almost as if you don't want help. – David Heffernan Feb 12 '20 at 12:07
  • @SaketSharad Does file name **"SomeLongFilename.txt"** reproduce the same issue in Win32 C++ application (not MFC)? How long do you mean about "a long filename"? – Rita Han Feb 13 '20 at 00:19
  • @RitaHan-MSFT I have not tried with a non-MFC Win32 C++ application. But I know that this issue was NOT present with the older CFileDialog class. In the different trials that I have done, I guess anything beyond 12-13 characters in filename field will produce this problem. – Saket Sharad Feb 13 '20 at 04:55

3 Answers3

1

I found a workaround for this issue by setting the focus to another control and back to the filename edit box.

STDMETHODIMP MyFileDialogEventsImplementation::OnSelectionChange(IFileDialog* pfd)
{   
    if (!m_bInitialized)
    {
        m_bInitialized = true;

        IOleWindow* pOleWindow;
        if (SUCCEEDED(pfd->QueryInterface(IID_PPV_ARGS(&pOleWindow))))
        {
            HWND hwnd;
            if (SUCCEEDED(pOleWindow->GetWindow(&hwnd)))
            {
                CWnd* pDialog = CWnd::FromHandle(hwnd);
                
                if (pDialog != nullptr)
                {
                    CWnd* pCtrlWithFocus = pDialog->GetFocus();
                    
                    if (pCtrlWithFocus != nullptr)
                    {
                        CWnd* pNextDlgTabItem = pDialog->GetNextDlgTabItem(pCtrlWithFocus);

                        if (pNextDlgTabItem != nullptr)
                        {
                            pNextDlgTabItem->SetFocus();
                            pCtrlWithFocus->SetFocus();
                        }
                    }
                }

            }
            pOleWindow->Release();
        }
    }

    return S_OK;
}
Tom Jones
  • 11
  • 1
0

Few errors in your example. An example that you provided is of save dialog and screenshot is open dialog.

The issue that you are talking about will never reproducible on save dialog because filter combo box and save button are on the next line of default text edit control.

enter image description here

Now your problem is specific to open dialog and default text is not truncating its hiding towards left due to less space availability in the same line. i.e. since file name static text, default text edit control and filter combo box are on the same line, windows interpret this as less availability of space for default text and wrap the text towards left. If you scroll text cursor towards file name static control then you will get complete text. You may raise this concern to MSDN.

enter image description here

Now, a workaround to your problem is to show open file dialog in maximize mode. One way to do this is once a dialog is open then maximize it. Next onwards dialog will open in maximize mode.

Santosh Dhanawade
  • 1,756
  • 14
  • 29
0

Expanding on Tom's answer from earlier .. if your using MFC like me then this works... OnFileNameChange was the only override that actually seems to work when Vista mode is set. So derive from CFileDialog and override OnFileNameChange then call Tom's code.

Gary

void CThumbnailFileDialog::OnFileNameChange()
{

//total hack to fix the microsoft screw up... that's still broke after 6 years !

    CFileDialog::OnFileNameChange();

    IFileOpenDialog* openDlgPtr = GetIFileOpenDialog();

    // Make sure that it is not null
    if (openDlgPtr != NULL)
    {

        IOleWindow* pOleWindow;
        if (SUCCEEDED(openDlgPtr->QueryInterface(IID_PPV_ARGS(&pOleWindow))))
        {
            HWND hwnd;
            if (SUCCEEDED(pOleWindow->GetWindow(&hwnd)))
            {
                CWnd* pDialog = CWnd::FromHandle(hwnd);

                if (pDialog != nullptr)
                {
                    CWnd* pCtrlWithFocus = pDialog->GetFocus();

                    if (pCtrlWithFocus != nullptr)
                    {
                        CWnd* pNextDlgTabItem = pDialog->GetNextDlgTabItem(pCtrlWithFocus);

                        if (pNextDlgTabItem != nullptr)
                        {
                            pNextDlgTabItem->SetFocus();
                            pCtrlWithFocus->SetFocus();
                        }
                    }
                }

            }
            pOleWindow->Release();


        }
        // Release the pointer
        openDlgPtr->Release();
    }

}