[vb6] Is het mogelijk value textbox van ander prog te lezen?

Pagina: 1
Acties:

  • DPLuS
  • Registratie: April 2000
  • Niet online
Ik wilde wat dingetjes gaan automatiseren en toen liep ik tegen het volgende punt aan:
Is het mogelijk om via wat API calls in VB6 de waarde van bijv. een textbox van een andere applicatie uit te lezen, met alleen de hwnd waarde van de applicatie?
Bijvoorbeeld van de rekenmachine (standaard in windows).

Nou had ik al de volgende code gevonden:

Visual Basic 6:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Option Explicit

' ClassSpy Sample from BlackBeltVB.com
' http://blackbeltvb.com
'
' Written by Matt Hart
' Copyright 1999 by Matt Hart
'
' This software is FREEWARE. You may use it as you see fit for
' your own projects but you may not re-sell the original or the
' source code. Do not copy this sample to a collection, such as
' a CD-ROM archive. You may link directly to the original sample
' using "http://blackbeltvb.com/classspy.htm"
'
' No warranty express or implied, is given as to the use of this
' program. Use at your own risk.
'
' This shows several APIs - mainly how to get the class name.
' This is handy as a utility so that you can later use the FindWindow API call.

Private Type POINTAPI
    x As Long
    y As Long
End Type

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) 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 Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Const WM_GETTEXT = &HD
Const WM_GETTEXTLENGTH = &HE

Private Sub Form_Unload(Cancel As Integer)
    End
End Sub

Private Sub Timer1_Timer()
    Dim P As POINTAPI, lRet As Long, hHandle As Long, aClass As String, aText As String
    lRet = GetCursorPos(P)
    hHandle = WindowFromPoint(P.x, P.y)
    aClass = Space$(128)
    lRet = GetClassName(hHandle, aClass, 128)
    aClass = Left$(aClass, lRet)
    lblHandle.Caption = hHandle
    lblClassName.Caption = aClass
    Dim lTextlen As Long
    lTextlen = SendMessage(hHandle, WM_GETTEXTLENGTH, 0, ByVal 0)
    If lTextlen Then
        If lTextlen > 1024 Then lTextlen = 1024
        lTextlen = lTextlen + 1
        aText = Space$(lTextlen)
        lRet = SendMessage(hHandle, WM_GETTEXT, lTextlen, ByVal aText)
        aText = Left$(aText, lRet)
    End If
    lblWText.Caption = aText
End Sub


Maar nu zit ik met het volgende probleem:
de handle van de rekenmachine verandert natuurlijk iedere keer (tevens de handle van de uit te lezen textbox).
Hoe kan ik er dan achter komen wat de handle van die desbetreffende textbox is die ik wil uitlezen, nadat de applicatie bijv. nog eens wordt opgestart?

  • klinz
  • Registratie: Maart 2002
  • Laatst online: 21-05 09:01

klinz

weet van NIETS

Met FindWindow() of FindWindowEx(). Werkt natuurlijk alleen als er maar één instantie van de rekenmachine loopt.

  • RayNbow
  • Registratie: Maart 2003
  • Laatst online: 12:56

RayNbow

Kirika <3

klinz schreef op 12 januari 2004 @ 00:39:
Met FindWindow() of FindWindowEx(). Werkt natuurlijk alleen als er maar één instantie van de rekenmachine loopt.
Dat probleem kan worden verholpen door de Rekenmachine zelf te starten, zie:
HOWTO: Find a Window Handle from an Instance Handle

Ipsa Scientia Potestas Est
NNID: ShinNoNoir


  • DPLuS
  • Registratie: April 2000
  • Niet online
Het probleem is ook niet dat ik de window-handle niet kan vinden, het probleem zit het in het feit dat de handles voor de textboxjes OOK meeveranderen.
En daar wil ik juist de waardes uit lezen en dat gaat niet als ik de handle van de textbox niet ken...

Uitleg:

Ik heb een programma met form.caption: “Mijn Programma - $nummertje”.
Er kunnen meerdere instanties van dit programma gestart worden.
Dus het eerste wat ik hoogstwaarschijnlijk moet doen is het opzoeken van de juiste window-handle van de instantie waar ik gegevens aan wil onttrekken.
Dus eerst geef ik via een API call (findwindow) de gebruiker een scherm met de mogelijk opgestarte instanties van “Mijn Programma - $nummertje”.
De gebruiker kiest op zijn beurt de juiste instantie en voila, ik heb alvast de handle van het juiste window.
Dit window heeft een stuk of 20 textboxjes met waardes.

EN NU:
wil ik dus de waardes uit die textboxjes lezen.
Ik weet dat ik dan de handle moet hebben van die textboxjes, maar hoe kan ik er achter komen welke handles dat zijn?

Ik nam toen straks al een voorbeeld met de rekenmachine in windows.
Als je bovenstaande code uitvoert en je hovert met je muis boven het textboxje van de rekenmachine, krijg je de windows-handle van de textbox terug MET DE WAARDE.
Alleen weet ik wel hoe je een windows-handle vindt van een applicatie, maar hoe van de controls binnen de applicatie??

Ik hoop dat het een beetje duidelijk is…

  • curry684
  • Registratie: Juni 2000
  • Laatst online: 12-05 22:23

curry684

left part of the evil twins

Via EnumWindows alle toplevel windows aflopen totdat je de goede hebt, daarna via EnumChildWindows voor de child windows (oftewel de editboxen) hetzelfde doen.

Als je het niet op naam/type maar op positie wilt doen kun je in volgorde hetzelfde doen met WindowFromPoint en daarna ChildWindowFromPoint.

Wat vanzelfsprekend allemaal staat in het Windowing gedeelte van MSDN Library Online :z

Professionele website nodig?