Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const EM_GETLINE = &HC4
Private Const EM_CHARFROMPOS = &HD7
Option Explicit
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    Dim pos As Long, lc As Long
    Dim Line As Integer, CharPos As Integer
    Dim str1 As String * 250
    Dim str2 As String
    Dim str3 As String
    
    x = x / Screen.TwipsPerPixelX
    y = y / Screen.TwipsPerPixelY
    pos = x + y * 65536
    lc = SendMessage(Text1.hwnd, EM_CHARFROMPOS, 0, ByVal pos)
    
    Line = lc \ 65536 '这里不得到的是行号?
    CharPos = lc Mod 65536
    
    MsgBox "第几行 = " & Line & vbCrLf & "第几个字符= " & CharPos
   ' 根据MSDN对EM-GETLINE的说明
  ' EM_GETLINE
'wParam = (WPARAM) line;          // line number to retrieve 找到的行号
'lParam = (LPARAM) (LPCSTR) lpch; // address of buffer for line行的内存地址
SendMessage Text1.hwnd, EM_GETLINE, lc, ByVal str1
str3 = StrConv(str1, vbUnicode)
str2 = Left(str3, InStr(1, str3, Chr(0) - 1))
MsgBox str2End Sub '我知道白把str1(230)这样的行,但在这样为什么就不行?lc得到了行号,我直接用不就行了吗?

解决方案 »

  1.   

    楼主,你存放行号的变量不是“Line”吗?怎么又变成了lc???
      

  2.   

    你的定义错了,参考我写的这个代码:
    Option Explicit
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Const EM_GETLINE = &HC4Private Function GetLine(Text1 As TextBox, ByVal ntx As Long) As String
            Dim str1(255) As Byte '如果您的字串 > 255 byte请自行增加该Byte Array
            Dim str2 As String, i As Long
            
            str1(0) = 255 '字串的前两个Byte存该字串的最大长度
            str1(0) = 255
            i = SendMessage(Text1.hwnd, EM_GETLINE, ntx, str1(0))
            If i = 0 Then
                GetLine = ""
            Else
                str2 = StrConv(str1, vbUnicode)
                GetLine = Left(str2, InStr(1, str2, Chr(0)) - 1)
            End If
    End FunctionPrivate Sub Command1_Click()
            Dim str1 As String
            str1 = GetLine(Text1, 2) '取得第二行的字串,以0为基底
            Debug.Print str1
            
    End SubPrivate Sub Form_Load()
            Text1.Text = "中华人民共和国0" & vbCrLf & "中华人民共和国1" & vbCrLf & "中华人民共和国2" & vbCrLf & "中华人民共和国3"
            
    End Sub