C# EM_CHARFROMPOS 如何将一个点投向 Intptr?

问题描述 投票:0回答:1

当我使用下面的方法接收到Caret(x,y)的位置后,如何将其投向Intptr?GetCaretPos 我想... SendMessage()EM_CHARFROMPOS.问题是 lParam 应为 IntPtr的结果是 GetCaretPos 是一个点,我如何正确地将这个点转为一个IntPtr?

c# winapi win32com
1个回答
1
投票

这段代码必须是 如果你的目标是一个富编辑控件和一个编辑控件,那么你的目标是不同的。但你可以从.NET的代码中获得灵感(你可以根据自己的需要定义多个版本的SendMessage)。

对于一个文本框:https:/referencesource.microsoft.com#System.Windows.FormswinformsManagedSystemWinFormsTextBoxBase.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.formswinformsManagedSystemWinFormsRichTextBox.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);
© www.soinside.com 2019 - 2024. All rights reserved.