| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5548 人关注过本帖
标题:VB获取其它程序中的文本内容
只看楼主 加入收藏
msxjc
Rank: 1
等 级:新手上路
帖 子:83
专家分:0
注 册:2005-3-28
结帖率:50%
收藏
 问题点数:0 回复次数:7 
VB获取其它程序中的文本内容
如何用VB获取计算器中的计算结果?只要能得到计算器上那个文本框里面的内容就行了。

[ 本帖最后由 msxjc 于 2010-6-11 09:46 编辑 ]
搜索更多相关主题的帖子: 获取 文本 
2010-06-11 09:43
msxjc
Rank: 1
等 级:新手上路
帖 子:83
专家分:0
注 册:2005-3-28
收藏
得分:0 
就是没人回答吗?

2010-06-11 14:30
bczgvip
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:66
帖 子:1310
专家分:5312
注 册:2009-2-26
收藏
得分:0 
直接复制不就行咯?
2010-06-11 14:53
jiashie
Rank: 8Rank: 8
等 级:贵宾
威 望:10
帖 子:237
专家分:999
注 册:2009-4-30
收藏
得分:0 
貌似计算器的那个文本框作了特殊的处理,直接用GetWindowText是获取不到它的值的。必须更直接地用WM_GETTEXT消息才行。
参考:http://www.
先在窗体中声明以下函数及常数
程序代码:
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 GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) 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 Sub RtlMoveMemory Lib "KERNEL32" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
Private Const WM_GETTEXT = &HD
Private Const WM_GETTEXTLENGTH = &HE

在按钮的单击事件中写以下代码
程序代码:
Dim lHwnd As Long
Dim lChildHwnd As Long
Dim strRet As String
Dim n As Long
Dim bArr() As Byte, bArr2() As Byte

lHwnd = FindWindow(vbNullString, "计算器")
If lHwnd > 0 Then
    lChildHwnd = FindWindowEx(lHwnd, 0&, "Edit", vbNullString)
    If lChildHwnd > 0 Then
        ' 获取文本长度
        n = SendMessage(lChildHwnd, WM_GETTEXTLENGTH, 0, 0)
        If n > 0 Then
            ' 设置缓冲区
            ReDim bArr(n + 1) As Byte
            ReDim bArr2(n - 1) As Byte
            Call RtlMoveMemory(bArr(0), n, 2)
            '发送 WM_GETTEXT 消息
            Call SendMessage(lChildHwnd, WM_GETTEXT, n + 1, bArr(0))
            Call RtlMoveMemory(bArr2(0), bArr(0), n)
            ' 得到文本
            strRet = StrConv(bArr2, vbUnicode)
            
            '//! 对于计算器的文本框不能直接用GetWindowText.
            'n = GetWindowTextLength(lChildHwnd)
            'strRet = String(n, "")
            'call GetWindowText(lChildHwnd, strRet, n)
            Debug.Print "text=", strRet
        End If
    End If
End If


[ 本帖最后由 jiashie 于 2010-6-11 15:21 编辑 ]
2010-06-11 15:18
msxjc
Rank: 1
等 级:新手上路
帖 子:83
专家分:0
注 册:2005-3-28
收藏
得分:0 
以下是引用jiashie在2010-6-11 15:18:43的发言:

貌似计算器的那个文本框作了特殊的处理,直接用GetWindowText是获取不到它的值的。必须更直接地用WM_GETTEXT消息才行。
参考:http://www.
先在窗体中声明以下函数及常数
 
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 GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) 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 Sub RtlMoveMemory Lib "KERNEL32" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
Private Const WM_GETTEXT = &HD
Private Const WM_GETTEXTLENGTH = &HE
 
在按钮的单击事件中写以下代码
 
Dim lHwnd As Long
Dim lChildHwnd As Long
Dim strRet As String
Dim n As Long
Dim bArr() As Byte, bArr2() As Byte
 
lHwnd = FindWindow(vbNullString, "计算器")
If lHwnd > 0 Then
    lChildHwnd = FindWindowEx(lHwnd, 0&, "Edit", vbNullString)
    If lChildHwnd > 0 Then
        ' 获取文本长度
        n = SendMessage(lChildHwnd, WM_GETTEXTLENGTH, 0, 0)
        If n > 0 Then
            ' 设置缓冲区
            ReDim bArr(n + 1) As Byte
            ReDim bArr2(n - 1) As Byte
            Call RtlMoveMemory(bArr(0), n, 2)
            '发送 WM_GETTEXT 消息
            Call SendMessage(lChildHwnd, WM_GETTEXT, n + 1, bArr(0))
            Call RtlMoveMemory(bArr2(0), bArr(0), n)
            ' 得到文本
            strRet = StrConv(bArr2, vbUnicode)
            
            '//! 对于计算器的文本框不能直接用GetWindowText.
            'n = GetWindowTextLength(lChildHwnd)
            'strRet = String(n, "")
            'call GetWindowText(lChildHwnd, strRet, n)
            Debug.Print "text=", strRet
        End If
    End If
End If
这段代码,在XP系统下,运行通过。但我的程序不是运行在XP下的。是运行在WIN 7下,WIN 7下的计算器,那个框的类名,并不是Edit,而是Static,所以此代码在WIN 7下无效

2010-06-11 19:37
jiashie
Rank: 8Rank: 8
等 级:贵宾
威 望:10
帖 子:237
专家分:999
注 册:2009-4-30
收藏
得分:0 
回复 5楼 msxjc
哦,我写这代码的时候系统是win XP的,直接用spy++查看的类名,没想到在win 7下连计算器都不一样了。那我就没办法测试了,自己动手根据上面的代码作调整吧。
2010-06-12 08:29
chenxiaobin
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-6-26
收藏
得分:0 
回复 5楼 msxjc
你好,我试了下,按你的编程代码编译时出现错误,就是窗体代码有问题,请教是怎么回事?谢谢
2015-06-26 22:49
chenxiaobin
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-6-26
收藏
得分:0 
本人对VB不太懂,请教如何用VB获取第三方软件窗口中的文本,将获得的数据保存在变量中,请给予详细的
VB程序代码,第三方软件已放在附件中,谢谢大家
KOZ-0010-00-V116[SIDV检测线安装文件].rar (286.64 KB)
2015-06-26 22:59
快速回复:VB获取其它程序中的文本内容
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021719 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved