微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

用VB获取计算器中的值


代码如下,并加了一些注释

转自http://hi.baidu.com/ok100fen/blog/item/8b825a3a065522ed14cecb67.html

Option Explicit


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 FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String,ByVal lpWindowName 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 Const WM_GETTEXT = &HD   ''要想获得其他程序edit控件中的信息,只能通过发送WM_GETTEXT消息才能成功

Private Const WM_GETTEXTLENGTH = &HE


'执行这个方法前先用Shell "calc.exe"调用起计算器,先运算上几个数据做为测试


Private Sub Command1_Click()

      Dim ChildHwnd As Long

      Dim Phwnd     As Long  

     Phwnd = FindWindow(vbNullString,"计算器") 		'用FindWindow函数得到计算器的句柄                                                
      If Phwnd <> 0 Then

    	   '在这里已经知道计算器里对那个编辑框的名字为"Edit"

            '用findwindowex得到计算器里Edit的句柄。

              ChildHwnd = findwindowex(Phwnd,0&,"Edit",vbNullString)         

              Dim str As String * 256 '这里最好设置一个足够大的数

    	   '要想获得其他程序edit控件中的信息,只能通过发送WM_GETTEXT消息才能成功

              '用SendMessage向系统发送消息从Edit里取值存到str里。

              SendMessage ChildHwnd,WM_GETTEXT,100,ByVal str '100是字符串的长度   

              str = Trim(str)

            txtText1.Text = str

      End If

End Sub

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐