Foreword
I found this tutorial by just me in the German forum, and thought it would be useful for those who come here.
With just me's kind permission, I offer this translation.
Regards,
burque505

MyRTF.PNG (29.36 KiB) Viewed 4056 times

As a developer, every now and then you might need to display formatted text in a GUI. AHK does not provide suitable control for this. AHK 1. 1 offers two possibilities for embedding further controls registered in the system:
• ActiveX
• Custom
As a rule, ActiveX controls are COM objects that can only be integrated with a GUI in a way that permits use similar to that of built-in controls.

Custom controls usually behave similar to built-in controls. You can assign variables and subroutines, for example. In principle, all built-in controls can also be used as custom controls. That offers few practical benefits.

For 'simple' output of formatted text, there are two alternatives:
1. HTML -> MSIE-Browser Control (ActiveX)
2. RTF -> RichEdit Control (Custom)
We'll limit ourselves here to the second variant (RTF), and the implementation of custom controls.

RichEdit Control

Like the Edit Control, the RichEdit Control is one of the controls provided by Microsoft via the system, but unlike the 'normal' Edit Control, it has not yet been integrated into AHK..

Some time ago I wrote a RichEdit Class, which allows integrating RichEdit Controls into your own GUIs. The actual reason for this was that I needed a way to print formatted text out of AHK without being dependent on any other application. Nonetheless, I equipped the class with a lot of features and methods only used for editing. This makes the whole package a bit on the bulky side, and it requires a bit of training and practice to use.

While I was developing the class, I kept running across documentation for the Text Object Model (TOM). At first I couldn't get very far with it, and even later I remained a bit leery of getting used to this … 'stuff'. Recently I found this post by teadrinker, motivating me to take a closer look at it. I realized that TOM can make the handling of RichEdit Controls for simple tasks, such as the display of RTF files, much easier. Thus, the following tutorial!

Let's get started!

Step 1:

The RichEdit control is provided and registered by the system library msftedit. dll. From WinXP on, the control class registered is RICHEDIT50W. To be able to use the RichEdit control, the script has to explicitly load this dll, i.e.:

Because AHK doesn't recognize the controls added as being Custom, only the essential styles are set automatically, such as: Eg WS_CHILD, WS_TABSTOP, WS_VISIBLE. Scrollbars can be set with the usual options HScroll / VScroll, because they are not control specific (WS_HSCROLL, WS_VSCROLL). However, even with a height specification such as h400 or r20, AHK can't tell it's a multi-line RichEdit Control. You have to set the appropriate style ES_MULTILINE (0x04) yourself. This applies to all other possible RichEdit styles too, for example, ES_READONLY (0x0800). For a multi-line, read-only RichEdit Control with a width and height of 400 pixels each and a vertical scrollbar, you need:

Now we have an empty RichEdit Control. To get this to display an existing RTF file, we need the associated TOM object, more specifically the TextDocument object. This can be elicited by the control via the message EM_GETOLEINTERFACE. How to do that is roughly described here. For AHK, it looks like this (without going into the ComObj instructions):
• Here, we send an EM_GETOLEINTERFACE message to the RichEdit control. This returns a pointer to the IRichEditOle interface.

Now we've got a RichEdit control and its TextDocument object. This object has the Open method, used to read files into the RichEdit control. The parameter Flags determines what content the file has. For files with RTF text, the flag tomRTF (0x01) is displayed. You can also specify that the file content being read in should a) completely replace the content of the RichEdit control or b) be inserted (tomPasteFile (0x1000).) That's all that's needed to display the file:

With Win 8, Microsoft distributed a significantly updated RichEdit control. For one thing, the display of 'embedded objects' like images is greatly simplified. For RTF files created and displayed under Win 8, no further action is required. However, per my tests, the RichEdit control must not be ReadOnly when the file is read. Why? Because you can't just isolate and remove this style, you have to use the EM_SETOPTIONS message after the control has been created and populated:

@iPhilip: I think you can ignore this. All three posts of sana seem quite strange and not related to the individual threads. In one, that I have reported now and should get deleted, this user also added a spam link to a quote (not quoting him-/herself, but some other user). Nothing productive so far from this user...

Glad to hear it, Marius, it didn't occur to me till later that you might be using the class, and not the sample - sorry about that!

(I didn't want to say "Just hit Ctrl-A and change the font color from the dialog", which is why I posted the GIF.)

For the benefit of anyone else who might have the same question or issue, the included file Class_RichEditDlgs.ahk is the place to look for things like changing font, color, opening and saving files, and finding and replacing text.

I managed to get it much more optimized and with zero width carets and so on, text selections. Even, full support for all hindi / abugida scripts. This version is already available on the official site.

Now, I am rethinking / recoding the alternate typing mode, for which I decided to use a rich edit..... I don't need RTF stuff, just to allow the user to type in any language. I'm halfway done with it already, but still a lot left....

Now I'm struggling to force right alignment. Center works, but not right. I don't know why.

Marius, this is just a thought and maybe way off base (and is definitely off-topic), but if you are looking to use RTL languages, a DotNet WinForm might be easier. Here is a very crude example - I just lost a ton of files, and I mashed this up from what I could find so far. Please let me know if it doesn't work out of the box. The EventHelper dll was compiled on 64-bit Win7.
Regards,
burque505

But... I get... Call to non-existing function... for CLR_LoadLibrary("System.Drawing")

I have two questions:
1. How to set font quality to the rich edit field?
2. How to force the field use multiple fonts? ... now, when characters are missing, it falls back to Arial for the entire line...

This RichEdit seems to be what I need and it works with IMEs, and RTL scripts. I managed to overcome the issues I was having with the text alignment.

I also use Fnt_Library. With this one, the font looks good , with desired quality and font substitution works as expected. However, when I use Fnt_SetFont on the richedit, it gets to the default color: black.