RichEdit controls: get/set text

Get help with using AutoHotkey and its commands and hotkeys
User avatar
jeeswg
Posts: 4958
Joined: 19 Dec 2016, 01:58
Location: UK

RichEdit controls: get/set text

15 Oct 2017, 17:08

I have a function that works to set the text of an internal/external RichEdit control when you specify raw text, or RTF text (text that starts '{\rtf'), but setting the RTF text so far only works with ANSI mode and not Unicode.

Also I have not found a way to get the RTF text directly from an external RichEdit control, note: this is not a priority for me, is this possible? A workaround would be to save the text using WordPad, as an rtf file, or perhaps copy and paste the text into an internal RichEdit control and retrieve the RTF text via EM_STREAMOUT (not yet tested), or if it's possible, to copy the text to the clipboard and retrieve the RTF text somehow.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



[Note: I worked on some related functions here: JEE_ClipboardSetHtml and JEE_ClipboardSetRtf]
GUI COMMANDS: COMPLETE RETHINK - Page 2 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=25893&p=170834#p170834
User avatar
jNizM
Posts: 2320
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: RichEdit controls: get/set text

16 Oct 2017, 03:19

Not sure if this helps, but maybe it is somewhere here (Class_RichEdit.ahk)
[AHK] 1.1.29.01 x64 Unicode | [WIN] 10 Pro (Version 1803) x64 | [GitHub] Profile
Donations are appreciated if I could help you
teadrinker
Posts: 387
Joined: 29 Mar 2015, 09:41
Contact:

Re: RichEdit controls: get/set text

17 Oct 2017, 04:48

RichEdit control has COM-interface similar to Word.Application. Do you consider using it?
User avatar
jeeswg
Posts: 4958
Joined: 19 Dec 2016, 01:58
Location: UK

Re: RichEdit controls: get/set text

17 Oct 2017, 10:31

Yes I would consider using that, for internal controls, and if it works, external controls also. Do you have any working code for that?
User avatar
jeeswg
Posts: 4958
Joined: 19 Dec 2016, 01:58
Location: UK

Re: RichEdit controls: get/set text

17 Oct 2017, 12:02

I've been working on getting/setting a stream for an internal RichEdit control, it works to get the stream (e.g. get RTF), but at the moment, it doesn't work to set the stream.

I'm working on various functions to get/set the text in internal/external controls, and this is the last function I need to complete (unless objects provide an additional way as mentioned above, that would mean perhaps 2 more functions).

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



LINKS:
[this was useful as a template for getting a control's text (stream)]
cRichEdit - Standard RichEdit control for AutoHotkey scripts - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/17869-crichedit-standard-richedit-control-for-autohotkey-scripts/

[this has an example for setting a control's text (stream) based on a file (but not based on a variable)]
Class_RichEdit/Class_RichEdit.ahk at master · AHK-just-me/Class_RichEdit · GitHub
https://github.com/AHK-just-me/Class_RichEdit/blob/master/Sources/Class_RichEdit.ahk
teadrinker
Posts: 387
Joined: 29 Mar 2015, 09:41
Contact:

Re: RichEdit controls: get/set text

17 Oct 2017, 14:16

jeeswg wrote:Yes I would consider using that, for internal controls, and if it works, external controls also. Do you have any working code for that?

Since I got interested in that just after your post and haven't found any examples for scripting, I don't have working code yet. But I'm going to have go at it, if I succeed, I'll post it here.
teadrinker
Posts: 387
Joined: 29 Mar 2015, 09:41
Contact:

Re: RichEdit controls: get/set text

18 Oct 2017, 13:55

My exercises with RichEdit's COM-interfaces:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
jeeswg
Posts: 4958
Joined: 19 Dec 2016, 01:58
Location: UK

Re: RichEdit controls: get/set text

18 Oct 2017, 14:21

If anyone can help me with my set stream function that would be most appreciated.

==================================================

Well done teadrinker, this is really good. It works on my Windows 7 version of WordPad, and on the Windows XP version that I also have.

The example script writes to an external RichEdit control, is it easy to read from an external RichEdit control? [EDIT: The script writes the same contents to both an internal and an external control, but there isn't a wholesale copy/paste, I'm experimenting ...]

It's only been one or two days since I first found out that RichEdit controls support objects, they really are quite powerful. You've also confirmed for me that they can do highlighting. This means now that RichEdit v. Scintilla is a tough choice. Thanks.

==================================================

My test script so far, which is basically the same as teadrinker's above.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



==================================================

[EDIT:] Btw are you using some documentation other than MSDN? Because it isn't even clear that you can do .Range(0, 10).Text, from what I've read.

The main things remaining that I've been struggling to do are: get/set all text/RTF.

Links:
ITextDocument interface (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/bb774052(v=vs.85).aspx
Text Object Model Interfaces (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/ff486058(v=vs.85).aspx
ITextRange interface (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/bb774058(v=vs.85).aspx
teadrinker
Posts: 387
Joined: 29 Mar 2015, 09:41
Contact:

Re: RichEdit controls: get/set text

18 Oct 2017, 18:18

Btw are you using some documentation other than MSDN? Because it isn't even clear that you can do .Range(0, 10).Text, from what I've read.

I didn't find any documentation, except the links you gave.
ITextDocument, Methods, Range —> ITextDocument::Range method —> ITextRange interface, Methods —> GetText, SetText
The problem is that method names for scripting may differ slightly, and one need to guess, how to change them to get them working for scripting.
The main things remaining that I've been struggling to do are: get/set all text/RTF.

Those interfaces give an access only to getting/setting plain text, anyway I didn't find any info about RTF.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
jeeswg
Posts: 4958
Joined: 19 Dec 2016, 01:58
Location: UK

Re: RichEdit controls: get/set text

28 Oct 2017, 04:04

I managed to complete various functions for getting/setting the stream of a RichEdit control that belongs to the script.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Example code to create a GUI with a RichEdit control:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Btw one interesting question relating to standard Edit controls:
Edit control: right-aligned control but left-aligned text - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=38764
DigiDon
Posts: 176
Joined: 19 May 2014, 04:55
Contact:

Re: RichEdit controls: get/set text

02 Feb 2018, 03:58

Hi jeeswg,

I have some troubles trying to use your StreamIn function to set RTF text.

I am trying to insert for example a link. The rtf is correct. But If I try this it does not work

Code: [Select all] [Download] GeSHi © Codebox Plus

LinkVar:="{\rtf1{\field{\*\fldinst{ HYPERLINK "" http://www.msn.com""}}{\fldrslt{ MSN} }}}"
JEE_RichEditSetStream(RE2.HWND, 0x2 | 0x8000, &LinkVar, (StrLen(LinkVar)+1)*2)


I'm not sure about the StringLength but I've tried different settings and it didn't work.

Your example of plain text did work in contrary:

Code: [Select all] [Download] GeSHi © Codebox Plus

LinkVar:="abcdefghijklmnopqrstuvwxyz"
JEE_RichEditSetStream(RE2.HWND, 0x11, &LinkVar,(StrLen(LinkVar)+1)*2)


What am I not understanding ?
Thanks
EverFastAccess : Take Notes on anything the Fast way: Attach notes, Set reminders & Speed up research in 1 gesture - AHK topic
AHK Dynamic Obfuscator L - Protect your AHK code by Obfuscation - AHK topic
QuickModules for Outlook : Sort Outlook emails very quickly to multiple folders - AHK topic

Coding takes lots of time and efforts. If I have helped you or if you enjoy one of my free projects, please consider a small donation :thumbup:
User avatar
jeeswg
Posts: 4958
Joined: 19 Dec 2016, 01:58
Location: UK

Re: RichEdit controls: get/set text

02 Feb 2018, 05:19

- I had real trouble with the JEE_RESetStream, and finding information about it, I have one example for it here:
GUIs via DllCall: get/set internal/external control text - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=40514
Also there is JEE_RESetStreamFromFile. In general I had problems with using UTF-16 in streams, and possibly just me did as well, although I believe it's possible to use Unicode in RichEdit via ANSI streams. Any progress you make with setting the stream, especially with 0x8000 if it can be made to work, would be worth carefully documenting.
GitHub - AHK-just-me/Class_RichEdit: AHK RichEdit Control
https://github.com/AHK-just-me/Class_RichEdit
- It can be useful to save RTF files in WordPad, and inspect the contents using Notepad.
- Btw here is an example to put RTF on the clipboard.
GUI COMMANDS: COMPLETE RETHINK - Page 2 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=25893&p=170834#p170834
DigiDon
Posts: 176
Joined: 19 May 2014, 04:55
Contact:

Re: RichEdit controls: get/set text

03 Feb 2018, 12:38

Well just me's class function SetText is in fact working well.
But it is using the EM_SETTEXTEX = 0x0461 message and not the EM_STREAMIN = 0x0449 message.
Indeed it is passing the string as AINSI.
Here is the code from just me:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



So... I won't say this was easy ;) but I finally managed to translate the _GUICtrlRichEdit_StreamFromVar function from AU3 GuiRichEdit class.
And it is ... finally working !
So for the sake of it here is how it does it:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

EverFastAccess : Take Notes on anything the Fast way: Attach notes, Set reminders & Speed up research in 1 gesture - AHK topic
AHK Dynamic Obfuscator L - Protect your AHK code by Obfuscation - AHK topic
QuickModules for Outlook : Sort Outlook emails very quickly to multiple folders - AHK topic

Coding takes lots of time and efforts. If I have helped you or if you enjoy one of my free projects, please consider a small donation :thumbup:
User avatar
jeeswg
Posts: 4958
Joined: 19 Dec 2016, 01:58
Location: UK

Re: RichEdit controls: get/set text

03 Feb 2018, 14:19

- Hmm, I have been curious about when exactly you can pass Unicode text, rather than ANSI text. (Although RTF text stored as ANSI has ways to store Unicode characters by their number I believe.)
- Does 'set text', handle plaintext only, or RTF text as well?
- Btw have you been testing occasionally in both x64 and x32? Sometimes, but not always, you need to use Ptr instead of Int, and specify different offsets. Thanks.
DigiDon
Posts: 176
Joined: 19 May 2014, 04:55
Contact:

Re: RichEdit controls: get/set text

04 Feb 2018, 03:54

-Don't know about the first
-RTF works as well for SetText
-I have Unicode 64 bits. I believe if it works on it, given that I use A_PtrSize, it should work on 32bits.
I wish someone will be able to confirm it though.
Last edited by DigiDon on 04 Feb 2018, 03:56, edited 1 time in total.
EverFastAccess : Take Notes on anything the Fast way: Attach notes, Set reminders & Speed up research in 1 gesture - AHK topic
AHK Dynamic Obfuscator L - Protect your AHK code by Obfuscation - AHK topic
QuickModules for Outlook : Sort Outlook emails very quickly to multiple folders - AHK topic

Coding takes lots of time and efforts. If I have helped you or if you enjoy one of my free projects, please consider a small donation :thumbup:
burque505
Posts: 545
Joined: 22 Jan 2017, 19:37

Re: RichEdit controls: get/set text

12 Mar 2018, 19:53

Hi, you guys are light years ahead of me on this, but it seems like it might interest you.
My quest here was to grab the RTF content from a file as it displays (not the RTF code) and be able to paste it via Ctrl-V.
After reading just me's tutorial on RTF yesterday, I put together this little scenario:

Using a function derived from just me's code, I grab the content of an RTF file, put it into a hidden GUI with a custom RichEdit like just me's and jeeswg's.
Then I grab the content by sending ^a^c to the control, send it a click to kill the highlighting, then kill the gui.

Function:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Script with function:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Then I fire up a jeeswg-ish (new adjective :twisted: It's yours from this thread nearly verbatim) GUI:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Then I paste the clipboard into the jeeswg-ish GUI (manually so far, not in the script, this is just proof-of-concept).
The real beauty of this for me is that if I paste into a non-RTF capable app, I still get the unformatted text!
If I paste into any RTF-capable app, it works so far, and I get the formatted text in LibreOffice, MS Word, and WordPad, which is to be expected since there's rich text on the clipboard.
I'm on Win7, so I haven't been able to take advantage of the new msftedit.dll with its image-pasting capability.

RTFtoClip working.zip
(12.76 KiB) Downloaded 11 times



Regards
burque505
User avatar
jeeswg
Posts: 4958
Joined: 19 Dec 2016, 01:58
Location: UK

Re: RichEdit controls: get/set text

12 Mar 2018, 20:47

- Interesting. So I suppose the idea is to get the underlying RTF content from a RichEdit control.
- To get the RTF content from a RichEdit control that belongs to your process, there is JEE_REGetStream.
GUIs via DllCall: get/set internal/external control text - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=40514
- To get the RTF content from a RichEdit control from an external process, you can select the control's contents, copy to the clipboard, and use JEE_ClipboardGetRtf. I don't know if there is more direct way than this.
Is it possible to stringreplace rtf to clipboard? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=45284
- To get the text from an internal or external RichEdit control, you use ControlGetText.
- You can get the RTF content from a file by using FileRead.
- For the clipboard, you can put multiple clipboard formats onto the clipboard, and other programs check for the presence of certain formats. You can use NirSoft InsideClipboard to see what's on the clipboard.
- There may be other methods I don't know about. Cheers.
burque505
Posts: 545
Joined: 22 Jan 2017, 19:37

Re: RichEdit controls: get/set text

13 Mar 2018, 10:06

Hi jeeswg, thanks for the links and the scripts. The main idea here for me was, when copying formatted text, to be able to get what Ctrl-C does under the hood onto the clipboard. The reason is that ( besides whatever else the Windows clipboard does) it allows pasting the content in either formatted or unformatted text with just Ctrl-V, depending on the application. My previous efforts allowed getting RTF from a file into the clipboard, but only the RTF would paste with Ctrl-V. In other words, whatever I copied wouldn't transfer with just Ctrl-V as unformatted text to, for example, Notepad.

A couple of hours ago I found this thread with DigiDon's RichEdit OleCallback.ahk function. Now I can pull RTF that contains images directly out of a file and paste it.

RTFtoClipCallback.ahk

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



RTFtoClipExampleCB (Includes the script above)

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



and DigiDon and just me's RichEdit OleCallback.ahk:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



I run the script, which pulls the content of the RTF file into the transparent GUI, copies it to the clipboard, and exits.
The pasting is done manually with Ctrl-V.
The only thing the script does is get the RTF content (not code) of the RTF file onto the clipboard.

Regards,
burque505
burque505
Posts: 545
Joined: 22 Jan 2017, 19:37

Re: RichEdit controls: get/set text

13 Mar 2018, 11:10

Hey jeeswg, just me sent me your RTF2Clip.ahk! I'll put in below for everyone else's benefit. It still pastes plain text where applicable, which was my goal.
Spoiler


It makes my Rube Goldberg attempts above obsolete. For Win7, I made the following change (line 14) so it would paste RTF that has images (requires RichEdit OleCallback.ahk be included, see above):
Spoiler


Here's a calling script:
Spoiler


DigiDon, jeeswg, just me, thanks again!
Regards,
burque505

Return to “Ask For Help”

Who is online

Users browsing this forum: Adrulesok99, newbieforever, NovaRanger, truekefir and 81 guests