How Do you fill in a control of another program say if I had calculator open I could Add numbers to the text box
and another
question how would I get what the text box in calculator was saying and put it in a label on my program
Printable View
How Do you fill in a control of another program say if I had calculator open I could Add numbers to the text box
and another
question how would I get what the text box in calculator was saying and put it in a label on my program
You can use the SendMessage API:
Usage:Code:Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Const WM_GETTEXT = &HD
Public Const WM_SETTEXT = &HC
This should give you the text in the calculator textbox in your own textbox.Code:Dim sText As String
Dim lMaxLength As Long
Dim lLength As Long
lMaxLength = 1024
sText = Space(lMaxLength + 1)
'hwndCalc is the handle of the text box in calculator which you can get using a spy tool or the FindWindow API
lLength = SendMessage(hwndCalc, WM_GETTEXT, lMaxLength, ByVal sText)
Text1.Text = Left(sText, lLength)
Similarly you can use WM_SETTEXT to change the text in calculator.
I didn't test it yet, but it should work.
If it doesn't, post the error you get and I'll try to fix it.
Hope it helps.
This is what I got using a spy tool:
Class of calculator: "SciCalc"
Class of the textbox in calculator: "Static"
The hwnd of the textbox is not a constant.
So if you want to get the handle of the textbox, you should loop through all the child windows of the calculator, and find the one with a text like "0." or something like that.
Although this technique can be used to get or set the text in the label, you should not expect that the calculator really calculates with the numbers you put in the label. Very likely it uses internal variables for this.
Well, this works for setting the text, but it obviously keeps its calculations to itself because it won't accept it as a number.
Code:Private Sub Form_KeyPress(KeyAscii As Integer)
On Error Resume Next
Dim hWndCalc As Long
Dim hWndDisp As Long
Dim sText As String * 50
Dim sTextB As String
Dim bTx() As Byte
If KeyAscii <= vbKey9 Or KeyAscii >= vbKey0 Then
hWndCalc = FindWindow("SciCalc", vbNullString)
hWndDisp = GetDlgItem(hWndCalc, 403) ' 403 is the ID of the display
sText = String(50, 0)
GetWindowText hWndDisp, sText, 50
sTextB = Left(sText, InStr(1, sText, Chr(0)) - 1)
sTextB = sTextB & Chr(KeyAscii)
ReDim bTx(Len(sTextB))
For i = 0 To Len(sTextB)
bTx(i) = Asc(Mid(sTextB, i + 1, 1))
Next i
bTx(Len(sTextB)) = 0
SendMessage hWndDisp, WM_SETTEXT, 0, bTx(0)
End If
End Sub
Another thing you can do is simulate keypresses to the calculator using keybd_event.
The calculator responds to number keys and -, +, *, / etc.
Ok thanks guys