Link to home
Start Free TrialLog in
Avatar of rcherne1
rcherne1Flag for United States of America

asked on

How do I make a hook to copy a result from windows calculator?

Let me give you an example of what I’m looking for. Lets say I have a button on a vb form that activates the windows calculator. I what to do the math, but When I close the “Calculator App" I want to copy the result to a text box in my vb app.  
 

 
Avatar of JohnBPrice
JohnBPrice

To get the value out of calculator, you can use GetWindowText on the text box.  It is the only edit box in calculator, so you should be able to find it pretty easy.  I'll post some code in a minute.


VB6 or .net?

In VB 6, a hack would be to shell the calculator, and keep reading the text box value until the calculator goes away, the last value you get should be the value you want.

In VB.net, you could hook into the windows callbacks that announce when an application is closing, and grab your data then
oops, I guess you can't use GetWindowText across apps, but you can use WM_GetText, here is code for the hack version. It's just a form with a button.  The button will launch calculator and get the result when you close the calculator.

Option Explicit
Private Const WM_GETTEXT = &HD
Private Const GW_CHILD = 5
Private Const GW_HWNDNEXT = 2

Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd 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 GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount 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 Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)


Private Sub Command1_Click()
    Dim CalcWindow As Long
    Dim CalcText As String
    Dim erc As Long
    Shell "C:\windows\System32\calc.exe"
    CalcWindow = FindCalculatorWindow
   
    If CalcWindow <> 0 Then
        Do
            erc = GetCalculatorText(CalcWindow, CalcText)
            Sleep (50)
        Loop Until erc <> 0
    End If
    MsgBox CalcText
End Sub
Function FindCalculatorWindow() As Long
    Dim desktop As Long
    Dim calculator As Long
    Dim textbox As Long
    Dim buffer As String * 200
    Dim bufflen As Long
    Dim bytearray(200) As Byte
    desktop = GetDesktopWindow()
   
    calculator = GetWindow(desktop, GW_CHILD)
    Do While calculator <> 0
        bufflen = GetWindowText(calculator, buffer, 200)
        If Left(buffer, 10) = "Calculator" Then
            Exit Do
        End If
        calculator = GetWindow(calculator, GW_HWNDNEXT)
    Loop
    If calculator = 0 Then
        MsgBox "Could not find calculator!"
        Exit Function
    End If
   
    textbox = GetWindow(calculator, GW_CHILD)
    Do While textbox <> 0
        bufflen = GetClassName(textbox, buffer, 200)
        If Left(buffer, 4) = "Edit" Then
            Exit Do
        End If
        textbox = GetWindow(textbox, GW_HWNDNEXT)
    Loop
    If textbox = 0 Then
        MsgBox "Could not find text box in calculator"
        Exit Function
    End If
    FindCalculatorWindow = textbox
End Function
Function GetCalculatorText(hwnd As Long, ByRef result As String) As Long
    Dim bufflen As Long
    Dim bytearray(200) As Byte
    Dim i As Integer
    Dim Answer As String

    bufflen = SendMessage(hwnd, WM_GETTEXT, 200, bytearray(0))

    If bufflen > 0 Then
        For i = 0 To bufflen - 1
            Answer = Answer & Chr(bytearray(i))
        Next
        result = Answer
        GetCalculatorText = 0
    Else
        GetCalculatorText = -1
        'don't touch result
    End If

End Function
In theory, you should be able to use FindWindowEx to find the textbox instead of my loops, but for some reason FindWindowEx never works for me.
To get the edit window handle of calculator using FindWindowEx, use:

'Get the handle of the Edit window of Calc
lhWndEdit = FindWindowEx(lhWnd, 0, "Edit", vbNullString)

where

lhWnd is the main window handle of calculator.
ASKER CERTIFIED SOLUTION
Avatar of JohnBPrice
JohnBPrice

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial