Click here to Skip to main content
Click here to Skip to main content

The Ultimate Toolbox Resource File

, 25 Aug 2007
Rate this:
Please Sign up or sign in to vote.
A class for enumerating and parsing file based resources from the Ultimate Toolbox

Visit the Ultimate Toolbox main page for an overview and configuration guide to the Ultimate Toolbox library.

Source code and project files for this sample can be found in the samples\utility\resfile directory of the sample projects download.

Overview

The COXResourceFile provides a resource with a CSharedFile (thus CFile) interface. One can read or write any type (e.g. a GIF-picture, a movie etc. as in RT_RCDATA) of resources directly through the familiar methods Read(), Write(), Seek(), Flush(), Close(), etc.. The resource will be loaded into memory and wrapped by a COXResourceFile. All changes will be applied to the resource in memory and will be committed when the file is flushed.

The COXResourceLibrary wraps the Win32 API's resource functions (together with COXResourceFile). One object of the COXResourceLibrary corresponds to a library file (an executable: .DLL or .EXE file), whereas one object of the COXResourceFile corresponds to one resource in a library.

Features

  • Direct access of a resource's binary data through the familiar CFile interface.
  • Easy multiple resources update.
  • Minimum code to be written when implementing drag/drop/copy/paste of resources.
  • Easy to enumerate resources in a library file.
  • Flexible parameter data types when specifying the type or name of a resource.

Usage

The samples\utility\resfile sample contains a document class with a COXResourceLibrary m_ResLib member (include OXResourceLibrary.h) and public arrays that will contain the types, names, and languages enumerated:

class CResLibDoc : public CDocument
{
protected: // create from serialization only

    CResLibDoc();
    DECLARE_DYNCREATE(CResLibDoc)

// Attributes

public:
    COXResourceLibrary m_ResLib;
    CStringArray m_sTypes;
    CStringArray m_sNames;
    CWordArray   m_nLangs;

In the OnOpenDocument handler, the selected file is opened and its resources are enumerated:

BOOL CResLibDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
    if (!CDocument::OnOpenDocument(lpszPathName))
        return FALSE;
    
    if (!m_ResLib.Open(lpszPathName))
        {
        AfxMessageBox(_T("Cannot open the specified file."));
        return FALSE;
        }

    if (!m_ResLib.EnumResources(&m_sTypes, &m_sNames, &m_nLangs))
        {
        AfxMessageBox(_T("Cannot enumerate resources."));
        return FALSE;
        }
    return TRUE;
}

Finally, in the views OnInitialUpdate, the arrays are iterated through in order to populate a tree control:

void CResLibView::OnInitialUpdate()
{
    CTreeView::OnInitialUpdate();
    CTreeCtrl& rTree = GetTreeCtrl();
    rTree.SetImageList(&m_ilFolder, TVSIL_NORMAL); 

    m_nClipFormat = ((CResFileApp*)AfxGetApp())->m_nClipFormat;
    m_pDoc = GetDocument();
    m_pResLib = &m_pDoc->m_ResLib;

    // calling IsModifiable() once instead of repeatedly checking it

    m_bModifiable = m_pResLib->IsModifiable();

    if (m_pResLib == NULL)
        return;

    m_hRootItem = rTree.InsertItem(m_pResLib->GetFileName() + 
        (m_bModifiable ? _T("") : _T(" <<<read />>>")));
    rTree.SetItemData(m_hRootItem, TVI_DATA_FOLDER);

    CString sType, sName, sPrevType;
    HTREEITEM hTypeItem = NULL;
    int iImage = 0;
    for (int i = 0; i < m_pDoc->m_sTypes.GetSize(); i++)            
    // iterate through the resource types enumerated in the document

    {
        sType = m_pDoc->m_sTypes[i];
        sName = m_pDoc->m_sNames[i];
        if (sType != sPrevType)
        {
            sPrevType = sType;
            GetTypeOutput(sType, iImage);

            hTypeItem = rTree.InsertItem(sType, m_hRootItem);
            rTree.SetItemData(hTypeItem, TVI_DATA_FOLDER);
        }

        GetNameOutput(sName, m_pDoc->m_nLangs[i]);

        ASSERT(hTypeItem);
        HTREEITEM hResItem = rTree.InsertItem(sName, iImage, iImage, 
            hTypeItem);
        rTree.SetItemData(hResItem, (DWORD)i);
    }

    rTree.Expand(m_hRootItem, TVE_EXPAND);
    rTree.SortChildren(m_hRootItem);
}

An extended overview and complete class references for these classes is available in the compiled HTML help documentation.

History

Initial CodeProject release August 2007.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

The Ultimate Toolbox
Web Developer
Canada Canada
In January 2005, David Cunningham and Chris Maunder created TheUltimateToolbox.com, a new group dedicated to the continued development, support and growth of Dundas Software’s award winning line of MFC, C++ and ActiveX control products.
 
Ultimate Grid for MFC, Ultimate Toolbox for MFC, and Ultimate TCP/IP have been stalwarts of C++/MFC development for a decade. Thousands of developers have used these products to speed their time to market, improve the quality of their finished products, and enhance the reliability and flexibility of their software.
Group type: Organisation

370 members


Comments and Discussions

 
Questionchange the text of Static ctrl on a dialog?? [modified] Pinmembercanercaner6-Dec-07 3:26 

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.

| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 25 Aug 2007
Article Copyright 2007 by The Ultimate Toolbox
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid