Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

cRichEdit - Standard RichEdit control for AutoHotkey scripts


  • Please log in to reply
110 replies to this topic
corrupt
  • Members
  • 2558 posts
  • Last active: Nov 01 2014 03:23 PM
  • Joined: 29 Dec 2004
It was something simple I was missing with loading a RichEdit 2.0 control :oops: .

corrupt
  • Members
  • 2558 posts
  • Last active: Nov 01 2014 03:23 PM
  • Joined: 29 Dec 2004
Updated to version 0.02 Beta.
[*: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

Daniel2
  • Guests
  • Last active:
  • Joined: --

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

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!! :))

corrupt
  • Members
  • 2558 posts
  • Last active: Nov 01 2014 03:23 PM
  • Joined: 29 Dec 2004
Updated to version 0.03 Beta. Added:

*****************************************
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)
*****************************************

... plus a few other updates. Please see the first post for details, screenshots, downloads, etc... :) .

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
This 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.

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)

Posted Image

corrupt
  • Members
  • 2558 posts
  • Last active: Nov 01 2014 03:23 PM
  • Joined: 29 Dec 2004

This 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.

Thanks for the code contribution :) . Out of curiousity, have you tried this instead?
SendMessage(hwnd, EM_SETCHARFORMAT, SCF_SELECTION, &CHARFORMATstruct)


corrupt
  • Members
  • 2558 posts
  • Last active: Nov 01 2014 03:23 PM
  • Joined: 29 Dec 2004
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 may have a closer look sometime later (or possibly sooner if anyone has any code suggestions/submissions).

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)
  }


Daniel2
  • Guests
  • Last active:
  • Joined: --
Cool!

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

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:
I still haven't digested everything yet.. but I appreciate all your work so far!! :D

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006

Out of curiousity, have you tried this instead?

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.
Posted Image

corrupt
  • Members
  • 2558 posts
  • Last active: Nov 01 2014 03:23 PM
  • Joined: 29 Dec 2004
Since RichEdit has built-in methods for playing with fonts I decided to use them instead of using CreateFont for now. The CreateFont code will likely come in handy though :) .

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...

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006

The CreateFont code will likely come in handy though .

Yeah, I should definitely put this as separate func for ppl that need explicit font handles.

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.
Posted Image

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
Great work! A feature that I would find very useful is custom word-breaks and delimiter chars. This can be done with EditWordBreakProc (requires callback). 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.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


corrupt
  • Members
  • 2558 posts
  • Last active: Nov 01 2014 03:23 PM
  • Joined: 29 Dec 2004

Great work! A feature that I would find very useful is custom word-breaks and delimiter chars. This can be done with EditWordBreakProc (requires callback).

Thanks :) . Custom word-breaks and delimiter chars are on the list but it might take me a little while longer to get to them. Thanks for the link. Here'sa related link specific to the RichEdit control.

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'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.

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...

corrupt
  • Members
  • 2558 posts
  • Last active: Nov 01 2014 03:23 PM
  • Joined: 29 Dec 2004

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...

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.

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.

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...

corrupt
  • Members
  • 2558 posts
  • Last active: Nov 01 2014 03:23 PM
  • Joined: 29 Dec 2004
Updated to version 0.05 beta
[*: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