How can I use sendmessage to input some text into Richtextbox(C#)?

  • Thread starter Thread starter Jason Zhou
  • Start date Start date
J

Jason Zhou

I want to use the api, sendmessage, to input some text into the
richtextbox
I think I should send 'key-down' message to the richtextbox.

for example,
e.shift && e.keycode == e.D0
is
)

Would you please tell me how to write the sendmessage method?
Thanks!
 
Jason said:
I want to use the api, sendmessage, to input some text into the
richtextbox
I think I should send 'key-down' message to the richtextbox.

for example,
e.shift && e.keycode == e.D0
is
)

Would you please tell me how to write the sendmessage method?
Thanks!

Hi Jason,

I am surprised no-one has answered this yet, its actually quite simple.

You don't need to send key-down messages to send text to the rich text
box, you need to send a WM_SETTEXT message, the text is sent via the
lparam, I personally find the easiest way to send lpstr values across
to API calls is to marshal the data as a string, some people like to
muck about with stringbuilders etc..the below code is a simple example
of how to do this....(you'll also need to stick
System.Runtime.InteropServices in the using section)


[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int wMsg, int
wParam, [MarshalAs(UnmanagedType.LPStr)] string lParam);

private const int WM_SETTEXT = 0x0C;

private void button1_Click(object sender, EventArgs e)
{
string txt = "Hello World";
SendMessage(richTextBox1.Handle, WM_SETTEXT,0, txt);
}

Regards Tim.
--
 
Jason said:
I want to use the api, sendmessage, to input some text into the
richtextbox
I think I should send 'key-down' message to the richtextbox.
for example,
e.shift && e.keycode == e.D0
is
)
Would you please tell me how to write the sendmessage method?
Thanks!

Hi Jason,

I am surprised no-one has answered this yet, its actually quite simple.

You don't need to send key-down messages to send text to the rich text
box, you need to send a WM_SETTEXT message, the text is sent via the
lparam, I personally find the easiest way to send lpstr values across
to API calls is to marshal the data as a string, some people like to
muck about with stringbuilders etc..the below code is a simple example
of how to do this....(you'll also need to stick
System.Runtime.InteropServices in the using section)

[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int wMsg, int
wParam, [MarshalAs(UnmanagedType.LPStr)] string lParam);

private const int WM_SETTEXT = 0x0C;

private void button1_Click(object sender, EventArgs e)
{
string txt = "Hello World";
SendMessage(richTextBox1.Handle, WM_SETTEXT,0, txt);
}

Regards Tim.
--

Hi Tim,

Thanks for your kind-hearted help.
Yes the method you supplied is simple. But the key point in my
question is:
1. The text I want to send to the richtextbox is not only like 'Hello
World'. Maybe the text would be '_&*^%"'. And all these letters are
sent from keyboard, not from a variable.

2. You use the api settext. It's not so good. Because 'settext' will
clear all the orginal text. But what I want to do is to append.

Wish you can understand. Thanks again.
 
2. You use the api settext. It's not so good. Because 'settext' will
clear all the orginal text. But what I want to do is to append.

Wish you can understand. Thanks again.

Ok, in that case you just need to use...

[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int wMsg, int
wParam, [MarshalAs(UnmanagedType.LPStr)] string lParam);

private const int WM_SETTEXT = 0x0C;
private const int EM_REPLACESEL = 0xC2;

private void button1_Click(object sender, EventArgs e)
{
string txt = "_&*^%";
SendMessage(richTextBox1.Handle, EM_REPLACESEL,0, txt);
}

The EM_REPLACESEL will as it says replace the selection, if the caret
is at the end of the edit box it will just append and move the
selection to the end. It will append the "_%*^%" characters that you
mention just fine Note you can also set the selection manually as well
if you need to programatically make sure that the selection is at the
end (or for that matter in the middle), for that just use the message
EM_SETSEL.

Note, you only want to send key down messages to controls if you want
those controls to actually process the key, in the case of EditBoxes
and RichEdits this usually just means displaying it, if your app is the
one getting the key strokes its probably just the results of the
processing i.e. the characters that you want to send to the control,
i.e. settext replacesel much simpler than doing it char by char.

If what you want to do really and truely is to send keystrokes to the
control then use WM_KEYDOWN in the same kind of way as I demonstrated
above but with the lparam changed to an int instead of marshalled as a
lpstr. You will also need to know how to set the bits in the lparam
parameter, there is some doco here....
http://msdn.microsoft.com/en-us/library/ms646280(VS.85).aspx

Hope this helps.

Regards Tim.
--
 
Peter said:
I'm not really clear on why you want to use the SendMessage function.
If you've got a RichTextBox instance, why not just append the text
to the Text property?

Heh heh, actually thats a good question, my assumption was that his
RichTextBox is a handle procured from an externaly running App, ala an
Expose type of thing.

Cheers Tim.


--
 
This is great info, Tim. Thanks for your help with the EM_SETSEL.
Jason Zhou wrote:


Hi Jason,

I am surprised no-one has answered this yet, its actually quite simple.

You don't need to send key-down messages to send text to the rich text
box, you need to send a WM_SETTEXT message, the text is sent via the
lparam, I personally find the easiest way to send lpstr values across
to API calls is to marshal the data as a string, some people like to
muck about with stringbuilders etc..the below code is a simple example
of how to do this....(you'll also need to stick
System.Runtime.InteropServices in the using section)


[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int wMsg, int
wParam, [MarshalAs(UnmanagedType.LPStr)] string lParam);

private const int WM_SETTEXT = 0x0C;

private void button1_Click(object sender, EventArgs e)
{
string txt = "Hello World";
SendMessage(richTextBox1.Handle, WM_SETTEXT,0, txt);
}

Regards Tim.
--
09:41 -0700, Jason Zhou <[email protected]>
wrote:


Why does the actual characters and their source have anything to do with
how you add them to the control?


I'm not really clear on why you want to use the SendMessage function. If
you've got a RichTextBox instance, why not just append the text to the
Text property?

Using p/invoke for things you could do readily using the regular .NET API
seems a little weird to me. What's the point here? Why do you want to do
that? What is it about the .NET API that doesn't accomplish what you want?

Pete
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int wMsg, int
wParam, [MarshalAs(UnmanagedType.LPStr)] string lParam);

private const int WM_SETTEXT = 0x0C;
private const int EM_REPLACESEL = 0xC2;

private void button1_Click(object sender, EventArgs e)
{
string txt = "_&*^%";
SendMessage(richTextBox1.Handle, EM_REPLACESEL,0, txt);
}

The EM_REPLACESEL will as it says replace the selection, if the caret
is at the end of the edit box it will just append and move the
selection to the end. It will append the "_%*^%" characters that you
mention just fine Note you can also set the selection manually as well
if you need to programatically make sure that the selection is at the
end (or for that matter in the middle), for that just use the message
EM_SETSEL.

Note, you only want to send key down messages to controls if you want
those controls to actually process the key, in the case of EditBoxes
and RichEdits this usually just means displaying it, if your app is the
one getting the key strokes its probably just the results of the
processing i.e. the characters that you want to send to the control,
i.e. settext replacesel much simpler than doing it char by char.

If what you want to do really and truely is to send keystrokes to the
control then use WM_KEYDOWN in the same kind of way as I demonstrated
above but with the lparam changed to an int instead of marshalled as a
lpstr. You will also need to know how to set the bits in the lparam
parameter, there is some doco here....
http://msdn.microsoft.com/en-us/library/ms646280(VS.85).aspx

Hope this helps.

Regards Tim.
--
 
Back
Top