SendMessage and Handles

S

Steve Barnett

I have an app that includes two side by side Rich Text controls. What I need
to do is keep both controls synchronised, such that if I scroll the left rtb
up or down then the right one scrolls by the same amount in the same
direction.

In my simplistic mind, I believe that this can be achieved by sending an
EM_GetScrollPos message to the left rtb and using the results to send an
EM_SetScrollPos message to the right one. Problem is that I'm converting
over from VB6 and have no idea how to

a) Call the SendMessage API,
b) Code a "POINT" structure in C# for use in the call, or
c) Find the windows handle of the Rich Text box.

All of which means that I have a very long way to go to achieve a very
simple function. Can anyone please point me at some code that does this
please? Better still, can I achieve this in native C# code?

Thanks
Steve
 
M

Mattias Sjögren

Steve,
a) Call the SendMessage API,

You first declare it like this

[DllImport("user32.dll", CharSet=CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, uint msg, IntPtr wParam,
ref Point lParam);

then call it like any other method.

b) Code a "POINT" structure in C# for use in the call, or

You can use System.Drawing.Point in its place.

c) Find the windows handle of the Rich Text box.

Each control has a Handle property that returns it.


Mattias
 
N

Nicholas Paldino [.NET/C# MVP]

Steve,

See inline:
a) Call the SendMessage API,

Check out http://www.pinvoke.net. It will have the declaration of
SendMessage. You might have to tweak it for your specific message. In the
case of SetScrollPos, you will want to use this:

[DllImport("user32.dll", CharSet = CharSet.Auto, EntryPoint = "SendMessage",
ExactSpelling = false)]
static extern int SendEmSetScrollPosMessage(
IntPtr hWnd,
[MarshalAs(UnmanagedType.U4)]
int Msg,
IntPtr wParam,
ref System.Drawing.Point lParam);
b) Code a "POINT" structure in C# for use in the call, or

You can use System.Drawing.Point. It will marshal correctly.
c) Find the windows handle of the Rich Text box.

The RichTextBox control exposes a Handle property which you can use to
get the handle. Actually, the Control class exposes it, so all controls
expose their handle.

Hope this helps.
 
S

Steve Barnett

Much appreciated guys.

Steve

Nicholas Paldino said:
Steve,

See inline:
a) Call the SendMessage API,

Check out http://www.pinvoke.net. It will have the declaration of
SendMessage. You might have to tweak it for your specific message. In
the case of SetScrollPos, you will want to use this:

[DllImport("user32.dll", CharSet = CharSet.Auto, EntryPoint =
"SendMessage", ExactSpelling = false)]
static extern int SendEmSetScrollPosMessage(
IntPtr hWnd,
[MarshalAs(UnmanagedType.U4)]
int Msg,
IntPtr wParam,
ref System.Drawing.Point lParam);
b) Code a "POINT" structure in C# for use in the call, or

You can use System.Drawing.Point. It will marshal correctly.
c) Find the windows handle of the Rich Text box.

The RichTextBox control exposes a Handle property which you can use to
get the handle. Actually, the Control class exposes it, so all controls
expose their handle.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
All of which means that I have a very long way to go to achieve a very
simple function. Can anyone please point me at some code that does this
please? Better still, can I achieve this in native C# code?

Thanks
Steve
 

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