获取ToolTipText内容
本篇内容提供一个获取ToolTipText内容的通用模块,并提供一个使用范例。
请按以下四个步骤进行:
一、把下面内容保存为Project1.vbp
Type=Exe
Form=Form1.frm
Module=mTray; mTray.bas
IconForm=\"Form1\"
Startup=\"Form1\"
HelpFile=\"\"
Title=\"Project1\"
ExeName32=\"GetToolTipText.exe\"
Command32=\"\"
Name=\"Project1\"
HelpContextID=\"0\"
CompatibleMode=\"0\"
MajorVer=1
MinorVer=0
RevisionVer=0
AutoIncrementVer=0
ServerSupportFiles=0
VersionCompanyName=\"999-999-999\"
CompilationType=0
OptimizationType=0
FavorPentiumPro(tm)=0
CodeViewDebugInfo=0
NoAliasing=0
BoundsCheck=0
OverflowCheck=0
FlPointCheck=0
FDIVCheck=0
UnroundedFP=0
StartMode=0
Unattended=0
Retained=0
ThreadPerObject=0
MaxNumberOfThreads=1
[MS Transaction Server]
AutoRefresh=1
二、把下面内容保存为Form1.frm
VERSION 5.00
Begin VB.Form Form1
BorderStyle Caption ClientHeight ClientLeft ClientTop ClientWidth LinkTopic MaxButton MinButton ScaleHeight = 3 'Fixed Dialog
= \"获取ToolTipText内容\"
= 3510
45
= 330
= 3525
= \"Form1\"
= 0 'False
= 0 'False
= 3510
=
ScaleWidth = 3525
ShowInTaskbar = 0 'False
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command1
Caption = \"获取xp“开始”按钮提示内容\"
Height = 1215
Left = 840
TabIndex = 0
Top = 1920
Width = 1815
End
End
Attribute VB_Name = \"Form1\"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Declare Function FindWindow Lib \"user32\" Alias \"FindWindowA\" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib \"user32\" Alias \"FindWindowExA\" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Sub Command1_Click()
Dim i As Long
Dim sTrayList As Variant
i = FindWindow(\"Shell_TrayWnd\
If i = 0 Then Exit Sub
i = FindWindowEx(i, 0, \"Button\开始\")
sTrayList = GetTrayList(i)
For i = 0 To UBound(sTrayList)
If sTrayList(i) <> \"\" Then
MsgBox \"“开始”按钮提示内容是:\" & sTrayList(i)
End If
Next
End Sub
三、把下面内容保存为mTray.bas
Option Explicit
Private Declare Function OpenProcess Lib \"kernel32\" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Const PROCESS_VM_OPERATION = &H8
Private Const PROCESS_VM_READ = &H10
Private Const PROCESS_VM_WRITE = &H20
Private Const PROCESS_ALL_ACCESS = 0
Private Const MEM_COMMIT = &H1000
Private Const MEM_RESERVE = &H2000
Private Const MEM_DECOMMIT = &H4000
Private Const MEM_RELEASE = &H8000
Private Const MEM_FREE = &H10000
Private Const MEM_PRIVATE = &H20000
Private Const MEM_MAPPED = &H40000
Private Const MEM_TOP_DOWN = &H100000
Private Const PAGE_READWRITE = &H4&
Private Declare Function VirtualAllocEx Lib \"kernel32\" (ByVal hProcess As Long, ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
Private Declare Function VirtualFreeEx Lib \"kernel32\" (ByVal hProcess As Long, lpAddress As Any, ByVal dwSize As Long, ByVal dwFreeType As Long) As Long
Private Declare Function CloseHandle Lib \"kernel32\" (ByVal hObject As Long) As Long
Private Type RECT
Left As Long
top As Long
Right As Long
Bottom As Long
End Type
Private Type TOOLINFO
cbSize As Long
uFlags As Long
Hwnd As Long
uID As Long
cRect As RECT
hInst As Long
lpszText As Long 'LPCSTR
lParam As Long
End Type
Private Type TOOLTEXT
sTipText As String * 80
End Type
Private Const WM_USER = &H400
Private Const TTM_GETTOOLCOUNT = (WM_USER + 13)
Private Const TTM_ENUMTOOLSA = (WM_USER + 14)
Private Const TTM_ENUMTOOLSW = (WM_USER + 58)
Private Const TTM_GETTEXTA = (WM_USER + 11)
Private Const TTM_GETTEXTW = (WM_USER + 56)
Private Declare Function GetParent Lib \"user32\" (ByVal Hwnd As Long) As Long
Private Declare Function FindWindow Lib \"user32\" Alias \"FindWindowA\" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib \"user32\" Alias \"FindWindowExA\" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
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
Private Declare Function EnumWindows& Lib \"user32\" (ByVal lpEnumFunc As Long, ByVal lParam As Long)
Private Declare Function GetWindowThreadProcessId Lib \"user32\" (ByVal Hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetClassName Lib \"user32\" Alias \"GetClassNameA\" (ByVal Hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As
Long
Private Declare Function GetWindowLong& Lib \"user32\" Alias
\"GetWindowLongA\" (ByVal Hwnd As Long, ByVal nIndex As Long)
Const GWL_STYLE = (-16)
Private Declare Function ReadProcessMemory Lib \"kernel32\" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function WriteProcessMemory Lib \"kernel32\" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Sub CopyMemory Lib \"kernel32\" Alias \"RtlMoveMemory\" (lpDest As Any, lpSource As Any, ByVal cBytes As Long)
Dim sTips() As String
Dim NWThreadID As Long, NWPid As Long, NWWnd As Long
Private Function GetMemSharedNT(ByVal pid As Long, ByVal memSize As Long, hProcess As Long) As Long
hProcess = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ
Or PROCESS_VM_WRITE, False, pid)
GetMemSharedNT = VirtualAllocEx(ByVal hProcess, ByVal 0&, ByVal memSize, MEM_RESERVE Or MEM_COMMIT, PAGE_READWRITE)
End Function
Private Sub FreeMemSharedNT(ByVal hProcess As Long, ByVal MemAddress As Long, ByVal memSize As Long)
Call VirtualFreeEx(hProcess, ByVal MemAddress, memSize, MEM_RELEASE)
CloseHandle hProcess
End Sub
Public Function GetTrayList(ByVal Hwnd As Long) As Variant
ReDim sTips(0)
If Hwnd = 0 Then Hwnd = GetTrayNotifyWnd
NWWnd = Hwnd
NWThreadID = GetWindowThreadProcessId(Hwnd, NWPid)
EnumWindows AddressOf EnumWinProc, 0
GetTrayList = sTips
End Function
Private Function GetTrayNotifyWnd() As Long
GetTrayNotifyWnd = FindWindowEx(FindWindow(\"Progman\\"SHELLDLL_DefView\
GetTrayNotifyWnd = FindWindowEx(GetTrayNotifyWnd, 0, \"SysListView32\vbNullString)
End Function
Private Function EnumWinProc(ByVal Hwnd As Long, ByVal lParam As Long) As Long
Dim pid As Long, tid As Long, lStyle As Long
Dim hProcess As Long, nCount As Long, lWritten As Long, i As Long, j As Long
Dim lpSysShared As Long, hFileMapping As Long, dwSize As Long
Dim lpSysShared2 As Long, hFileMapping2 As Long
Dim h As Long
Dim ti As TOOLINFO
Dim tt As TOOLTEXT
tid = GetWindowThreadProcessId(Hwnd, pid)
lStyle = GetWindowLong(Hwnd, GWL_STYLE)
If pid = NWPid And GetWndClass(Hwnd) = \"tooltips_class32\" Then
'If GetWndClass(hwnd) = \"tooltips_class32\" Then
nCount = SendMessage(Hwnd, TTM_GETTOOLCOUNT, 0&, ByVal 0&)
If nCount = 0 Then GoTo 1
j = UBound(sTips)
ReDim Preserve sTips(j + nCount)
ti.cbSize = Len(ti)
dwSize = ti.cbSize
lpSysShared = GetMemSharedNT(pid, dwSize, hProcess)
lpSysShared2 = GetMemSharedNT(pid, LenB(tt), hProcess)
WriteProcessMemory hProcess, ByVal lpSysShared, ti, dwSize, lWritten
WriteProcessMemory hProcess, ByVal lpSysShared2, tt, LenB(tt), lWritten
For i = 0 To (nCount - 1) '枚举所在进程所有tooltiptext
Call SendMessage(Hwnd, TTM_ENUMTOOLSW, i, ByVal lpSysShared)
ReadProcessMemory hProcess, ByVal lpSysShared, ti, dwSize, lWritten
ti.lpszText = lpSysShared2
WriteProcessMemory hProcess, ByVal lpSysShared, ti, dwSize, lWritten
Call SendMessage(Hwnd, TTM_GETTEXTW, 0&, ByVal lpSysShared)
ReadProcessMemory hProcess, ByVal lpSysShared2, tt, LenB(tt), lWritten
If NWWnd = ti.uID Then '当与当前窗口句柄匹配时
sTips(j + i + 1) = StrConv(tt.sTipText, vbFromUnicode)
End If
If i = nCount - 1 Then Exit For
Next i
FreeMemSharedNT hProcess, lpSysShared, dwSize
FreeMemSharedNT hProcess, lpSysShared2, LenB(tt)
End If
1:
EnumWinProc = 1
End Function
Private Function GetWndClass(Hwnd As Long) As String
Dim k As Long, sName As String
sName = Space$(128)
k = GetClassName(Hwnd, sName, 128)
If k > 0 Then sName = Left$(sName, k) Else sName = \"No class\"
GetWndClass = sName
End Function
四、把以上三个文件放在同一个目录下,然后双击鼠标运行Project1.vbp即可运行form1并看到效果。
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- huatuo3.com 版权所有 蜀ICP备2023022190号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务