RTF-Edit



Homepage: CRulerRichEditCtrl - a complete RTF mini-editor

RTFEditHwnd := DllCall("AHKCtrlSupport\CreateRTFEditID", DWORD, dwexStyle, DWORD, Edit2HWnd, int, nScroll, "Cdecl Int")
Creates a RTF edit control
RTFEditHwnd Handle to the new control
Style See AHK documentation for edit control styles
Edit2HWnd Handle to an existing edit control. This one gets replaced by the RTF edit control
nScroll 0: no horizontal scrolling
1: horizontal scrolling



nRC := DllCall("AHKCtrlSupport\SetRTFEdit", DWORD, RTFEditHwnd, int, nMode, int, fToolbar, int, fRuler, "Cdecl Int")
Sets the mode for the ruler. Show/Hide the toolbar / ruler
nRC Error: 0
ok: 1
RTFEditHwnd Handle to the RTF edit control.
nMode 0: Ruler is in inches - 1: Ruler is in metrics
fToolbar 0: Hide Toolbar - 1: Show Toolbar
fRiler 0: Hide Ruler - 1: Show Ruler



nRC := DllCall("AHKCtrlSupport\RTFEditLoad", DWORD, RTFEditHwnd, str, RTFFileName, int, 254, "Cdecl Int")
Loads a RTF-file.
nRC Error: 0
ok: 1
RTFEditHwnd Handle to the RTF edit control.
RTFFileName Filename to open. If you specify an empty string, a file open dialog is displayed
int 254 Length of the string. Use i.e. VarSetCapacity(xxx,200) to reserve enough space



nRC := DllCall("AHKCtrlSupport\RTFEditSave", DWORD, RTFEditHwnd, str, RTFFileName, int, 254, "Cdecl Int")
Saves a RTF-file.
nRC Error: 0
ok: 1
RTFEditHwnd Handle to the RTF edit control.
RTFFileName Filename to save. If you specify an empty string, a file save dialog is displayed
int 254 Length of the string. Use i.e. VarSetCapacity(xxx,200) to reserve enough space



nRC := DllCall("AHKCtrlSupport\GetRTFEditText", DWORD, RTFEditHwnd, str, szText, int, nSize, "Cdecl Int")
Gets the RTF-contents of the control
nRC Error: 0
ok: 1
If called with nSize = 0 you retrieve the length of the text
RTFEditHwnd Handle to the RTF edit control.
szText Variable to retrieve the RTF-content
int nSize You must reserve enough space to retrieve the full text. Use VarSetCapacity(szText,5000)

It's hard to guess the correct length. So you better call this function with "0" as nSize. Then you can retrieve the needed length in the nRC return value. See the example below



nRC := DllCall("AHKCtrlSupport\SetRTFKey", int, RTFEditHwnd, int, nModifier, int, nKey, int, nFunction, int, nparameter, "Cdecl Int")
Set Hotkey and function to call, when hotkey was pressed
nRC Error: 0
ok: 1
If called with nSize = 0 you retrieve the length of the text
RTFEditHwnd Handle to the RTF edit control.
nModifier 0= no modifier, 1=ALT-key pressed
nKey You can retrieve the key with that code:

OnMessage(0x0102, "CHARMsg")
OnMessage(0x0106, "CHARMsg")

CHARMsg(wp, lp, msg) { ToolTip, Key arrived. Use for SetRTFKey: lp:%lp% - wp:%wp% } return
nFunction
  1. eBold = 0
  2. eItalic = 1
  3. eFont = 2
  4. eUnderline = 3
  5. eLeftAlign = 4
  6. eCenterAlign = 5
  7. eRightAlign = 6
  8. eIndent = 7
  9. eOutdent = 8
  10. eBullet = 9
  11. eFind = 10
  12. eToolbarShowHideToolbar = 100
  13. eToolbarShowHideRuler = 101
nParameter Depends on nFunction
eFind: 0:Find, 1:Replace
eToolbarShowHideToolbar: 1:Show Toolbar, 0:Hide Toolbar, 2:Toggle Toolbar
eToolbarShowHideRuler: 1:Show Ruler, 0:Hide Ruler, 2:Toggle Ruler



nRC := DllCall("AHKCtrlSupport\GetRTFLine", int, RTFEditHwnd, "int *", LineCnt, "str", szLine, int, szLength, "Cdecl Int")
Retrieves how many lines of text are in the control and retrieves one line
nRC Error: 0
ok: 1
If called with nSize = 0 you retrieve the length of the text
RTFEditHwnd Handle to the RTF edit control.
LineCnt Retrieves the count of lines if szLength == 0
Retrieves the amount of character copied into the buffer
szLine Buffer. Line is copied into this var. Use i.e. VarSetCapacity(xxx,2000) to reserve enough space



nRC := DllCall("AHKCtrlSupport\RTFSelection", int, RTFEditHwnd, int, nFunc, "int *", nFrom, "int *", nTo, int, nSize, "str", szLine, "Cdecl Int")
Various selection manupulation
nRC Error: 0
ok: 1
If called with nSize = 0 you retrieve the length of the text
RTFEditHwnd Handle to the RTF edit control.
nFunc
  • 0: GetSel. Param3 (out): Zero-based index of the first character in the current selection
    Param4 (out): Zero-based index of the last character in the current selection
  • 1: GetSelectionType: Param3 (out): SEL_EMPTY:0x0000 / SEL_TEXT:0x0001 / SEL_OBJECT:0x0002 /SEL_MULTICHAR:0x0004 /SEL_MULTIOBJECT:0x0008
  • 2: GetSelectionText: Param3 (out): The number of characters copied into buffer
    Param6 (out): Selected Text
  • 3: GetTextRange: Param3 (out): The character position index immediately preceding the first character in the range
    Param4 (out): The character position immediately following the last character in the range
    Param5 (in): Reserved length of the string (Param4) Param6 (out): String
  • 4: ReplaceSel: Param6 (in): String to replace with selection
  • 5: SetSel: Param3 (in): Zero-based index of the first character for the selection
    Param4 (in): Zero-based index of the last character for the selection
  • 6: SetFontName: Param6 (in): String: fontname
  • 7: SetFontSize: Param3 (in): Size of the font
  • 8: SetFontColor: Param3 (in): BGR-Color of text i.e 0x0000ff for red text
  • 9: SetHilightCol:Param3 (in): BGR-Color of Hilighttext i.e 0xff0000 for blue text
  • 10:SetBkGrndCol: Param3 (in): BGR-Color of RTF-control background



Example:
; --------------- Create a RTF Edit ----------------------------------
; -----------------------------------------------------------------------
dwexStyle := 0x0
RTFEditHwnd := DllCall("AHKCtrlSupport\CreateRTFEditID", DWORD, dwexStyle, DWORD, Edit2HWnd, "Cdecl Int")
if (errorlevel <> 0) || (RTFEditHwnd = 0)
{
	MsgBox error while calling CreateRTFEditID Errorlevel: %errorlevel% - RC: %RTFEditHwnd%
	return
}
nMode = 0 ; 0 = inch, 1 = metric
fToolbar = 1
fRuler = 1
nRC := DllCall("AHKCtrlSupport\SetRTFEdit", DWORD, RTFEditHwnd, int, nMode, int, fToolbar, int, fRuler, "Cdecl Int")
if (errorlevel <> 0) || (nRC = 0)
{
	MsgBox error while calling SetRTFEdit Errorlevel: %errorlevel% - RC: %RTFEditHwnd%
	return
}
return
LoadRTF:
VarSetCapacity(RTFFileName, 255)
nRC := DllCall("AHKCtrlSupport\RTFEditLoad", DWORD, RTFEditHwnd, str, RTFFileName, int, 254, "Cdecl Int")
if (errorlevel <> 0) || (nRC = 0)
{
	MsgBox error while calling RTFEditLoad Errorlevel: %errorlevel% - RC: %RTFEditHwnd%
	return
}
return

SaveRTF:
VarSetCapacity(RTFFileName, 255)
nRC := DllCall("AHKCtrlSupport\RTFEditSave", DWORD, RTFEditHwnd, str, RTFFileName, int, 254, "Cdecl Int")
if (errorlevel <> 0) || (nRC = 0)
{
	MsgBox error while calling RTFEditLoad Errorlevel: %errorlevel% - RC: %RTFEditHwnd%
	return
}
return

GetRTF:
;Get the size of the String by setting nSize to 0
nSize = 0
nRC := DllCall("AHKCtrlSupport\GetRTFEditText", DWORD, RTFEditHwnd, str, szText, int, nSize, "Cdecl Int")
;nRC is the length of the text
nRC += 2 ;string terminates with zero. Include this in size
VarSetCapacity(szText ,nRC)
nSize := nRC
;ToolTip, %nSize%
nRC := DllCall("AHKCtrlSupport\GetRTFEditText", DWORD, RTFEditHwnd, str, szText, int, nSize, "Cdecl Int")
if (errorlevel <> 0) || (nRC = 0)
{
	MsgBox error while calling GetRTFEditText Errorlevel: %errorlevel% - RC: %RTFEditHwnd%
	return
}
MsgBox %szText%
return