如何获得textbox中光标的位置?
就是光标所在的坐标如何获得?

解决方案 »

  1.   

    textBox1.GetPositionFromCharIndex(textBox1.SelectionStart);
    //最后一个位置需处理一下
      

  2.   

    RichTextBox.GetPositionFromCharIndex
    textBox.?
      

  3.   

    以前在网上看到的代码,你试试:
    const int EM_GETSEL = 0xB0;
    const int EM_LINEFROMCHAR = 0xC9;
    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 void GetCaretPos(int TextHwnd, ref int LineNo, ref int ColNo)
    {
    int i = 0, j = 0, lParam = 0, wParam = 0, k = 0;
    i = SendMessage(TextHwnd, EM_GETSEL, wParam, ref lParam);
    j = i / 65536;
    LineNo = SendMessage(TextHwnd, EM_LINEFROMCHAR, j, ref lParam) + 1;
    k = SendMessage(TextHwnd, EM_LINEINDEX, -1, ref lParam);
    ColNo = j - k + 1;
    }