Creating an editor with a COM interface for incoming data.

  1. Setup the project.

    Go through the "MFC AppWizard(exe)". Call the project "OutputCOMEditor".
    Install the "Automation" option.
    Keep defaults "Multiple Documents" and "Document/View architecture support".
    Change the basic view class to "CEditView".
    Install other options on your preference.

  2. Implement the automation interface.

    Use automation menu of the Class Wizard to add COM visible member functions to the COutputCOMEditorDoc class .

    Add a member variable kaBasicClasses::CEditOutputStream COutputCOMEditorDoc::controlStream;

    Include the file CEditOutputStream.h to the OutputCOMEditorDoc.h

    The added functions should be implemented as follows
    void COutputCOMEditorDoc::ShowWindow();
    void COutputCOMEditorDoc::AddText(LPCTSTR text);
    void COutputCOMEditorDoc::EndLine();
    long COutputCOMEditorDoc::GetCursorPosition();
    long COutputCOMEditorDoc::GetMaximalCursorPosition();
    void COutputCOMEditorDoc::MoveCursorBy(long offset);
    void COutputCOMEditorDoc::SetCursorAtEnd();
    void COutputCOMEditorDoc::SetCursorAtPosition(long position);
    void COutputCOMEditorDoc::SetCursorAtStart();
    Note that the implementations are preceeded by using namespace kaBasicClasses; and using namespace std;

  3. Test the program (calling the COM object from Excel).

    Create an empty Excel spreadsheet. Program access to the functions ShowWindow and AddText from the spreadsheet using VBA's CreateObject function and information from the OutputCOMEditor.odl .
  4. Minor changes.

  5. Insure that the Editor only ask to save changes on closure if there are changes. Add the line
    SetModifiedFlag(FALSE);
    before the "return" statement in the function
    COutputCOMEditorDoc::OnNewDocument().
    Add the line
    SetModifiedFlag(TRUE);
    to the end of the functions void COutputCOMEditorDoc::AddText(LPCTSTR text). and void COutputCOMEditorDoc::EndLine();


  6. The child editor window looks better maximised within the main frame of the application.
    To make this happen edit the functions COutputCOMEditorDoc::ShowWindow() , COutputCOMEditorDoc::OnNewDocument() and include the directive
    #include "MainFrm.h"
    into the file EditWindowDoc.cpp.

Debugging

To debug both part of the program (C++ and VBA) go to Microsoft Visual C++/Project/Settings/Debug and enter "/Automation" in the "Program arguments" field. Then build and start the C++ program as always. With the C++ program running debug the VBA code. You will not be able to "step into" the VBA calls to the functions implemented in C++. However, all the breakpoints installed in the C++ source code will be operational.

Calling from an MFC application.

We will create a basic MFC application which activates, uses and deactivates COM objects implemented in the OutputCOMEditor.exe.
  1. Setup the project. Go through the "MFC AppWizard(exe)". Call the project "testingOutputCOMEditor".
    Make it "Dialog based".
    Install "Automation"!
    Install other options on your preference.
  2. Edit the dialog window. The result should look like this.
  3. Use "Properties" to set the following properties
    Component Property name Property value
    Small editor box in the position section ID IDC_POSITIONEDITORBOX
    Small editor box in the text section ID IDC_TEXTEDITORBOX
    Small editor box in the text section Styles->Multiline Checked
    Small editor box in the text section Styles->Want return Checked
     Small editor box in the text section Styles->Vertical Scroll Checked
    "Get" button ID IDC_GET
    "Set" button ID IDC_SET
    "Get max" button ID IDC_GETMAX
    "Move by" button ID IDC_MOVEBY
    "Set at end" button ID IDC_SETATEND
    "Set at start" button ID IDC_SETATSTART
    "Insert" button  ID IDC_INSERT
    "Load" button ID ID_LOAD
    "Unload" button ID ID_UNLOAD
    "OK" button ID ID_OKBUTTON
  4. Use Class Wizard to add the message handlers to the created resources as shown in the table.
    ID of the resource ID of the Event Name of the Handler
    ID_LOAD BN_CLICKED OnLoad
    ID_INSERT BN_CLICKED OnInsert
    ID_UNLOAD BN_CLICKED OnUnload
  5. Use Class Wizard to add a member variable
    CString m_text;
    to the "text controls" edit box.
  6. Generate an interface class IOutputCOMEditor for the OutputCOMEditor.
  7. Edit the functions
    void CTestingOutputCOMEditorDlg::OnLoad();
    void CTestingOutputCOMEditorDlg::OnUnload();
    void CTestingOutputCOMEditorDlg::OnInsert();
  8. Add definitions of a "class OutputCOMEditorStream: public std::ostream { ... };" to the files OutputCOMEditorStream.h and OutputCOMEditorStream.cpp .
  9. Add the variable kaBasicClasses::OutputCOMEditorStream* theEditor;
    to the definition of the dialog class CTestingOutputCOMEditorDlg. 
  10. Insert the statement class kaBasicClasses::OutputCOMEditorStream; before definition of the class CTestingOutputCOMEditorDlg and include the OutputCOMEditorStream.h header file to the cpp file.
  11. Add initialization theEditor=NULL in the constructor and the clean up if( theEditor!=NULL ) delete theEditor; in the destructor.
  12. Attach OutputStream.cpp, OutputStream.h, EditorOutputStream.h to the project.
  13. Add the variable int m_position to the editor box in the cursor control section.
  14. Add the message handlers to the rest of the buttons and provide the following implementation.
  15. Check how it works.
1
Hosted by www.Geocities.ws