Jason Zhou
5/22/2008 3:10:00 AM
On 5?22?, ??3?00?, "Tim Jarvis" <t...@jarvis.com.au> wrote:
> Jason Zhou wrote:
> > 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.