RichTextBox and ITextHost
-
I am trying to draw RTF text in my custom UserControl without actually having a RichTextBox window. I have seen a couple articles on the web about creating a windowless RichTextBox by supplying it with a custom ITextHost interface (see http://www.codeguru.com/Cpp/controls/richedit/windowless/article.php/c5367). The problem is, these examples are for c++ and I am wondering how one could go about doing this from C#. Is there a way to somehow import the ITextHost interface from the control? Does anybody have any clue to point me in the right direction? (and no, I am not above using unmanaged code to get this to work). Or is there a better way to accomplish this? Thanks in advance! Michael Developer, Author, Chef
-
I am trying to draw RTF text in my custom UserControl without actually having a RichTextBox window. I have seen a couple articles on the web about creating a windowless RichTextBox by supplying it with a custom ITextHost interface (see http://www.codeguru.com/Cpp/controls/richedit/windowless/article.php/c5367). The problem is, these examples are for c++ and I am wondering how one could go about doing this from C#. Is there a way to somehow import the ITextHost interface from the control? Does anybody have any clue to point me in the right direction? (and no, I am not above using unmanaged code to get this to work). Or is there a better way to accomplish this? Thanks in advance! Michael Developer, Author, Chef
.NET is a natural progression from COM, and can easily interop with COM. Read Interoperating with Unmanaged Code[^] in the .NET Framework SDK for basic information on these concepts. Also, be sure to read Windowless Rich Edit Controls[^] in the Platform SDK for details information, not just examples. First, you should consider encapsulating both the
CreateTextServices
API as well as the implementation ofITextHost
in a class to make it easy to use. You then need to declare theITextHost
and, optionally (though recommended),ITextServices
interfaces in C#. An example of (part, since it's so long) ofITextHost
host looks like this (methods must be in order since this interface inherits fromIUnknown
and methods must be in the proper VTBL order):[Guid("c5bdd8d0-d26e-11ce-a89d-00aa006cadc5")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ITextHost
{
IntPtr TxGetDC();
IntPtr TxReleaseDC(IntPtr hdc);
bool TxShowScrollBar(IntPtr fnBar, bool fShow);
// ...
}Be sure you know the conversion from unmanaged to managed data types. For example, many of the methods use
INT
, which is not necessarily 32 bits. It is system dependent, just likeIntPtr
. You can also use aInt64
(long
) and useMarshalAs(UnmanagedType.SysInt)
which would make it easier and should work well for many, many years (since 64-bit processors are just starting to hit main-stream on the x86 platform supported by current CLI implementations). TheCreateTextServices
factory function would look like this:[DllImport("riched20.dll")]
private static extern int CreateTextServices(
[MarshalAs(UnmanagedType.IUnknown)] object punkOuter,
ITextHost pITextHost,
[MarshalAs(UnmanagedType.IUnknown), Out] out object ppUnk);You could also use
IntPtr
instead ofobject
and then use appropriate methods from the -
.NET is a natural progression from COM, and can easily interop with COM. Read Interoperating with Unmanaged Code[^] in the .NET Framework SDK for basic information on these concepts. Also, be sure to read Windowless Rich Edit Controls[^] in the Platform SDK for details information, not just examples. First, you should consider encapsulating both the
CreateTextServices
API as well as the implementation ofITextHost
in a class to make it easy to use. You then need to declare theITextHost
and, optionally (though recommended),ITextServices
interfaces in C#. An example of (part, since it's so long) ofITextHost
host looks like this (methods must be in order since this interface inherits fromIUnknown
and methods must be in the proper VTBL order):[Guid("c5bdd8d0-d26e-11ce-a89d-00aa006cadc5")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ITextHost
{
IntPtr TxGetDC();
IntPtr TxReleaseDC(IntPtr hdc);
bool TxShowScrollBar(IntPtr fnBar, bool fShow);
// ...
}Be sure you know the conversion from unmanaged to managed data types. For example, many of the methods use
INT
, which is not necessarily 32 bits. It is system dependent, just likeIntPtr
. You can also use aInt64
(long
) and useMarshalAs(UnmanagedType.SysInt)
which would make it easier and should work well for many, many years (since 64-bit processors are just starting to hit main-stream on the x86 platform supported by current CLI implementations). TheCreateTextServices
factory function would look like this:[DllImport("riched20.dll")]
private static extern int CreateTextServices(
[MarshalAs(UnmanagedType.IUnknown)] object punkOuter,
ITextHost pITextHost,
[MarshalAs(UnmanagedType.IUnknown), Out] out object ppUnk);You could also use
IntPtr
instead ofobject
and then use appropriate methods from theHeath, You had 49 minutes (since I posted) to think about and write a response and that is all you could manage? ;) Seriously, many many many many thanks for the tips and pointers. I will be looking into this as you suggest. If I manage to get this working well, I will share the info in the form of an article. Again, thanks thanks thanks for setting me down the right path! Michael Developer, Author, Chef
-
Heath, You had 49 minutes (since I posted) to think about and write a response and that is all you could manage? ;) Seriously, many many many many thanks for the tips and pointers. I will be looking into this as you suggest. If I manage to get this working well, I will share the info in the form of an article. Again, thanks thanks thanks for setting me down the right path! Michael Developer, Author, Chef
Actually, I replied to the other message below first that I hadn't gotten to yet since yesterday (or that could've used some additional attention). Much of what I posted I already knew (I strive to learn all I can, and in-depth). I had to check the headers, though, to make sure I was giving you correct method declarations and it took some time to find the GUID for the
ITextHost
interface, since in the header it's extern'd. :mad: Sorry it took so long! :-O Mwolski wrote: Developer, Author, Chef Same here! My friends and family are always making me cook whenever I can and I like exploring and creating new recipies. I'm thinking "chef" would make a nice mid-life crisis career path! :)Microsoft MVP, Visual C# My Articles
-
Actually, I replied to the other message below first that I hadn't gotten to yet since yesterday (or that could've used some additional attention). Much of what I posted I already knew (I strive to learn all I can, and in-depth). I had to check the headers, though, to make sure I was giving you correct method declarations and it took some time to find the GUID for the
ITextHost
interface, since in the header it's extern'd. :mad: Sorry it took so long! :-O Mwolski wrote: Developer, Author, Chef Same here! My friends and family are always making me cook whenever I can and I like exploring and creating new recipies. I'm thinking "chef" would make a nice mid-life crisis career path! :)Microsoft MVP, Visual C# My Articles
Heath Stewart wrote: Sorry it took so long! Practice makes perfect! ;P - Nick Parker
My Blog | My Articles -
Actually, I replied to the other message below first that I hadn't gotten to yet since yesterday (or that could've used some additional attention). Much of what I posted I already knew (I strive to learn all I can, and in-depth). I had to check the headers, though, to make sure I was giving you correct method declarations and it took some time to find the GUID for the
ITextHost
interface, since in the header it's extern'd. :mad: Sorry it took so long! :-O Mwolski wrote: Developer, Author, Chef Same here! My friends and family are always making me cook whenever I can and I like exploring and creating new recipies. I'm thinking "chef" would make a nice mid-life crisis career path! :)Microsoft MVP, Visual C# My Articles
Heath, 1) Just wanted to let you know that I was successful in taking the second route you outlined. I initially spent about 2 hours trying to get everything defined in C#. This was a bit of a nightmare as you suggested. Then I spent about 10 minutes doing it as a mixed-mode MC++ class library, and exposing the routines I needed. Works like a charm! In the future, I plan revisit the c# way (to eliminate the mc++ dll and learn what I was doing wrong), but for now this is completely workable. Thanks for the previous advice. 2) 5 Years ago I was lucky enough to take a 1 year sabbatical. What did I do? Since I was about to turn 30 I decided I wanted to see the world, but learn something as well. Since I have always cooked, I decided to go to Paris and attend culinary school at Le Cordon Bleu. I heartily reccommend it for burned-out developers! Amazing time, amazing people, amazing food, and an amazing amount of work. The only problem: Software pays way too much to let it go completely! So I heartily applaud the mid-life career change, but I warn you that you will find it hard to let go of your current one. Thanks again! Michael Developer, Author, Chef
-
Heath, 1) Just wanted to let you know that I was successful in taking the second route you outlined. I initially spent about 2 hours trying to get everything defined in C#. This was a bit of a nightmare as you suggested. Then I spent about 10 minutes doing it as a mixed-mode MC++ class library, and exposing the routines I needed. Works like a charm! In the future, I plan revisit the c# way (to eliminate the mc++ dll and learn what I was doing wrong), but for now this is completely workable. Thanks for the previous advice. 2) 5 Years ago I was lucky enough to take a 1 year sabbatical. What did I do? Since I was about to turn 30 I decided I wanted to see the world, but learn something as well. Since I have always cooked, I decided to go to Paris and attend culinary school at Le Cordon Bleu. I heartily reccommend it for burned-out developers! Amazing time, amazing people, amazing food, and an amazing amount of work. The only problem: Software pays way too much to let it go completely! So I heartily applaud the mid-life career change, but I warn you that you will find it hard to let go of your current one. Thanks again! Michael Developer, Author, Chef
Mwolski wrote: ...but I warn you that you will find it hard to let go of your current one I don't think I'll have any problems letting go of the chest pains, irregular heart beat, headaches, stiff joints, tendenitous, and stress. Money is a slight factor, but hopefully these stock options pay-out. :)
Microsoft MVP, Visual C# My Articles