请教sendmessage的用法

andysonglei 2003-09-10 03:45:23
谁能告诉我sendmessage的用法,特别是gettext的用法,谢谢
...全文
109 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
liul17 2003-09-10
  • 打赏
  • 举报
回复
'--------------------------------
'扩展文本框功能
Public Const EM_GETSEL = &HB0
Public Const EM_LINEFROMCHAR = &HC9
Public Const EM_LINEINDEX = &HBB
Public Const EM_GETLINE = &HC4
Public Const EM_GETLINECOUNT = &HBA
Public Declare Function SendMessage 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 SendMessages Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Any) As Long

'获得文本的行数
Public Function GetTextLines(txtHwnd As Long) As Long
GetTextLines = SendMessage(txtHwnd, EM_GETLINECOUNT, 0, 0)
End Function
'功能描述:获得指定文本的光标
Public Sub GetCaretPos(ByVal TextHwnd As Long, LineNo As Long, ColNo As Long)
Dim i As Long, j As Long
Dim lParam As Long, wParam As Long
Dim k As Long

'首先向文本框传递EM_GETSEL消息以获取从起始位置到
'光标所在位置的字符数
i = SendMessage(TextHwnd, EM_GETSEL, wParam, lParam)
j = i / 2 ^ 16
'再向文本框传递EM_LINEFROMCHAR消息根据获得的字符
'数确定光标以获取所在行数
LineNo = SendMessage(TextHwnd, EM_LINEFROMCHAR, j, 0)
LineNo = LineNo + 1
'向文本框传递EM_LINEINDEX消息以获取所在列数
k = SendMessage(TextHwnd, EM_LINEINDEX, -1, 0)
ColNo = j - k + 1
End Sub
'功能描述:获得指定行的文本
Public Function ReadLine(ByVal TextHwnd As Long, intLine As Long) As String
Dim m_sLineString As String
Dim m_intRet As Long

m_sLineString = Space$(1056)
m_intRet = SendMessages(TextHwnd, EM_GETLINE, intLine, ByVal m_sLineString)
ReadLine = Left(m_sLineString, m_intRet)
End Function
'获得光标处的字符
Public Function GetWord(ByVal TextHwnd As Long) As String
'打开错误处理陷阱
On Error GoTo ErrGoto
'---------------------------------
'代码正文
Dim LineNo As Long
Dim ColNo As Long
Dim strData As String
Dim i As Integer
Dim intAsc As Integer
Dim intBegin As Integer, intEnd As Integer

GetCaretPos TextHwnd, LineNo, ColNo
strData = ReadLine(TextHwnd, LineNo - 1)
'---------------------------
'修正含有汉字的列数
intCharNum = 0
For i = 0 To Len(strData) - 1
If Asc(Mid(strData, i + 1, 1)) < 0 Then
intCharNum = intCharNum + 2
Else
intCharNum = intCharNum + 1
End If

If intCharNum >= ColNo Then
Exit For
End If
Next i
ColNo = i + 1
'-----------------------------
If Len(strData) > 0 Then
For i = ColNo - 1 To 1 Step -1
intAsc = Asc(Mid(strData, i, 1))
If Not ((intAsc >= Asc("a") And intAsc <= Asc("z")) Or (intAsc >= Asc("A") And intAsc <= Asc("Z")) Or (intAsc >= Asc("0") And intAsc <= Asc("9")) Or intAsc = Asc("_")) Then
intBegin = i + 1
Exit For
End If
Next i
For i = ColNo To Len(strData)
intAsc = Asc(Mid(strData, i, 1))
If Not ((intAsc >= Asc("a") And intAsc <= Asc("z")) Or (intAsc >= Asc("A") And intAsc <= Asc("Z")) Or (intAsc >= Asc("0") And intAsc <= Asc("9")) Or intAsc = Asc("_")) Then
intEnd = i - 1
Exit For
End If
Next i

If intBegin <= 0 Then intBegin = 1
If intEnd <= 0 Then intEnd = Len(strData)
If intEnd > intBegin Then
GetWord = Trim(Mid(strData, intBegin, intEnd - intBegin + 1))
Else
GetWord = ""
End If
Else
GetWord = ""
End If
'----------------------
Exit Function
'----------------------
ErrGoto:
GetWord = ""
End Function
andysonglei 2003-09-10
  • 打赏
  • 举报
回复
果然是高手,万分感谢
lilaclone 2003-09-10
  • 打赏
  • 举报
回复
'向该窗口发送一个WM_GETTEXT消息,以获得该窗口的文本,并显示在PasswordText文本框中
  tempstr = Space(255)
  strlong = Len(tempstr)
  rtn = Send Message(curwnd, WM _GETTEXT, strlong, tempstr)
  tempstr = Trim(tempstr)
  PasswordText.Text = tempstr
  End If
  End Sub
  Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
  If IsDragging = True Then
  Screen.MousePointer = vbStandard
  IsDragging = False
   '释放鼠标消息的抓取
   ReleaseCapture
  End If
  End Sub
  Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  If IsDragging = False Then
  IsDragging = True
  Screen.MouseIcon = LoadPicture(App.Path + “Cursor1.cur")
  Screen.MousePointer = vbCustom
  '将以后的鼠标输入消息都发送到本程序窗口
  SetCapture (Mainfrm.hwnd)
  End If
  End Sub
lilaclone 2003-09-10
  • 打赏
  • 举报
回复
给你贴个例子看看,里面有gettext用法:

如何在VB中实现密码的读取
首先,建立一个标准EXE工程,窗体名称为Mainfrm,并将ScaleMode设为3-Pixel。在窗体上放置四个Label控件和四个名称依次为PointText、hWndText、WndClassText和PasswordText的TextBox控件,再放置一个名称为Check1,Caption属性为 “总在最上面”的CheckBox控件和一个名称为Picture1的PictureBox控件。整个窗口的布局如图所示。为了程序外观的美化,在Picture1中放置一幅合适的图片,并制作一个相应的光标,将光标文件命名为Cursor1.cur并存在源程序所在的目录下。
  其次,新建一个类模块,在其中输入如下内容:
  Option Explicit
  Declare Function SetWindowPos& Lib“user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
  Declare Function SetCapture Lib “user32" (ByVal hwnd As Long) As Long Declare Function ReleaseCapture Lib “user32" () As Long
  Declare Function ClientToScreen Lib “user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long Declare Function WindowFromPoint Lib “user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long Declare Function GetClassName Lib “user32" Alias “GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long Declare Function GetLastError Lib “kernel32" () As Long Declare Function SendMessage Lib “user32" Alias “SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
  Public Const WM_GETTEXT = &HD
  Type POINTAPI
  x As Long
  y As Long
  End Type
  注意函数SendMessage的声明中最后一个变量并不是如API浏览器所提供的lParam As Any,而是ByVal lParam As String或Byval lParam As Any,否则你的程序会毫无疑问地崩溃。
  最后,键入主程序原代码如下:
  Dim IsDragging As Boolean '是否处在拖动读取状态
  Private Sub SetOnTop(ByVal IsOnTop As Integer)
  Dim rtn As Long
  If IsOnTop = 1 Then
  rtn = SetWindowPos(Mainfrm.hwnd, -1, 0, 0, 0, 0, 3) '将窗口置于最上面
  Else
  rtn = SetWindowPos(Mainfrm.hwnd, -2, 0, 0, 0, 0, 3)
  End If
  End Sub
  Private Sub Check1_Click()
  SetOnTop (Check1.Value)
  End Sub
  Private Sub Form_Load()
  Check1.Value = 1
  SetOnTop (Check1.Value)
  IsDragging = False
  End Sub
  Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
  If IsDragging = True Then
  Dim rtn As Long, curwnd As Long
  Dim tempstr As String
  Dim strlong As Long
  Dim point As POINTAPI
  point.x = x
  point.y = y
  '将客户坐标转化为屏幕坐标并显示在PointText文本框中
  If ClientToScreen(Mainfrm.hwnd, point) = 0 Then Exit Sub
  PointText.Text = Str(point.x) + “," + Str(point.y)
  '获得鼠标所在的窗口句柄并显示在hwndtext文本框中
  curwnd = WindowFromPoint(point.x, point.y)
  hwndtext.Text = Str(curwnd)
  '获得该窗口的类型并显示在WndClassText文本框中
  tempstr = Space(255)
  strlong = Len(tempstr)
  rtn = GetClassName(curwnd, tempstr, strlong)
  If rtn = 0 Then Exit Sub
  tempstr = Trim(tempstr)
  WndClassText.Text = tempstr

1,486

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧