| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5548 人关注过本帖
标题:VB获取其它程序中的文本内容
取消只看楼主 加入收藏
msxjc
Rank: 1
等 级:新手上路
帖 子:83
专家分:0
注 册:2005-3-28
结帖率:50%
收藏
 问题点数:0 回复次数:2 
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
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
快速回复:VB获取其它程序中的文本内容
数据加载中...
 
   



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

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