我这里有个方法,是从网上找的。
#region 获取光标位置代码
private const int EM_GETSEL = 0xB0;
private const int EM_LINEFROMCHAR = 0xC9;
private const int EM_LINEINDEX = 0xBB;
[DllImport("user32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(int hwnd,int wMsg,int wParam,ref int lParam);
private Point GetCursorPos(int TextHwnd)
{
int i = 0, j = 0, k = 0;
int lParam = 0, wParam = 0;
i = SendMessage(TextHwnd, EM_GETSEL, wParam, ref lParam);
j = i / 65536;
int lineNo = SendMessage(TextHwnd, EM_LINEFROMCHAR, j, ref lParam) + 1;
k = SendMessage(TextHwnd, EM_LINEINDEX, -1, ref lParam);
int colNo = j - k + 1;
Point ret = new Point(lineNo, colNo);
return ret;
}
#endregion
这里返回一个point。然后下面:
Point p = GetCursorPos(this.richTextBox1.Handle.ToInt32());
this.statusStrip1.Items[2].Text = string.Format("第{0}行, 第{1}列", p.X, p.Y);