XLL+ Class Library (6.3)

ExUIPopupMFC Sample

Demonstrates a popup editor within the Excel Formula Wizard (using MFC)

Overview

This project demonstrates how to show a popup editor for an argument in the Excel Formula Wizard, using the Microsoft Foundation Classes (MFC). It contains a single worksheet function, SimulateLife.

A class CFileNamePopupProvider is defined, which is derived from CXlWizExUIPopupProviderBase, and implements the virtual function Edit.

CFileNamePopupProvider

CFileNamePopupProvider::Edit does the 3 steps required of an editor.

  1. Creates a dialog (in this case by initializing a CFileDialog object) and initializes the values in the dialog (in this case, by setting the file name to be the value of strValue passed to Edit).
  2. Shows the dialog.
  3. If the user selects a file, sets strValue and returns true. The file name is surrounded by quotes, so that it is a legal Excel string value. If the user presses Cancel, then Edit returns false.

Note that CFileNamePopupProvider contains a data member, m_strTitle, which will be displayed in the title of the file dialog. This member can be set to be different for each of the arguments for which this editor is provided.

CopyC++
virtual bool Edit(CString& strValue, HWND hwndParent)
{
    // Fix the scope of the MFC DLL
    XLL_FIX_STATE;

    // Clean up strValue
    CString strFileName;
    // ... Omitted for brevity 
 
    // Create an MFC wrapper for the parent window
    CWndAdapter wndParent(hwndParent); 

    // Set up a file dialog
    CFileDialog fd(FALSE, 0, (LPCTSTR)strFileName, 
        OFN_HIDEREADONLY | OFN_PATHMUSTEXIST,
        _T("Text files (*.txt)|*.txt|All files|*.*||"),
        &wndParent);

    // Show file dialog 
    if (fd.DoModal() == IDOK)
    {
        // Copy user's selection to output, with quotes
        strValue.Format(_T("\"%s\""), fd.GetPathName());
        return true;
    }
    else 
        return false;
}

Attaching to the HWND

A helper class CWndAdapter is provided, which creates a CWnd object for the supplied HWND hwndParent, but does not destroy the HWND when it goes out of scope (as the standard CWnd constructor would do).

Registering the popup provider

To register the popup editor, an instance of CXlWizExUIArgumentPopupCreator is declared at global scope. The arguments to the constructor include (i) the name of the add-in function for which the editor is being registered, (ii) the index of the argument to which it applies and (iii) an instance of the popup provider, CFileNamePopupProvider.

Note that a specialized title is provided in the constructor of CFileNamePopupProvider, which applies only to the popup editor for this specific argument (DataFile in SimulateLife). In this case the title is "Select a data file".

Note that the developer is responsible for constructing the instance of CFileNamePopupProvider, but the framework is responsible for destroying it. For this reason, if a destructor is provided for a popup provider class, it must be declared as virtual.

Classes and functions used

CXlWizExUIPopupProviderBase | CXlWizExUIPopupProviderBase::Edit | CXlWizExUIArgumentPopupCreator | CXlWizExUIArgumentPopupCreator::CXlWizExUIArgumentPopupCreator

Sample project

Each sample project is located in a sub-directory of the Samples directory of the XLL+ installation. To use the sample project, open the solution file ExUIPopupMFC.sln or the project file ExUIPopupMFC.vcproj.

You can enable debugging under Excel by using the Setup Debugging command in the XLL+ ToolWindow.

When delivered, the help files are excluded from the build. You can enable the help build by selecting the files ExUIPopupMFC.help.xml and ExUIPopupMFC.chm in the Solution Explorer, and using the right-click menu to view Properties. Select the page "Configuration Properties/General" and set the "Excluded from build" property to "No". See Generating help in the User Guide for more information.

See Also

List of Sample Projects | Popup Editors (User Guide) | ExUIPopup sample | ExUIPopupClr sample