[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

Re: Ok, in that case you just need to use... [DllImport("user32.

noah meyer

5/19/2011 10:56:00 PM

This is great info, Tim. Thanks for your help with the EM_SETSEL.

> On Wednesday, May 21, 2008 7:00 PM Tim Jarvis wrote:

> 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.
> --


>> On Thursday, May 22, 2008 12:34 AM Peter Duniho wrote:

>> On Wed, 21 May 2008 20:09:41 -0700, Jason Zhou <rongyuzhou@gmail.com>
>> 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


>>> On Thursday, May 22, 2008 12:58 AM Tim Jarvis wrote:

>>> 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/librar...(VS.85).aspx
>>>
>>> Hope this helps.
>>>
>>> Regards Tim.
>>> --


>>>> On Thursday, May 22, 2008 1:06 AM Tim Jarvis wrote:

>>>> Peter Duniho wrote:
>>>>
>>>>
>>>> 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.
>>>>
>>>>
>>>> --


>>>>> On Thursday, May 22, 2008 5:09 AM 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!


>>>>>> On Thursday, May 22, 2008 5:09 AM Jason Zhou wrote:

>>>>>> On 5=D4=C222=C8=D5, =C9=CF=CE=E73=CA=B100=B7=D6, "Tim Jarvis" <t...@jarvis.c=
>>>>>> om.au> wrote:
>>>>>>
>>>>>> 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.


>>>>>>> On Wednesday, May 27, 2009 9:31 AM Mira wrote:

>>>>>>> Tim,
>>>>>>>
>>>>>>> Your snippet was very useful for me. Thank you!