How to handle WM_GETTEXT in overridden WndProc ?

  • Thread starter =?ISO-8859-1?Q?Martin_M=FCller?=
  • Start date
?

=?ISO-8859-1?Q?Martin_M=FCller?=

Hi folks!
I have a .NET RichTextBox derived class which has to react differently
to WM_GETTEXT (and some other messages) than it does by default
(mainly converting between \n and \r\n).

I override WndProc, but returning the (altered) text does not work:

protected override void WndProc(ref System.Windows.Forms.Message m)
{
switch (m.Msg)
{
case WM_GETTEXT:
string s = ModifyOriginalText(this.Text);
int charsToCopy = Math.Min(m.WParam.ToInt32(),s.Length);
// return the string
m.LParam = Marshal.StringToHGlobalAuto(s.Substring(0,
charsToCopy));
// result is the number of characters copied
m.Result = new IntPtr(charsToCopy);
return;
}
base.WndProc(ref m);
}

I'm pretty sure there's a way to make this work, but right now I can't
see it.
Anyone can help?

TIA,

Martin Müller
 
C

Chris Taylor

Hi,

As stated in another post, you can override the Text property.

However if you requre WM_GETTEXT to be handled because the message is being
explicidly send, then the one problem I can see from a quick scan of your
code is that you are creating a new pointer and assigning it to the LParam.
For WM_GETTEXT the LParam should have a pointer to a already allocated
buffer which you should fill with the text. The WParam specifies the max
number of characters that the buffer pointer to by LParam can hold,
including the NULL terminator.

Hope this helps

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top