SendMessage API

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

I am trying to use the SendMessage API function using
EM_POSFROMCHAR to get the x,y location of the current
character. I have declared the function and placed the
call into my code. How do I access the x,y coordinates.
Some example code in C# would be greatly appreciated.
 
Steve,

I think you can get away with this by using the following declaration
for SendMessage:

// Note, you have to set the function name, depending on the platform to do
this. You can't
// just use "SendMessage" for the entry point.
[DllImport("user32.dll", EntryPoint="SendMessageA")]
public static extern int SendPosFromCharMessage(
IntPtr hWnd,
[MarshalAs(UnmanagedType.U4)] int Msg,
ref Point wParam,
IntPtr lParam);

The Point structure in .NET has the same layout as the POINTL structure
that the EM_POSFROMCHAR message expects. Also, declaring it this way should
handle the marshaling of the structure automatically.

If you didn't want to use this method, then you would have to declare a
structure and marshal it using the static StructureToPtr method on the
Marshal class and then pass that to the SendMessage function declared with
the wParam and lParam parameters as IntPtr values.

Hope this helps.
 
Nicholas,

thank you for the response. I know this may sound dumb - I
understand how to declare the function (although the
MarshalAs was something I hadn't thought of). My problem
is, I am unsure of how to issue the call and where the
information (point) is for retrieving the information. I
think this may have something to do with my lack of
experience with CALLBACK routines and pointers. Further
explanation of this would be appreciated.
-----Original Message-----
Steve,

I think you can get away with this by using the following declaration
for SendMessage:

// Note, you have to set the function name, depending on the platform to do
this. You can't
// just use "SendMessage" for the entry point.
[DllImport("user32.dll", EntryPoint="SendMessageA")]
public static extern int SendPosFromCharMessage(
IntPtr hWnd,
[MarshalAs(UnmanagedType.U4)] int Msg,
ref Point wParam,
IntPtr lParam);

The Point structure in .NET has the same layout as the POINTL structure
that the EM_POSFROMCHAR message expects. Also, declaring it this way should
handle the marshaling of the structure automatically.

If you didn't want to use this method, then you would have to declare a
structure and marshal it using the static StructureToPtr method on the
Marshal class and then pass that to the SendMessage function declared with
the wParam and lParam parameters as IntPtr values.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Steve said:
I am trying to use the SendMessage API function using
EM_POSFROMCHAR to get the x,y location of the current
character. I have declared the function and placed the
call into my code. How do I access the x,y coordinates.
Some example code in C# would be greatly appreciated.


.
 
Nicholas,
// Note, you have to set the function name, depending on the platform to do
this. You can't
// just use "SendMessage" for the entry point.

Why do you say that?



Mattias
 
Mattias,

I'm going by the examples. In the examples for using the EntryPoint in
the documentation for the DllImport attribute, the EntryPoint property is
set to "SendMessageA". Trying this out myself, however, it would seem the
documentation is misleading.
 
I'm going by the examples. In the examples for using the EntryPoint in
the documentation for the DllImport attribute, the EntryPoint property is
set to "SendMessageA".

Oh, okay.

Trying this out myself, however, it would seem the
documentation is misleading.

Yep, as long as you don't set ExactSpelling=true you should be able to
specify just SendMessage.



Mattias
 
F.Y.I. I ended up using a rich text box and used the
GetPositionFromCharIndex function. This saved me from
using potentially unsafe code.
 
Back
Top