C# EM_CHARFROMPOS 如何将一个点转换为 Intptr?

C# EM_CHARFROMPOS how to cast a point to Intptr?

在我使用 GetCaretPos 收到插入符 (x,y) 位置后,我想 SendMessage() of EM_CHARFROMPOS。 问题是 lParam 应该是 IntPtr 类型,而 GetCaretPos 的结果是一个点。 如何正确地将点转换为 IntPtr?

代码必须是different if you target a Rich edit control vs an Edit control,但是你可以从.NET的代码中得到灵感(你可以定义适合你需要的多个版本的SendMessage):

对于文本框: https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/TextBoxBase.cs,1754

...
var pt = (IntPtr)MAKELONG(pt.X, pt.Y);
SendMessage(handle, EM_CHARFROMPOS, 0, pt);
...
public static int MAKELONG(int low, int high) {
  return (high << 16) | (low & 0xffff);
}

[DllImport("user32", CharSet=CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

对于富文本框: https://referencesource.microsoft.com/#system.windows.forms/winforms/Managed/System/WinForms/RichTextBox.cs,2323

...
var pt = new POINT(pt.X, pt.Y);
SendMessage(handle, EM_CHARFROMPOS, 0, pt);
...

[StructLayout(LayoutKind.Sequential)]
public class POINT
{
  public int x;
  public int y;
}

[DllImport("user32", CharSet=CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, POINT lParam);