I'm writing a control similar to visio to draw shapes and text, now I want to support rich text drawing into my control. I searched the web and found what they call "windowless rich text control" but it does not have code for C#, below is what I tried to do but it is not work:
namespace WindowsFormsApplication1
{
[Guid("c5bdd8d0-d26e-11ce-a89d-00aa006cadc5")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ITextHost
{
IntPtr TxGetDC();
IntPtr TxReleaseDC(IntPtr hdc);
}
public partial class Form1 : Form, ITextHost
{
\[DllImport("riched20.dll")\]
private static extern int CreateTextServices(
\[MarshalAs(UnmanagedType.IUnknown)\] object punkOuter,
ITextHost pITextHost,
\[MarshalAs(UnmanagedType.IUnknown), Out\] out object ppUnk);
public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form1\_Load);
}
void Form1\_Load(object sender, EventArgs e)
{
object ppUnk = null;
int result = CreateTextServices(null, this, out ppUnk);
}
#region ITextHost Members
public IntPtr TxGetDC()
{
return this.Handle;
}
public IntPtr TxReleaseDC(IntPtr hdc)
{
return IntPtr.Zero;
}
}
}
When run into the method CreateTextServices
, it keeps saying that the memory is corrupted. I don't know what I did wrong in the code. And I stuck with it. Please help me with that, thanks for your concern.