Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default filename appears truncated in Windows IFileDialog

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();
    }
}
like image 452
Saket Sharad Avatar asked Jan 30 '26 10:01

Saket Sharad


1 Answers

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;
}
like image 65
Tom Jones Avatar answered Feb 02 '26 01:02

Tom Jones



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!