viksoe.dk

RTF Script Editor control

RTF Script Editor control


This article was submitted .


This control is derived from an old MFC control by Juraj Rojko. It's a syntax highlighting edit control. I have fixed a few problems and enhanced it with a more Visual Basic-like editing scheme.

RTF controls will always suffer from a few short-comings when it comes to providing a really good syntax highlighting editor. Formatting speed, for instance, can be a problem with large files. But Juraj Rojko initially did a good job trying to minimize the number of display updates, so as a quick'n'dirty editor, this is a fine control.

Anyway, if you're looking for a real professional editor control, why not take a look at the Scintilla project (see link below). It has it all!

To use this control, place a RTF control on your dialog (RTF version 1 will do).
Add a member variable to your dialog implementation file...

CRtfScriptEditorCtrl m_ctlEditor
In the OnInitDialog() event handler, add the following line:
  LRESULT OnInitDialog(UINT /*uMsg*/, 
                       WPARAM /*wParam*/, 
                       LPARAM /*lParam*/, 
                       BOOL& /*bHandled*/)
  {
    ...
    m_ctlEditor.SubclassWindow(GetDlgItem(IDC_EDIT1));
    ...
  }
Add the following reflection macro to your dialog's message map:
  BEGIN_MSG_MAP(CMainDlg)
    ...
    REFLECT_NOTIFICATIONS()
  END_MSG_MAP()
Then to initialize the control...
    m_ctlEditor.ClearKeywords();
    m_ctlEditor.AddKeyword("For");
    m_ctlEditor.AddKeyword("To");
    m_ctlEditor.AddKeyword("Next");
    m_ctlEditor.AddKeyword("If");
    m_ctlEditor.AddKeyword("Then");
    m_ctlEditor.AddKeyword("Else");

m_ctlEditor.ClearConstants(); m_ctlEditor.AddConstant("True"); m_ctlEditor.AddConstant("False");
m_ctlEditor.SetCaseSensitive(FALSE); m_ctlEditor.SetChangeCase(TRUE); m_ctlEditor.SetSyntax(SC_COMMENT, "'");
SYNTAXCOLOR ic = { 0 }; ic.clrText = RGB(0,0,240); m_ctlEditor.SetSyntaxColor(SC_KEYWORD, ic); ic.clrText = RGB(0,100,0); m_ctlEditor.SetSyntaxColor(SC_CONSTANT, ic);
And do remember to initialise the RTF library properly in your application startup code.
  int WINAPI _tWinMain(HINSTANCE hInstance, HIN...)
  {
    ...
    ::InitCommonControls();
    HINSTANCE hInstRich = 
         ::LoadLibrary(CRichEditCtrl::GetLibraryName());
    ...
    int nRet = Run(lpstrCmdLine, nCmdShow);
    ...
    ::FreeLibrary(hInstRich);
    ...
    return 0;
  }

Source Code Dependencies

Microsoft Visual C++ 6.0
Microsoft WTL 3.1 Library

Useful Links

Scintilla - A free professional editor control

Download Files

DownloadSource Code (5 Kb)

To the top