
cRichEdit - Standard RichEdit control for AutoHotkey scripts

[*:3cyau59t]fixed hInstance usage [thanks PhiLho]
[*:3cyau59t]fixed RICHEDIT was hard coded [thanks PhiLho]
[*:3cyau59t]added FreeDlls option (_action param) to cGUI() to free all dlls loaded by the cGUI() function
[*:3cyau59t]updated demo to load RichEdit v2.0
[*:3cyau59t]modifed global variables used
[*:3cyau59t]added win95_98.ahk demo to the zip file available for download**Note to Win95/98 users: If the control does not display when running the demo script then change RichEdit20A to RICHEDIT in the cGUI function in the demo script or download and install riched20.dll

Yeah-- the two that I mentioned alone would immiediately unlock a number of them. I look forward to seeing your examples (i'll learn quite a bit off of them!!As there are a few structs that are commonly used when modifying RichEdit controls, I'll likely add a function to make using a few of the common ones a bit easier


... plus a few other updates. Please see the first post for details, screenshots, downloads, etc...*****************************************
EXGETSEL - retrieves the starting and ending character positions of the selection
_action = EXGETSEL
*****************************************
EXSETSEL - set the current selection
_action = EXSETSEL
opt1 = start position
opt2 = end position
*****************************************
GETSELTEXT - get the current text that is selected in the control
_action = GETSELTEXT
*****************************************
GETTEXTLENGTH - get the number of characters in the control
_action = GETTEXTLENGTH
opt1 = (1 - unicode, 0 - ANSI) Default ANSI
*****************************************
GETTEXT - get the text in the control
_action = GETTEXT
opt1 = (1 - unicode, 0 - ANSI) Default ANSI
*****************************************
BackColor - change the background colour of the RichEdit control
_action = BackColor
opt1 = RRGGBB colour
*****************************************
SETSCROLLPOS - scroll to a specific position in the control
_action = SETSCROLLPOS
opt1 = x position
opt2 = y position
*****************************************
GETSCROLLPOS - get the current scroll position in the control
_action = SETSCROLLPOS
*****************************************
AutoURLDetect - automatic detection of URLs
_action = AutoURLDetect
opt1 = (1 = on, 0 = off)
*****************************************


HE_SetFont(hEdit, pFont="") { local height, weight, italic, underline, strikeout , nCharSet local hFont static WM_SETFONT := 0x30 ;parse font italic := InStr(pFont, "italic") ? 1 : 0 underline := InStr(pFont, "underline") ? 1 : 0 strikeout := InStr(pFont, "strikeout") ? 1 : 0 weight := InStr(pFont, "bold") ? 700 : 400 ;height RegExMatch(pFont, "(?<=[S|s])(\d{1,2})(?=[ ,])", height) if (height = "") height := 10 RegRead, LogPixels, HKEY_LOCAL_MACHINE, SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontDPI, LogPixels height := -DllCall("MulDiv", "int", Height, "int", LogPixels, "int", 72) ;face RegExMatch(pFont, "(?<=,).+", fontFace) if (fontFace != "") fontFace := RegExReplace( fontFace, "(^\s*)|(\s*$)") ;trim else fontFace := "MS Sans Serif" ;create font hFont := DllCall("CreateFont", "int", height, "int", 0, "int", 0, "int", 0 ,"int", weight, "Uint", italic, "Uint", underline ,"uint", strikeOut, "Uint", nCharSet, "Uint", 0, "Uint", 0, "Uint", 0, "Uint", 0, "str", fontFace) [color=red] SendMessage,WM_SETFONT,hFont,TRUE,,ahk_id %hEdit%[/color] return ErrorLevel }
THe usage:
fStyle := "s12 bold" , fFace := "Courier New" HE_SetFont( hEdit, fStyle "," fFace)

Thanks for the code contributionThis is the function you might want to use for font change. I created it for the HiEdit. Only the last line is related to HiEdit, so you can use it for richedit, perhaps even unchanged.

SendMessage(hwnd, EM_SETCHARFORMAT, SCF_SELECTION, &CHARFORMATstruct)

After wrestling with EM_GETCHARFORMAT and the CHARRANGE struct for a little while, I managed to get a Bold command working though so I'll try and post an update that includes a few basic text formatting features a bit later. Here's the current code for the Bold _action in case anyone wants to experiment, play, provide feedback, etc...

/* ***************************************************************************************** Bold - make the current selection Bold or add Bold text from the current location - this will act as a toggle if appplied to the same selection again _action = Bold ***************************************************************************************** */ Else If _action = Bold { VarSetCapacity(_tmp1, 60, 0) ; CHARFORMAT struct InsertInteger_cGUI(60, _tmp1, 0) ; cbSize InsertInteger_cGUI(1, _tmp1, 4) ; dwMask InsertInteger_cGUI(1, _tmp1, 8) ; dwEffects ; ** SendMessage(hwnd, EM_GETCHARFORMAT, SCF_SELECTION, &CHARFORMATstruct) DllCall("SendMessage", "UInt", _ctrlID, "UInt", 0x43A, "UInt", "1", "UInt", &_tmp1) ; ** Check if Bold is applied to the current selection VarSetCapacity(_tmp1, -1) _tmp2 := !((ExtractInteger_cGUI(_tmp1, 4) & 1) & (ExtractInteger_cGUI(_tmp1, 8) & 1)) ; ** SendMessage(hwnd, EM_SETCHARFORMAT, SCF_SELECTION, &CHARFORMATstruct) ; reverse the current effect VarSetCapacity(_tmp1, 60, Chr(0)) ; CHARFORMAT struct InsertInteger_cGUI(60, _tmp1, 0) ; cbSize InsertInteger_cGUI(1, _tmp1, 4) ; dwMask InsertInteger_cGUI(_tmp2, _tmp1, 8) ; dwEffects Return DllCall("SendMessage", "UInt", _ctrlID, "UInt", 0x444, "UInt", "1", "UInt", &_tmp1) }

Wouldn't one of the advantages of having cGUI_RICHEDIT handle RE wrapping be that it sort of encapsilates everything. The results of your struct functions should only have to be availiable to it..? Right? :?: :roll:Well, I came up with a few functions for working with a few specific structs but I decided against adding them due to the overhead involved in trying to avoid creating global variables
I still haven't digested everything yet.. but I appreciate all your work so far!!


No. Its richedit specific. Thats why I said you can reuse the function above. everything is calculated you just have to fill up the structure.Out of curiousity, have you tried this instead?


It's slowly starting to look and behave a bit more like a RichEdit control (lots left to go though)... Updated to version 0.04 Beta[*:3ge9hius]added: FontStyle _action to set/toggle Bold, Italic, UnderLine, Strikethrough for the current selection or current position (optional custom dwMask and dwEffects settings available also)
[*:3ge9hius]added: FontColor _action to change the colour to selected text or change the colour at the current location (RRGGBB value or by name - same names/values as Object Colors in the AutoHotkey documentation)
[*:3ge9hius]updated: BackColor _action to support colour names also
[*:3ge9hius]updated: feature_demo.ahk script to test some of the newer features that have been addedComing soon... change font, font size, GetTextRange...

Yeah, I should definitely put this as separate func for ppl that need explicit font handles.The CreateFont code will likely come in handy though .
Thx for the update. This is approaching to real RichEdit.

One question (out of curiosity

I guess cGUI is name for something bigger you want to have.
Is there any chance you provide more familiar include, something more appealing like, RichEdit_Create, RichEdit_SetSelectionEffect, etc...
Or even better, wait until we devise some standardisation of syntax for 3th party controls wrappers, but some things are already in most of the control wrappers like XXX_Create() to add control to the gui.


autohotkey.com/net Site Manager
Contact me by email (polyethene at autohotkey.net) or message tidbit
ThanksGreat work! A feature that I would find very useful is custom word-breaks and delimiter chars. This can be done with EditWordBreakProc (requires callback).

I'll be making a few structural changes with the next version but I'm not currently planning to switch to using separate functions for commands unless there's a really good reason.Overloads are confusing, I think you should switch to Chris' style of function names, i.e. RE_Add(), RE_SetText(), RE_LoadFile(), RE_Destroy() etc.
I am planning to split things up a bit and add separate help documention though which will likely make it easier to locate commands, descriptions on usage, param values expected, syntax, examples, etc...

Yes. I was thinking the same thing. I'll probably split the #Include file into 2 separate files and rename the cGUI_RICHEDIT() function to cRichEdit() instead.One question (out of curiosity
)
I guess cGUI is name for something bigger you want to have.
Is there any chance you provide more familiar include, something more appealing like, RichEdit_Create, RichEdit_SetSelectionEffect, etc...
That was/is the thought behind cGUI(). I'm planning to use it for other controls also. Although it needs a few additional features, it can currently be used to add other controls to a script. After a bit more testing with it I'll likely move it to another topic...Or even better, wait until we devise some standardisation of syntax for 3th party controls wrappers, but some things are already in most of the control wrappers like XXX_Create() to add control to the gui.

[*:2enbvpgv]modified: split the include files into cGUI.ahk and cRichEdit.ahk since cGUI.ahk might be used for other controls in the future.
[*:2enbvpgv]renamed: cGUI_RICHEDIT function to c_RichEdit() instead (easier to remember and less to type)
[*:2enbvpgv]modified: feature_demo.ahk to use a ListView to list/test commands instead of the buttons that were used previously. This method seems to allow more flexibility when adding/modifying/testing new options
[*:2enbvpgv]added: FontName _action to specify a font to use by name
[*:2enbvpgv]added: GetFontSize _action to get the font size of the current selection
[*:2enbvpgv]added: SetFontSize _action to set the font size of the current selection
