数组或者结构体能否定义为对象?
在网上看到用API函数读取文件任意行代码的例子,都是用SENDMESSAGE发送EM_GETLINECOUNT消息和EM_GETLINE 消息到TEXTBOX进行显示,是否必须这样做?我想将消息直接传到数组或结构体进行操作,谁能帮我修改一下?
谢谢
下面是从其他地方摘录的一个API示例
添加 Command1 Command2 Text1 Text2 Label1
'在Text2输入欲读取第几行的行号,再点击Command1
'Command2是倒数第二行的内容
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
Const EM_GETLINECOUNT = &HBA
Const EM_GETLINE = &HC4
Dim linecnt As Integer, i As Long, j As Long
Private Sub Form_Load()
Open "c:\test.txt" For Binary As #1
Text1.Text = StrConv(InputB(LOF(1), 1), vbUnicode)
Close #1
linecnt = TextBoxLineCnt(Text1) '本文件总行数
Text2.Text = "1"
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
If Val(Text2.Text) > 0 and Val(Text2.Text) < linecnt Then
command1_click
End If
End If
End Sub
Private Sub Command1_Click()
Dim str1 As String
i = CInt(Text2.Text) - 1
str1 = getlinetext(Text1, i) '取得Text2中的指定行的内容
Label1.Caption = "第 " & Trim(Text2.Text) & " 行的内容是:" & vbCrLf & Chr(10) & str1
End Sub
Private Sub Command2_Click() '倒数第二行
Dim str1 As String
If linecnt >= 2 Then
j = linecnt - 2 '第一行是0,最后一行是Linecnt - 1
str1 = getlinetext(Text1, j)
Label1.Caption = "倒数第二行的内容是:" & vbCrLf & Chr(10) & str1
End If
End Sub
Public Function getlinetext(Text1 As TextBox, ByVal ntx As Long) As String
Dim str1(255) As Byte
Dim str2 As String
str1(0) = 255
i = SendMessage(Text1.hwnd, EM_GETLINE, ntx, str1(0))
If i = 0 Then
getlinetext = ""
Else
str2 = StrConv(str1, vbUnicode)
getlinetext = Left(str2, InStr(1, str2, Chr(0)) - 1)
End If
End Function
Public Function TextBoxLineCnt(ctl As TextBox) As Long
TextBoxLineCnt = SendMessage(ctl.hwnd, EM_GETLINECOUNT, 0, 0)
End Function