用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

 

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...