C# 如何在C中将EM_CHARFROMPOS与RichTextBox一起使用#
我试图使用以下代码在RichTextBox中按位置检索字符索引。我知道我可以使用RichTextBox类提供的GetCharIndexFromPosition方法,但我想知道以下代码有什么问题: SendMessage导入是这样的:C# 如何在C中将EM_CHARFROMPOS与RichTextBox一起使用#,c#,winapi,richtextbox,C#,Winapi,Richtextbox,我试图使用以下代码在RichTextBox中按位置检索字符索引。我知道我可以使用RichTextBox类提供的GetCharIndexFromPosition方法,但我想知道以下代码有什么问题: SendMessage导入是这样的: [DllImport("User32.dll", EntryPoint = "SendMessage", CharSet= CharSet.Auto)] public static extern IntPtr SendMessage(IntPtr hWnd, int
[DllImport("User32.dll", EntryPoint = "SendMessage", CharSet= CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, ref POINTL lParam);
然后这个电话:
int returnVal = (int)WinUser.SendMessage(this._textBox.Handle, (int)WinUser.Message.EM_CHARFROMPOS, 0, ref p);
其中p是包含屏幕坐标的POINTL结构实例,其中RichTextBox的左上角为原点
POINTL结构定义为
[StructLayout(LayoutKind.Sequential)]
public struct POINTL
{
public long x;
public long y;
}
public override int GetCharIndexFromPosition(Point pt) {
NativeMethods.POINT wpt = new NativeMethods.POINT(pt.X, pt.Y);
int index = (int)UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), NativeMethods.EM_CHARFROMPOS, 0, wpt);
string t = this.Text;
// EM_CHARFROMPOS will return an invalid number if the last character in the RichEdit
// is a newline.
//
if (index >= t.Length) {
index = Math.Max(t.Length - 1, 0);
}
return index;
}
[StructLayout(LayoutKind.Sequential)]
public class POINT {
public int x;
public int y;
public POINT() {
}
public POINT(int x, int y) {
this.x = x;
this.y = y;
}
#if DEBUG
public override string ToString() {
return "{x=" + x + ", y=" + y + "}";
}
#endif
}
点Lp已初始化为:
WinUser.POINTL p;
p.x = 0;
p.y = 0;
现在的问题是:
如果p按照上面给出的方式初始化,则返回值为0
如果p是{x=10,y=10}或{x=1,y=1}之类的值,则returnVal为1
在这两种情况下,函数GetCharIndexFromPosition都提供了正确的索引。将
long
更改为int
(Win32
LONG
s是与.Netint
s相对应的32位整数)
.Net的GetCharIndexFromPosition
方法定义为
[StructLayout(LayoutKind.Sequential)]
public struct POINTL
{
public long x;
public long y;
}
public override int GetCharIndexFromPosition(Point pt) {
NativeMethods.POINT wpt = new NativeMethods.POINT(pt.X, pt.Y);
int index = (int)UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), NativeMethods.EM_CHARFROMPOS, 0, wpt);
string t = this.Text;
// EM_CHARFROMPOS will return an invalid number if the last character in the RichEdit
// is a newline.
//
if (index >= t.Length) {
index = Math.Max(t.Length - 1, 0);
}
return index;
}
[StructLayout(LayoutKind.Sequential)]
public class POINT {
public int x;
public int y;
public POINT() {
}
public POINT(int x, int y) {
this.x = x;
this.y = y;
}
#if DEBUG
public override string ToString() {
return "{x=" + x + ", y=" + y + "}";
}
#endif
}
NativeMethods.POINT
类型定义为
[StructLayout(LayoutKind.Sequential)]
public struct POINTL
{
public long x;
public long y;
}
public override int GetCharIndexFromPosition(Point pt) {
NativeMethods.POINT wpt = new NativeMethods.POINT(pt.X, pt.Y);
int index = (int)UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), NativeMethods.EM_CHARFROMPOS, 0, wpt);
string t = this.Text;
// EM_CHARFROMPOS will return an invalid number if the last character in the RichEdit
// is a newline.
//
if (index >= t.Length) {
index = Math.Max(t.Length - 1, 0);
}
return index;
}
[StructLayout(LayoutKind.Sequential)]
public class POINT {
public int x;
public int y;
public POINT() {
}
public POINT(int x, int y) {
this.x = x;
this.y = y;
}
#if DEBUG
public override string ToString() {
return "{x=" + x + ", y=" + y + "}";
}
#endif
}
这起作用了。如何看待.NET中函数的实际实现。就像你在这里给的那个?