Class RichEdit - update on 2015-04-14 (v0.1.05.00)

Post your working scripts, libraries and tools for AHK v1.1 and older
just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Class RichEdit - rich edit control for AHK (Unicode)

13 Mar 2015, 01:35

Hi iPhilip,

not the class as is, but you might use the wrapped messages. However, to work with controls of other applications you'll have to put the structures used with the mesages into the memory of the remote process and get the results from there, considering the bitness of the remote process.
iPhilip
Posts: 796
Joined: 02 Oct 2013, 12:21

Re: Class RichEdit - rich edit control for AHK (Unicode)

13 Mar 2015, 10:15

I guess it's harder than the simple attempt below. I am not sure how to "put the structures used with the messages into the memory of the remote process".

Code: Select all

#NoEnv                   ; Recommended for performance and compatibility with future AutoHotkey releases
#SingleInstance Force    ; Replaces the old instance automatically (same as Reload)

Gui, New, +HwndhGui
ControlGet, hEdit, Hwnd,, Edit1, ahk_class Notepad
Return

#f::
IfWinNotExist ahk_id %hFindDialog%
   hFindDialog := FindText(hGui,hEdit)
WinActivate ahk_id %hFindDialog%
Return

FindText(hGui,hEdit) { ; Find dialog box
; ===================================================================================================================
   ; RE : RichEdit object
   Static FINDMSGSTRING := "commdlg_FindReplace"
        , FR_DOWN := 1, FR_MATCHCASE := 4, FR_WHOLEWORD := 2
        , Buf := "", FR := "", Len := 256
        , FR_Size := A_PtrSize * 10
;      Text := RE.GetSelText()
   VarSetCapacity(FR, FR_Size, 0)
   NumPut(FR_Size, FR, 0, "UInt")
   VarSetCapacity(Buf, Len, 0)
   If (Text && !RegExMatch(Text, "\W"))
      Buf := Text
   Offset := A_PtrSize
   NumPut(hGui, FR, Offset, "UPtr") ; hwndOwner
   OffSet += A_PtrSize * 2
   NumPut(FR_DOWN, FR, Offset, "UInt")	   ; Flags
   OffSet += A_PtrSize
   NumPut(&Buf, FR, Offset, "UPtr")	      ; lpstrFindWhat
   OffSet += A_PtrSize * 2
   NumPut(Len,	FR, Offset, "Short")       ; wFindWhatLen
   FindTextProc("Init", hEdit, "")
   OnMessage(DllCall("User32.dll\RegisterWindowMessage", "Str", FINDMSGSTRING), "FindTextProc")
   Return DllCall("Comdlg32.dll\FindTextW", "Ptr", &FR, "UPtr")
}
; -------------------------------------------------------------------------------------------------------------------
FindTextProc(L, M, H) { ; skipped wParam, can be found in "This" when called by system
   ; Find dialog callback procedure
   ; EM_FINDTEXTEXW = 0x047C, EM_EXGETSEL = 0x0434, EM_EXSETSEL = 0x0437, EM_SCROLLCARET = 0x00B7
   ; FR_DOWN = 1, FR_WHOLEWORD = 2, FR_MATCHCASE = 4,
   Static FINDMSGSTRING := "commdlg_FindReplace"
        , FR_DOWN := 1, FR_MATCHCASE := 4, FR_WHOLEWORD := 2 , FR_FINDNEXT := 0x8, FR_DIALOGTERM := 0x40
        , HWND := 0
   If (L = "Init") {
      HWND := M
      Return True
   }
   Flags := NumGet(L + 0, A_PtrSize * 3, "UInt")
   If (Flags & FR_DIALOGTERM) {
      OnMessage(DllCall("User32.dll\RegisterWindowMessage", "Str", FINDMSGSTRING), "")
      ControlFocus, , ahk_id %HWND%
      HWND := 0
      Return
   }
   VarSetCapacity(CR, 8, 0)
   SendMessage, 0x0434, 0, &CR, , ahk_id %HWND%
   Min := (Flags & FR_DOWN) ? NumGet(CR, 4, "Int") : NumGet(CR, 0, "Int")
   Max := (Flags & FR_DOWN) ? -1 : 0
   OffSet := A_PtrSize * 4
   Find := StrGet(NumGet(L + Offset, 0, "UPtr"))
   VarSetCapacity(FTX, 16 + A_PtrSize, 0)
   NumPut(Min, FTX, 0, "Int")
   NumPut(Max, FTX, 4, "Int")
   NumPut(&Find, FTX, 8, "Ptr")
   SendMessage, 0x047C, %Flags%, &FTX, , ahk_id %HWND%
   S := NumGet(FTX, 8 + A_PtrSize, "Int"), E := NumGet(FTX, 12 + A_PtrSize, "Int")
   If (S = -1) && (E = -1)
      MsgBox, 262208, Find, No (further) occurence found!
   Else {
      Min := (Flags & FR_DOWN) ? E : S
      SendMessage, 0x0437, 0, % (&FTX + 8 + A_PtrSize), , ahk_id %HWND%
      SendMessage, 0x00B7, 0, 0, , ahk_id %HWND%
   }
}
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Class RichEdit - rich edit control for AHK (Unicode)

13 Mar 2015, 11:16

Do you want to use the FindText dialog with an Edit of another application? That's not actually related to the class.
m3user
Posts: 235
Joined: 17 Jan 2014, 18:11

Re: Class RichEdit - rich edit control for AHK (Unicode)

16 Mar 2015, 14:30

Thank you for this great class!

I have one question: after some text is entered in the control - is to possible to determine if the text was actually formatted in any way (contains any rtf formatting) or is it just a plain non-formatted text?
m3user
Posts: 235
Joined: 17 Jan 2014, 18:11

Re: Class RichEdit - rich edit control for AHK (Unicode)

19 Mar 2015, 10:38

Pasting images actually works on my Win 8.1 64bit, however not on Win 7. Any idea what might be the issue?
just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Class RichEdit - rich edit control for AHK (Unicode)

20 Mar 2015, 02:19

m3user wrote:I have one question: after some text is entered in the control - is to possible to determine if the text was actually formatted in any way (contains any rtf formatting) or is it just a plain non-formatted text?
I do not understand the question. All text contained in the control is RTF formatted.
Any idea what might be the issue?
Yes, with Win 8 new features where added to the rich edit control and others.
m3user
Posts: 235
Joined: 17 Jan 2014, 18:11

Re: Class RichEdit - rich edit control for AHK (Unicode)

20 Mar 2015, 19:18

Sorry, I mean is there a way to determine if the text in the control is still in default format or the formatting has changed?

Regarding pictures, does this mean it is not possible to paste pictures to Rtf controls in Win 7 at all or this is just AutoHotkey issue?
just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Class RichEdit - rich edit control for AHK (Unicode)

21 Mar 2015, 01:11

You might use GetCharFormat() and/or GetParaFormat().

You're running the same script and the control isn't one of AHK's built-in. How could it be an AHK issue?
m3user
Posts: 235
Joined: 17 Jan 2014, 18:11

Re: Class RichEdit - rich edit control for AHK (Unicode)

26 Mar 2015, 06:42

Thanks just me, this helped!

Another question: I'm sometimes using russian language layout and when a western only font is selected it automatically changes a keyboard layout to EN. Is there a way to correct this? The language layout should not change. Thanks!
just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Class RichEdit - rich edit control for AHK (Unicode)

26 Mar 2015, 09:23

I cannot see how the script would change the keyboard layout.
m3user
Posts: 235
Joined: 17 Jan 2014, 18:11

Re: Class RichEdit - rich edit control for AHK (Unicode)

26 Mar 2015, 09:48

I do this (default language RU)
- run RichEdit_sample.ahk
- select one word and open a font dialog,
- select a font with Script=Western

Now whenever if I move a cursor to this word, language layout changes to EN, when I move cursor to other words, it goes back to RU.

I tried msgbox % RE2.GetRTF() and the rtf code shows lang changes.
just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Class RichEdit - rich edit control for AHK (Unicode)

26 Mar 2015, 21:34

OK, this might be caused by

Code: Select all

      ; Initialize control
      ; EM_SETLANGOPTIONS = 0x0478 (WM_USER + 120)
      ; IMF_AUTOKEYBOARD = 0x01, IMF_AUTOFONT = 0x02
      SendMessage, 0x0478, 0, 0x03, , ahk_id %HWND%
within the __New() method. You might try to comment it out.
m3user
Posts: 235
Joined: 17 Jan 2014, 18:11

Re: Class RichEdit - rich edit control for AHK (Unicode)

27 Mar 2015, 17:01

thanks, this did the trick. Thanks for your support just me!
m3user
Posts: 235
Joined: 17 Jan 2014, 18:11

Re: Class RichEdit - rich edit control for AHK (Unicode)

07 Apr 2015, 06:45

possible bug?
- select few lines of text and change the font size and color,
- apply Format/Numbering to the same text
The last line numbering is not formatted with the same font/size.
just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Class RichEdit - rich edit control for AHK (Unicode)

08 Apr 2015, 04:00

Format/Numbering just sends a WM_SETPARAFORMAT message to the control which does not specify a font. You might try to exclude the line feed of the last line from the selection. Maybe it helps?
m3user
Posts: 235
Joined: 17 Jan 2014, 18:11

Re: Class RichEdit - rich edit control for AHK (Unicode)

12 Apr 2015, 12:05

Thanks just me, makes sense.

I tried to use a class in my script and it works great! There is still a thing I cannot figure out: I have difficulty to delete a file which is/was opened in Rich edit. For example, I tried to add a FileDelete, %Open_file% at the end of a FileClose label. Unfortunately the file is not deleted as it obviously requires some extra permissions. Sometimes it gets deleted after the script is closed.

Is there any command which can be used to un-lock or release the previously opened file for deleting? Thanks.
just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Class RichEdit - rich edit control for AHK (Unicode)

13 Apr 2015, 02:52

Hi m3user,

I suppose it happens only for RTF files. It might be caused by a glitch with the file handling in the LoadRTF() method. I revised both the LoadRTF() and the LoadRTFProc() callback function. Would you please test and tell me whether it fixes your problem?

Code: Select all

; Removed test code.
Last edited by just me on 14 Apr 2015, 00:38, edited 1 time in total.
m3user
Posts: 235
Joined: 17 Jan 2014, 18:11

Re: Class RichEdit - rich edit control for AHK (Unicode)

13 Apr 2015, 12:40

Hi just me, I tested your solution and it now works as expected. Thank you!
just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Class RichEdit - update on 2015-04-14 (v0.1.05.00)

14 Apr 2015, 00:37

Hi m3user, thanks for testing. I've updated the script.
User avatar
BGM
Posts: 507
Joined: 20 Nov 2013, 20:56
Contact:

Re: Class RichEdit - update on 2015-04-14 (v0.1.05.00)

14 May 2015, 19:59

Oh, I'm missing a step somewhere just trying to get the control to appear. When the GUI shows there is no control present. What am I missing?

Code: Select all

#include Class_RichEdit.ahk
appname := "SimpleRichEdit"
gosub, mainui
return
;------------------------------

mainui:
	Gui, +Hwnd%appname%

	MyEdit := new richedit(appname,"w75 vmyedit gMessage", false)
	MyEdit.AlignText("RIGHT")

	Gui, Show, h200 w300 center,%appname%
Return

Message:
	guicontrolget, myedit
	msgbox, My contents are %myedit%
return

GuiClose:
ExitApp
Last edited by BGM on 14 May 2015, 21:32, edited 1 time in total.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: gwarble and 108 guests