在VB6里面的API函数SendMessage调用,放在VB2005里面应如何实现
在VB6里面是
Public Declare Function SendMessage Lib "user32 " Alias "SendMessageW " (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function SendMessageByRef Lib "user32 " Alias "SendMessageA " (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function SendMessageStr Lib "user32 " Alias "SendMessageA " (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
'特定字符所在行号
Public Const EM_LINEFROMCHAR = &HC9
'第一行的字符
Public Const EM_LINEINDEX = &HBB
'取得当前光标所在点的行号
Public Const EM_GETLINE = &HC4
'取得当前光标所在点的列号
Public Const EM_GETSEL = &HB0
'文本框中的行数
Public Const EM_GETLINECOUNT = &HBA
'取得文本框中的行数
Public Function LineCount(txtA As RichTextBox) As Long
LineCount = SendMessage(txtA.hwnd, _
EM_GETLINECOUNT, 0&, 0&)
End Function
'判断是否是数字
Public Function IsDigit(strData As String) As Boolean
IsDigit = False
IsDigit = IIf(IsNumeric(strData), True, False)
End Function
'取得当前行内容
Public Function GetTextRow(txtA As RichTextBox) As String
Dim astr As String
Dim l, iLineY As Long
astr = Space(1024)
l = SendMessage(txtA.hwnd, EM_LINEINDEX, -1, 0)
iLineY = SendMessage(txtA.hwnd, EM_LINEFROMCHAR, l, 0)
l = SendMessageStr(txtA.hwnd, EM_GETLINE, iLineY, ByVal astr)
astr = Trim(astr)
GetTextRow = Left$(astr, Len(astr) - 3)
End Function
'取得当前行列号
Public Function GetCurPos(txtA As RichTextBox) As Long
Dim iLineX, iLineY As Long
Dim l, l1, l2 As Long
Dim astr As String * 256
l = SendMessage(txtA.hwnd, EM_LINEINDEX, -1, 0)
iLineY = SendMessage(txtA.hwnd, EM_LINEFROMCHAR, l, 0)
SendMessageByRef txtA.hwnd, EM_GETSEL, l1, l2
iLineX = l1 - l
' Label1.Caption = "列: " + Str(iLineX)
' Label2.Caption = "行: " + Str(iLineY)
GetCurPos = iLineY
End Function
'取得当前行内容
Public Function GetSelTextRow(iRowNow As Integer, txtA As RichTextBox) As String
Dim astr As String
Dim l, iLineY As Long
iLineY = iRowNow
astr = Space(1024)
l = SendMessageStr(txtA.hwnd, EM_GETLINE, iLineY, ByVal astr)
astr = Trim(astr)
GetSelTextRow = Left$(astr, Len(astr) - 3)
End Function
然后放到VB2005里面做了如下修改
Public Declare Function SendMessage Lib "user32 " Alias "SendMessageW " (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function SendMessageByRef Lib "user32 " Alias "SendMessageA " (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function SendMessageStr Lib "user32 " Alias "SendMessageA " (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
'特定字符所在行号
Public Const EM_LINEFROMCHAR = &HC9
'第一行的字符
Public Const EM_LINEINDEX = &HBB
'取得当前光标所在点的行号
Public Const EM_GETLINE = &HC4
'取得当前光标所在点的列号
Public Const EM_GETSEL = &HB0
'文本框中的行数
Public Const EM_GETLINECOUNT = &HBA
'取得文本框中的行数
Public Function LineCount(ByVal txtA As RichTextBox) As Long
LineCount = SendMessage(txtA.Handle, EM_GETLINECOUNT, 0&, 0&)
End Function
'判断是否是数字
Public Function IsDigit(ByVal strData As String) As Boolean
IsDigit = False
IsDigit = IIf(IsNumeric(strData), True, False)
End Function
'取得当前行内容
Public Function GetTextRow(ByVal txtA As RichTextBox) As String
Dim astr As String
Dim l, iLineY As Long
astr = Space(1024)
l = SendMessage(txtA.Handle, EM_LINEINDEX, -1, 0)
iLineY = SendMessage(txtA.Handle, EM_LINEFROMCHAR, l, 0)
l = SendMessageStr(txtA.Handle, EM_GETLINE, iLineY, astr)
astr = Trim(astr)
GetTextRow = Microsoft.VisualBasic.Left(astr, Len(astr) - 3)
End Function
'取得当前行列号
Public Function GetCurPos(ByVal txtA As RichTextBox) As Long
Dim iLineX, iLineY As Long
Dim l, l1, l2 As Long
Dim astr As New VB6.FixedLengthString(256)
l = SendMessage(txtA.Handle, EM_LINEINDEX, -1, 0)
iLineY = SendMessage(txtA.Handle, EM_LINEFROMCHAR, l, 0)
SendMessageByRef(txtA.Handle, EM_GETSEL, l1, l2)
iLineX = l1 - l
'Label1.text = "列: " + Str(iLineX)
'Label2.text = "行: " + Str(iLineY)
GetCurPos = iLineY
End Function
'取得当前行内容
Public Function GetSelTextRow(ByVal iRowNow As Integer, ByVal txtA As RichTextBox) As String
Dim astr As String
Dim iLineY As Long
Dim l As Long
iLineY = iRowNow
astr = Space(1024)
l = SendMessageStr(txtA.Handle, EM_GETLINE, iLineY, astr)
astr = Trim(astr)
GetSelTextRow = Microsoft.VisualBasic.Left(astr, Len(astr) - 3)
End Function
End Module
但是程序调用运行不了,不知道是哪里不对了,望大虾们给解答..小女子感激不尽...
[解决办法]
long统统改成integer,其他照搬即可
[解决办法]
看你的代码好像为了得到文本框的光标所在坐标(行,列),我提供另一个可行办法,在vs2005你直接复制就地了。
声明部分:(以后api都要像我这样声明。。。vb6的long是32位整数,在vb8相对应的是ineteger或者int32)
<Runtime.InteropServices.DllImport( "user32 ")> _
Public Shared Function GetCaretPos(ByRef lpPoint As System.Drawing.Point) As Boolean
End Function
使用:
Sub DoGetPos()
Dim P As New Point(0)
dim x,y as long
GetCaretPos(P)
Dim Pos As Integer = richtextbox1.GetCharIndexFromPosition(P)
y = richtextbox1.GetLineFromCharIndex(Pos)
If y > 0 Then
Dim offset As Integer = 1
While richtextbox1.Text(Pos - offset) <> Chr(10)
offset += 1
End While
x = offset
y += 1
Else
x = Pos + 1
y = 1
End If
Label1.Text = "行,列: " & y.ToString & ", " & x.ToString
End Sub