Jump to content
Sign in to follow this  
zelles

Syntax Highlighting Rich Edit

Recommended Posts

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  

  • Similar Content

    • By AutoPM
      For Example I'm Reading Clipboard Which is having a Link Copied From any webpage.
      Copied Link Will Be In Rich Text, How to Convert This link to Plain Text
       
      Rich Text : https://www.autoitscript.com
      Plain Text : https://www.autoitscript.com
    • By JNutt
      I see $hWnd used as a local variable in a lot of script examples.  I know the 'h' is used for handles, but what is the 'Wnd' short for?
      Thanks!
    • By loganizzi
      If you look at the help file for the above command, you'll see that it states that it only works for certain zoom values (100 and 200 to 6400).   I assume that this is due to an issue in an MSDN library that the command relies on.
      After doing some investigating, I noticed that the GUIRichEdit.au3 library file contains the following code:
      ; #FUNCTION# ==================================================================================================================== ; Authors........: Chris Haslam (c.haslam) ; Modified ......: ; =============================================================================================================================== Func _GUICtrlRichEdit_SetZoom($hWnd, $iPercent) If Not _WinAPI_IsClassName($hWnd, $__g_sRTFClassName) Then Return SetError(101, 0, False) If Not __GCR_IsNumeric($iPercent, ">0") Then Return SetError(1021, 0, False) Local $iNumerator, $iDenominator Select Case Not ($iPercent = 100 Or ($iPercent >= 200 And $iPercent < 6400)) Return SetError(1022, 0, False) Case $iPercent >= 100 $iNumerator = 10000 $iDenominator = 10000 / ($iPercent / 100) Case Else $iNumerator = 10000 * ($iPercent / 100) $iDenominator = 10000 EndSelect Return _SendMessage($hWnd, $EM_SETZOOM, $iNumerator, $iDenominator) <> 0 EndFunc ;==>_GUICtrlRichEdit_SetZoom Which ensures that values are only within the bounds specified in the help file.    I found that if I comment out the portion of the code that limits the values, the function works fine for most (if not all) values.   I changed the code in the library file to this by simply commenting out the restricting lines of code:
      ; #FUNCTION# ==================================================================================================================== ; Authors........: Chris Haslam (c.haslam) ; Modified ......: ; =============================================================================================================================== Func _GUICtrlRichEdit_SetZoom($hWnd, $iPercent) If Not _WinAPI_IsClassName($hWnd, $__g_sRTFClassName) Then Return SetError(101, 0, False) If Not __GCR_IsNumeric($iPercent, ">0") Then Return SetError(1021, 0, False) Local $iNumerator, $iDenominator Select ; Case Not ($iPercent = 100 Or ($iPercent >= 200 And $iPercent < 6400)) ; Return SetError(1022, 0, False) Case $iPercent >= 100 $iNumerator = 10000 $iDenominator = 10000 / ($iPercent / 100) Case Else $iNumerator = 10000 * ($iPercent / 100) $iDenominator = 10000 EndSelect Return _SendMessage($hWnd, $EM_SETZOOM, $iNumerator, $iDenominator) <> 0 EndFunc ;==>_GUICtrlRichEdit_SetZoom  
      I assume whatever windows bug existed at the time this was created, no longer exists so there is no need to restrict the values.   
      Just wanted to point this out if anyone else is using this part of the library and required this functionality like I did.
       
    • By AndreyS
      Tell me, please, how to set / change the cursor over RichEdit (created with _GUICtrlRichEdit_Create)?
    • 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.
×
×
  • Create New...