Sign in to follow this  
Followers 0
zelles

Syntax Highlighting Rich Edit

1 post in this topic

This is a basic editor that uses IE functions and jasvscript/css to control the display and highlight terms. It sets the html docuement in to edit mode, to be used as the edit box, and the javascript scans and runs the highlighter onkeyup.

The example is set to highlight "If", "Then" and "EndIf". If you were to type "endif", it would automatically change it to "EndIf" and highlight it.

Example Source Code:

#NoTrayIcon
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <IE.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)
Opt("GUIResizeMode", $GUI_DOCKAUTO)
Opt("TrayMenuMode", 3)

Local $html = '', $r = @CRLF
$html &= '<html>'&$r
$html &= '<head>'&$r
$html &= '   <script type="text/javascript">'&$r
$html &= '      function HighlightSyntax(word,highlightcolor) {'&$r
$html &= '         var replacement = "<span class=''color-" + highlightcolor + "''>" + word + "</span>";'
$html &= '         var re = new RegExp(word, "ig");'
$html &= '         document.body.innerHTML = document.body.innerHTML.replace(re, replacement);'
$html &= '         '&$r
$html &= '      }'&$r
$html &= '      function SyntaxHighlighter() {'&$r
$html &= '         HighlightSyntax("EndIf","blue");'&$r
$html &= '         HighlightSyntax("If","blue");'&$r
$html &= '         HighlightSyntax("Then","blue");'&$r
$html &= '      }'&$r
$html &= '   </script>'&$r
$html &= '   <style type="text/css">'&$r
$html &= '      html, body { width: 100%; height: 100%; background: #FFFFFF; color: #000000; border: 0px none; font-family: times; font-size: 14px; padding: 0px; margin: 0px; }'&$r
$html &= '      p { padding: 0px; margin: 0px; }'&$r
$html &= '      .color-blue { color: #0000FF; }'&$r
$html &= '   </style>'&$r
$html &= '</head>'&$r
$html &= '<body onkeyup="SyntaxHighlighter()" contenteditable="true" designMode="on">'&$r
$html &= '</body>'&$r
$html &= '</html>'

Global $UI_FORM = GUICreate("Syntax Highlighting, Resizable Rich Text Editor", 601, 371, 192, 124, BitOR($GUI_SS_DEFAULT_GUI,$WS_MAXIMIZEBOX,$WS_CLIPCHILDREN,$WS_TABSTOP,$WS_SIZEBOX), BitOR($WS_EX_TRANSPARENT,$WS_EX_WINDOWEDGE))
Local $UI_MENU_File = GUICtrlCreateMenu("&File")
Global $UI_MENU_Exit = GUICtrlCreateMenuItem("&Exit", $UI_MENU_File)
Global $UI_IE = _IECreateEmbedded()
Global $UI_IEOject = GUICtrlCreateObj($UI_IE, 0, 0, 600, 350)
GUICtrlSetOnEvent($UI_MENU_Exit, "GUI_Close")
GUISetOnEvent($GUI_EVENT_CLOSE, "GUI_Close", $UI_FORM)
GUICtrlSetResizing($UI_IEOject, $GUI_DOCKAUTO)
GUISetState(@SW_SHOW, $UI_FORM)

_IENavigate($UI_IE, "about:blank")
_IEDocWriteHTML($UI_IE, $html)

While 1
    Sleep(10)
WEnd

Func GUI_Close()
    GUIDelete($UI_FORM)
    Exit
EndFunc

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • StMaSi
      By StMaSi
      Two questions...
      When presenting a form containing a richedit control, is there any way to make the displayed text, within said richedit control, non-selectable?
      In addition, is there any way to hide the vertical line cursor, within said richedit control?
      Thanx.
    • TheSaint
      By TheSaint
      I've finally come up with some simple solutions for dragging & dropping a URL onto a control.
      There have been many requests here for this over the years.
      I do have an issue though, with my second code example solution as is mentioned here.
      The issue relates to working in Win 7 (and no doubt newer Windows).
      As I state there, failure is no doubt due to 'MSCOMCT2.OCX' missing from the System32 folder, at the very least.
      Not sure if it would be wise to just dump 'MSCOMCT2.OCX' from WinXP into the Win 7 System32 folder. I expect not.
      I imagine there is a better solution available in Win 7. Maybe a different OCX file, and perhaps a different Class of object etc.
      Any help would be greatly appreciated.
      Code is here (second example).
      EDIT
      Okay, I have rediscovered that 'MSCOMCT2.OCX' is a VB6 ActiveX control. So while I could dig up a copy and register it, that would probably not be helpful to everyone.
      And registering it didn't help. Possibly other dependencies are also needed?
      When running the script, it is clear going by the error reported, that Object is not being created.
    • AndreyS
      By AndreyS
      Please tell me, who knows how to apply and cancel styles in RTF?
      The following code does not work:
      #include <GuiRichEdit.au3> #include <WindowsConstants.au3> $hGUI = GUICreate("RichEdit Style", 500, 500, -1, -1) $hRichEdit = _GUICtrlRichEdit_Create($hGui, "Test", 20,40,460,400) GUISetState(@SW_SHOW, $hGUI) Sleep(3000) GUICtrlSetStyle($hRichEdit, $ES_RIGHT) Sleep(3000) GUICtrlSetStyle($hRichEdit, -1) Sleep(3000) Thanks!
    • kashamalasha
      By kashamalasha
      Hi everybody.
      Could you tell me is there any UDF that can help to highlight syntax in the _GUICtrlRichEdit element on the fly?
      Is it possible to implement this feature in script in simple way using some UDF or something, instead of writing thousand of strings manually?
      Thanks in advance.