基本介紹
- 外文名:VB SendMessage
- 函式原型:LRESULT SendMessage
- 參數:視窗程式將接收訊息的視窗的句柄
- Msg:指定被傳送的訊息
函式原型,函式套用,舉例,
函式原型
LRESULT SendMessage(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM IParam)
參數:
hWnd:其視窗程式將接收訊息的視窗的句柄。如果此參數為HWND_BROADCAST,則訊息將被傳送到系統中所有頂層視窗,包括無效或不可見的非自身擁有的視窗、被覆蓋的視窗和彈出式視窗,但訊息不被傳送到子視窗。
Msg:指定被傳送的訊息。
wParam:指定附加的訊息特定信息。
IParam:指定附加的訊息特定信息。
返回值:返回值指定訊息處理的結果,依賴於所傳送的訊息。
函式套用
可以模擬滑鼠/鍵盤事件,製作遊戲類的掛機腳本,運用此函式可以不需要將遊戲擺到活動視窗(當前視窗),但是此函式傳遞的訊息被一些遊戲所禁止。
舉例
'--------------------------------'擴展文本框功能PublicConstEM_GETSEL=&HB0PublicConstEM_LINEFROMCHAR=&HC9PublicConstEM_LINEINDEX=&HBBPublicConstEM_GETLINE=&HC4PublicConstEM_GETLINECOUNT=&HBAPublicDeclareFunctionSendMessageLib"user32"Alias"SendMessageA"(ByValhWndAsLong,ByValwMsgAsLong,ByValwParamAsLong,ByVallParamAsLong)AsLongPublicDeclareFunctionSendMessagesLib"user32"Alias"SendMessageA"(ByValhWndAsLong,ByValwMsgAsLong,ByValwParamAsLong,ByVallParamAsAny)AsLong'獲得文本的行數PublicFunctionGetTextLines(txtHwndAsLong)AsLongGetTextLines=SendMessage(txtHwnd,EM_GETLINECOUNT,0,0)EndFunction'功能描述:獲得指定文本的游標PublicSubGetCaretPos(ByValTextHwndAsLong,LineNoAsLong,ColNoAsLong)DimiAsLong,jAsLongDimlParamAsLong,wParamAsLongDimkAsLong'首先向文本框傳遞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+1EndSub'功能描述:獲得指定行的文本PublicFunctionReadLine(ByValTextHwndAsLong,intLineAsLong)AsStringDimm_sLineStringAsStringDimm_intRetAsLongm_sLineString=Space$(1056)m_intRet=SendMessages(TextHwnd,EM_GETLINE,intLine,ByValm_sLineString)ReadLine=Left(m_sLineString,m_intRet)EndFunction'獲得游標處的字元PublicFunctionGetWord(ByValTextHwndAsLong)AsString'打開錯誤處理陷阱OnErrorGoToErrGoto'---------------------------------'代碼正文DimLineNoAsLongDimColNoAsLongDimstrDataAsStringDimiAsIntegerDimintAscAsIntegerDimintBeginAsInteger,intEndAsIntegerGetCaretPosTextHwnd,LineNo,ColNostrData=ReadLine(TextHwnd,LineNo-1)'---------------------------'修正含有漢字的列數intCharNum=0Fori=0ToLen(strData)-1IfAsc(Mid(strData,i+1,1))<0ThenintCharNum=intCharNum+2ElseintCharNum=intCharNum+1EndIfIfintCharNum>=ColNoThenExitForEndIfNextiColNo=i+1'-----------------------------IfLen(strData)>0ThenFori=ColNo-1To1Step-1intAsc=Asc(Mid(strData,i,1))IfNot((intAsc>=Asc("a")AndintAsc<=Asc("z"))Or(intAsc>=Asc("A")AndintAsc<=Asc("Z"))Or(intAsc>=Asc("0")AndintAsc<=Asc("9"))OrintAsc=Asc("_"))ThenintBegin=i+1ExitForEndIfNextiFori=ColNoToLen(strData)intAsc=Asc(Mid(strData,i,1))IfNot((intAsc>=Asc("a")AndintAsc<=Asc("z"))Or(intAsc>=Asc("A")AndintAsc<=Asc("Z"))Or(intAsc>=Asc("0")AndintAsc<=Asc("9"))OrintAsc=Asc("_"))ThenintEnd=i-1ExitForEndIfNextiIfintBegin<=0ThenintBegin=1IfintEnd<=0ThenintEnd=Len(strData)IfintEnd>intBeginThenGetWord=Trim(Mid(strData,intBegin,intEnd-intBegin+1))ElseGetWord=""EndIfElseGetWord=""EndIf'----------------------ExitFunction'----------------------ErrGoto:GetWord=""EndFunction