An RTF Editor Control for .NET Compact Framework

0.00 avg. rating (0% score) - 0 votes

As you may have known, there is no RichTextBox control in .NET Compact Framework. This creates a lot of headaches for developers who want to display RTF-formatted text in their program. The only way is to use the RichInk control. However, there is no good free managed wrapper for it, even the OpenNetCF’s InkX wrapper is still untidy and requires a lot of P/Invoke calls to make it work properly. IntelliProg used to provide a working control but the company has seemingly disappeared (their domain is parked). Until 2011, DSRTech also sold an expensive commercial solution but the page for this control has since been removed from their website.

AgileNotesTouch with RichInk control

After some research, I found a freeware called AgileNotes Touch which allows user to write notes and save it into TXT, PWI or RTF. Since the source code is not available, I decided to go ahead decompiling the executable using Reflector. This was an easy task – the decompiled source code, with only some minor modifications, compiled and ran as if it were the original. It turned out AgilesNotes Touch also uses the RickInk control, with some nice managed .NET wrappers which can be re-used easily.

Creating RTF programmatically: NRtfTree

Since I need to create the RTF document programmatically, I decided to use the open source project NRtfTree. Some basic formatting worked well until I tried to create an RTF document with images such as this. The document was created properly but to my disappointment the image was not displayed when the document was open with my program and with Pocket Word. On the other hand, Wordpad, and Office Word, displayed the image just fine. It took me a long time before I figured out the cause – the RichInk control (and the RichEd50W control used by Pocket Word) only supports binary images, and not hex images. For example the following picture will be discarded:

{pictpngblippicw10449pich3280picwgoal5924pichgoal1860 hex data}

but the following will work:

{pictpngblippicw10449pich3280picwgoal5924pichgoal1860bin binary data}

And since NRtfTree only supports hex image, I had to modify the library to support binary image. What a hassle, but in the end everything worked well and images are displayed properly.

From RichInk to RichEdit50W

Next, I noticed that my control could not display images on the same line as text and seem to drop tables, while Pocket Word displayed them just fine. This is a hint that Pocket supports higher RTF versions then RichInk, so I use Remote Spy to find out which control Pocket is using:

PocketWord never used RichInk, but RichEd50W, similiar to what RichTextBox is using. I changed my code to use this window class (it stays in RichEd20.dll):

WindowsAPIs.LoadLibrary(“RichEd20.dll”);
this.m_RichInk = new WindowHost(“RICHEDIT50W”, 0, 0);

and images are displayed as intended. Surprisingly that’s all I need – my program is now as good as Pocket Word.

Making the document read-only

My main purpose was to display documents – and not allowing user to modify them so I applied ES_READONLY to the window style:

this.m_RichInk = new WindowHost(“RICHEDIT50W”, 0, ES_READONLY);

There is still a minor problem: the window display may be distorted or incomplete after some extensive usage. I have yet to figure out the cause of the problem or how to fix it.

Source code

The sample source code is here. It includes the modified NRtfTree library, the decompiled source code of AgilesNotes Touch, and a sample form showing you how to read and write RTF files. NRtfTree was modified to insert binary images, instead of hex, and to support unicode characters. I have commented clearly where I modified the code.

I hope this will help other developers facing the same problem.

0.00 avg. rating (0% score) - 0 votes
ToughDev

ToughDev

A tough developer who likes to work on just about anything, from software development to electronics, and share his knowledge with the rest of the world.

4 thoughts on “An RTF Editor Control for .NET Compact Framework

  • September 1, 2012 at 5:17 pm
    Permalink

    Wow, great thanks.
    Scoured the entire Internet and your solution is the best. Thank you.

    Reply
  • September 6, 2012 at 9:16 pm
    Permalink

    Alex, glad it help you. Cheers :)

    Reply
  • April 12, 2013 at 12:02 pm
    Permalink

    Hi
    Can you please check the link for the source code. It's not working.

    Reply
  • April 20, 2013 at 10:48 am
    Permalink

    Hi, Sorry for the late reply. I have updated the link to use dropbox. Mediafire for some reasons closed my account and taking with it a lot of important documents, including shared source code :(

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>