多行文本框中选择文本行

多行文本框中选择文本行

 

   Option Explicit 
   Private Const EM_LINELENGTH = &HC1
   Private Const EM_LINEINDEX = &HBB
   Private Const EM_SETSEL = &HB1
  
   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 Sub Form_Load()
    Text1.Text = "aaaaaaa" & vbCrLf & _
                 "bbbbbbbbb" & vbCrLf & _
                 "ccccccccccc" & vbCrLf & _
                 "ddddddddddddd"
    SEL_Line Text1.hWnd, 2, 2    '第2行起选择2行
End Sub

Sub SEL_Line(ByVal hWnd As Long, ByVal whichLine As Long, ByVal howLine As Long)
'=============================chinaboyzyq 2010-04-16============================
'第一个参数是文本框的句柄
'第二个参数是从第几行开始选择(1为第一行)
'第三个参数是选择几行
'===============================================================================
    whichLine = whichLine - 1
    If whichLine < 0 Then whichLine = 0
    Dim length As Long, lc As Long, StartLine As Long, i As Long
   
    For i = whichLine To whichLine + howLine - 1
        lc = SendMessage(hWnd, EM_LINEINDEX, i, ByVal 0&)
        If i = whichLine Then StartLine = lc
        length = length + SendMessage(hWnd, EM_LINELENGTH, lc, ByVal 0&) + 2
    Next
    SendMessage Text1.hWnd, EM_SETSEL, StartLine, ByVal (StartLine + length)
       
End Sub

你可能感兴趣的:(vb6)