请输入您要查询的百科知识:

 

词条 VB SendMessage
释义

VB中的SendMessage函数作用是将指定的消息发送到一个或多个窗口。此函数为指定的窗口调用窗口程序,直到窗口程序处理完消息再返回。而函数PostMessage不同,将一个消息寄送到一个线程的消息队列后立即返回。

函数原型

LRESULT SendMessage(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM IParam)

参数:

hWnd:其窗口程序将接收消息的窗口的句柄。如果此参数为HWND_BROADCAST,则消息将被发送到系统中所有顶层窗口,包括无效或不可见的非自身拥有的窗口、被覆盖的窗口和弹出式窗口,但消息不被发送到子窗口。

Msg:指定被发送的消息。

wParam:指定附加的消息特定信息。

IParam:指定附加的消息特定信息。

返回值:返回值指定消息处理的结果,依赖于所发送的消息。

函数应用

可以模拟鼠标/键盘事件,制作游戏类的挂机脚本,运用此函数可以不需要将游戏摆到活动窗口(当前窗口),但是此函数传递的消息被一些游戏所屏蔽。

概述

'--------------------------------

'扩展文本框功能

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

随便看

 

百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。

 

Copyright © 2004-2023 Cnenc.net All Rights Reserved
更新时间:2025/4/8 19:53:07