200分相送,如何获取正在运行的其他程序窗口中的文本内容!

wenw 2002-07-18 10:58:55
我们很容易获得正在运行的其他程序窗口的句柄和窗口标题,可是,我们怎么样获得它中间文本的内容呢?
...全文
303 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
wenw 2002-07-19
  • 打赏
  • 举报
回复
函数功能:该函数将指定窗口的标题条文本(如果存在)拷贝到一个缓存区内。如果指定的窗口是一个控制,则拷贝控制的文本。但是,GetWindowTeXt不能接收在其他应用程序中的控制文本。

我是要接收在其他应用程序中的弹出窗体的文本内容,该怎么办呢?
wenw 2002-07-19
  • 打赏
  • 举报
回复
问题已经解决,结帐!
40Star 2002-07-18
  • 打赏
  • 举报
回复
Option Explicit
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function SendMessageByStr& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String)
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


Const WM_GETTEXTLENGTH = &HE
Const WM_GETTEXT = &HD

Private Type POINTAPI
x As Long
y As Long
End Type

Private Sub Timer1_Timer()
Dim P As POINTAPI
Dim H As Long
GetCursorPos P
H = WindowFromPoint(P.x, P.y)

Dim L As Long
L = SendMessage(H, WM_GETTEXTLENGTH, 0, 0)

Dim t As String
t = Space(1000)
SendMessageByStr H, WM_GETTEXT, 1000, t
Text1.Text = t
End Sub
wenw 2002-07-18
  • 打赏
  • 举报
回复
例如对话框,我获得了句柄,该怎么得到它的提示内容呢?
Ranma_True 2002-07-18
  • 打赏
  • 举报
回复
先用spy++查出目标文本所在控件的类,然后根据这个类名称获得
此控件的句柄。
不知此方法是否可行。
KAIBate 2002-07-18
  • 打赏
  • 举报
回复
关注。:)
dsclub 2002-07-18
  • 打赏
  • 举报
回复
啊,对不起。如果你是想要“文本内容”那GETWINDOWTEXT是可以的,我看错了。为了表达歉意:
'Option Explicit

Private Type POINTAPI
x As Long
y As Long
End Type

Private Declare Sub GetCursorPos Lib "user32" (lpPoint As POINTAPI)
Private Declare Function WindowFromPoint Lib "user32" (ByVal ptY As Long, ByVal ptX As Long) As Long
Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
Private Declare Function GetWindowWord Lib "user32" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd 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 GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal h&, ByVal hb&, ByVal x&, ByVal y&, ByVal cx&, ByVal cy&, ByVal f&) As Long
Private Declare Function GetActiveWindow& Lib "user32" ()

Const GWW_HINSTANCE = (-6)
Const GWW_ID = (-12)
Const GWL_STYLE = (-16)
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Const SWP_NOMOVE = 2
Const SWP_NOSIZE = 1
Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
Const SW_MINIMIZE = 6




Private Sub Form_Load()

'This centers the form. I usually add a function to center
'a form but since this is just a one-form kinda program,
'I just put it here.
Me.Left = (Screen.Width - Me.Width) / 2
Me.Top = (Screen.Height - Me.Height) / 2

'This calls the StayOnTop function. You can find this
'particular function under every rock and in just about
'every book on Visual Basic. :)
StayOnTop Me

End Sub

Private Sub Label1_Click()
End
End Sub

'使本窗体alwaysontop
Private Sub StayOnTop(frm As Form)

Dim success&
success& = SetWindowPos(frm.hwnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)

End Sub

Private Sub Timer1_Timer()

'Among the things I played with was removing the Hex function from
'this code. Personally, I don't care much about Hex values. If that
'kind of thing floats *your* boat, pull out the KB article and
'change it back. :)

Dim ptCursor As POINTAPI
Dim sWindowText As String * 100
Dim sClassName As String * 100
Dim hWndOver As Long
Dim hWndParent As Long
Dim sParentClassName As String * 100
Dim wID As Long
Dim lWindowStyle As Long
Dim hInstance As Long
Dim sParentWindowText As String * 100

Dim sModuleFileName As String * 100
Static hWndLast As Long

Call GetCursorPos(ptCursor) ' Get cursor position
hWndOver = WindowFromPoint(ptCursor.x, ptCursor.y) ' Get window cursor is over
If hWndOver <> hWndLast Then ' If changed update display
hWndLast = hWndOver ' Save change
Cls ' Clear the form
Print "Window Handle: "; (hWndOver) ' Display window handle
Print "Focus: "; GetActiveWindow() 'I added this
r = GetWindowText(hWndOver, sWindowText, 100) ' Window text
Print "Window Text: " & Left(sWindowText, r)

r = GetClassName(hWndOver, sClassName, 100) ' Window Class
Print "Window Class Name: "; Left(sClassName, r)

lWindowStyle = GetWindowLong(hWndOver, GWL_STYLE) ' Window Style
Print "Window Style: "; (lWindowStyle)

' Get handle of parent window:
hWndParent = GetParent(hWndOver)

' If there is a parent get more info:

If hWndParent <> 0 Then
' Get ID of window:
wID = GetWindowWord(hWndOver, GWW_ID)
Print "Window ID Number: "; (wID)
Print "Parent Window Handle: "; (hWndParent)

' Get the text of the Parent window:
r = GetWindowText(hWndParent, sParentWindowText, 100)
Print "Parent Window Text: " & Left(sParentWindowText, r)

' Get the class name of the parent window:
r = GetClassName(hWndParent, sParentClassName, 100)
Print "Parent Window Class Name: "; Left(sParentClassName, r)
Else
' Update fields when no parent:
Print "Window ID Number: N/A"
Print "Parent Window Handle: N/A"
Print "Parent Window Text : N/A"

Print "Parent Window Class Name: N/A"
End If

' Get window instance:
hInstance = GetWindowWord(hWndOver, GWW_HINSTANCE)

' Get module file name:
r = GetModuleFileName(hInstance, sModuleFileName, 100)
Print "Module: "; Left(sModuleFileName, r)
End If



End Sub

anywayliu 2002-07-18
  • 打赏
  • 举报
回复
你是什么意思?是得到另一窗体的文本框的内容吗?
如是只须窗体名.文本框控件名即可,如:frm.text1
anywayliu 2002-07-18
  • 打赏
  • 举报
回复
你是什么意思?是得到另一窗体的文本框的内容吗?
如是只须窗体名.文本框控件名即可,如:frm.text1
dsclub 2002-07-18
  • 打赏
  • 举报
回复
不能用GETWINDOWTEXT

那个函数只能得到控件被置值后的TEXT内容.


chenyu5188 2002-07-18
  • 打赏
  • 举报
回复
用API, GETWINDOWTEXT
lou_df 2002-07-18
  • 打赏
  • 举报
回复
得到窗口的句柄后,取窗口的文本.
guiqing 2002-07-18
  • 打赏
  • 举报
回复
40Star(陪你去看--☆流星雨★) 说的方法就可以,不过这种方法在2000和XP下得不到类名为Edit框的文本内容,有谁知道顺便答复。谢谢。
还有用getwindowtext可以直接得到窗口名。
spy++是vs里的一个小程序,是用来看句柄和类名等的。它是独立程序,可以找一个来用。
renyao 2002-07-18
  • 打赏
  • 举报
回复
请问SPY++是什么?
在VB里怎么用SPY++?
网络咖啡 2002-07-18
  • 打赏
  • 举报
回复
学习
NowCan 2002-07-18
  • 打赏
  • 举报
回复
你的意思是不是,比如要得到一个MessageBox里的文字?
NowCan 2002-07-18
  • 打赏
  • 举报
回复
你的意思是不是,比如要得到一个MessageBox里的文字?

7,785

社区成员

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

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