Page 5 of 11

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

Posted: 13 Mar 2015, 01:35
by just me
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.

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

Posted: 13 Mar 2015, 10:15
by iPhilip
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%
   }
}

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

Posted: 13 Mar 2015, 11:16
by just me
Do you want to use the FindText dialog with an Edit of another application? That's not actually related to the class.

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

Posted: 16 Mar 2015, 14:30
by m3user
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?

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

Posted: 19 Mar 2015, 10:38
by m3user
Pasting images actually works on my Win 8.1 64bit, however not on Win 7. Any idea what might be the issue?

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

Posted: 20 Mar 2015, 02:19
by just me
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.

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

Posted: 20 Mar 2015, 19:18
by m3user
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?

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

Posted: 21 Mar 2015, 01:11
by just me
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?

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

Posted: 26 Mar 2015, 06:42
by m3user
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!

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

Posted: 26 Mar 2015, 09:23
by just me
I cannot see how the script would change the keyboard layout.

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

Posted: 26 Mar 2015, 09:48
by m3user
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.

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

Posted: 26 Mar 2015, 21:34
by just me
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.

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

Posted: 27 Mar 2015, 17:01
by m3user
thanks, this did the trick. Thanks for your support just me!

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

Posted: 07 Apr 2015, 06:45
by m3user
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.

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

Posted: 08 Apr 2015, 04:00
by just me
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?

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

Posted: 12 Apr 2015, 12:05
by m3user
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.

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

Posted: 13 Apr 2015, 02:52
by just me
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.

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

Posted: 13 Apr 2015, 12:40
by m3user
Hi just me, I tested your solution and it now works as expected. Thank you!

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

Posted: 14 Apr 2015, 00:37
by just me
Hi m3user, thanks for testing. I've updated the script.

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

Posted: 14 May 2015, 19:59
by BGM
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