Click here to Skip to main content
Licence 
First Posted 7 Nov 2002
Views 48,869
Bookmarked 13 times

WTL File Dialog Filter String

By | 7 Nov 2002 | Article
Use MFC-style file filter resource strings in your WTL application

Introduction

One neat feature that MFC supports is the ability to store filter strings for use with the CFileDialog in the applications stringtable. The reason special support exists for this is because the OPENFILENAME struct (used by the CFileDialog) expects the filter string components to be delimited using '\0' chars, for example:

Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0

Unfortunately, you can't embed strings containing \0 in your applications string table, so in MFC a pipe symbol is used instead, e.g.:

Text Files (*.txt)|*.txt|All Files (*.*)|*.*|

So, in order to allow strings in this format to be used with WTL applications, you can now use the CFileDialogFilter class. To use the class, first include the header file:

#include "filedialogfilter.h"

Next, simply create a CFileDialogFilter object, passing the ID of the resource string - and then pass it as the lpszFilter parameter to the WTL CFileDialog:

CFileDialogFilter strFilter(IDS_FILTER);
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, 
    strFilter);
// Display
dlg.DoModal();

That's it! The class will automatically load the string and replace any '|' chars with '\0' chars.

CFileDialogFilter

// Implementation of the CFileDialogFilter class.
#pragma once

#include <atlmisc.h>

// Class to support a filter list when using the WTL CFileDialog.
// Allows a filter string delimited with a pipe to be used 
// (instead of a string
// delimited with '\0')
class CFileDialogFilter  
{
private:
    CString m_strFilter;
public:
    CFileDialogFilter()
    {
    }

    /// nID The ID of a resource string containing the filter
    CFileDialogFilter(UINT nID)
    {
        SetFilter(nID);
    }
    
    /// lpsz The filter string
    CFileDialogFilter(LPCTSTR lpsz)
    {
        SetFilter(lpsz);
    }
    
    ~CFileDialogFilter()
    {
    }

    inline LPCTSTR GetFilter() const { return m_strFilter; }
    inline operator LPCTSTR() const { return m_strFilter; }

    // Set the filter string to use
    // nID - The ID of a resource string containing the filter
    void SetFilter(UINT nID)
    {
        if (m_strFilter.LoadString(nID) && !m_strFilter.IsEmpty())
            ModifyString();
    }

    // Set the filter string to use
    // lpsz - The filter string
    void SetFilter(LPCTSTR lpsz)
    {        
        m_strFilter = lpsz;
        if (!m_strFilter.IsEmpty())
            ModifyString();
    }
private:
    // Replace '|' with '\0'
    void ModifyString(void)
    {
        // Get a pointer to the string buffer
        LPTSTR psz = m_strFilter.GetBuffer(0);
        // Replace '|' with '\0'
        while ((psz = _tcschr(psz, '|')) != NULL)
            *psz++ = '\0';
    }
};

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Rob Caldecott

Architect

United Kingdom United Kingdom

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralExcellent contributions PinstaffNishant S19:42 2 Oct '03  
GeneralRe: Excellent contributions PinmemberRobert Edward Caldecott22:05 2 Oct '03  
QuestionReleaseBuffer()? PinmemberPaul Bludov16:18 9 Nov '02  
AnswerRe: ReleaseBuffer()? PinmemberRobert Edward Caldecott21:45 10 Nov '02  
GeneralRe: ReleaseBuffer()? PinmemberPaul Bludov23:10 10 Nov '02  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 8 Nov 2002
Article Copyright 2002 by Rob Caldecott
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid