ummm... i know this maybe a n00b question but thats what i am...
but how do i get hWnd of a textbox in another window??? :blush: :blush:
Printable View
ummm... i know this maybe a n00b question but thats what i am...
but how do i get hWnd of a textbox in another window??? :blush: :blush:
Here is an example from the API Guide.
VB Code:
Const WS_CHILD = &H40000000 Const WM_LBUTTONDOWN = &H201 Const WM_LBUTTONUP = &H202 Const SW_HIDE = 0 Const SW_NORMAL = 1 Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type 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 ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long Private Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long Dim tWnd As Long, bWnd As Long, ncWnd As Long Private Sub Form_Load() 'KPD-Team 1998 'URL: [url]http://www.allapi.net/[/url] 'E-Mail: [email][email protected][/email] Dim R As RECT 'Get the taskbar's window handle tWnd = FindWindow("Shell_TrayWnd", vbNullString) 'Get the start-button's window handle bWnd = FindWindowEx(tWnd, ByVal 0&, "BUTTON", vbNullString) 'Get the start button's position GetWindowRect bWnd, R 'Create a new button ncWnd = CreateWindowEx(ByVal 0&, "BUTTON", "Hello !", WS_CHILD, 0, 0, R.Right - R.Left, R.Bottom - R.Top, tWnd, ByVal 0&, App.hInstance, ByVal 0&) 'Show our button ShowWindow ncWnd, SW_NORMAL 'Hide the start button ShowWindow bWnd, SW_HIDE End Sub Private Sub Form_Unload(Cancel As Integer) 'show the start button ShowWindow bWnd, SW_NORMAL 'destroy our button DestroyWindow ncWnd End Sub
actually it duznt help v.much....
how about a much easier one? like gettin calculator 's answer texbox?
That is for the start button.
who is doubting that?
ok guyz i did it....
got hWnd and now reading text in it using WM_GETTEXT
but there iz a problem
there are two textboxes in the window i am reading
the one i want to read from is the second one.
but vb is reading from the first one. when i searched through this forum
the answer i got is to use EnumChildWindows but i dunno how to use it.
can anyone tell me how to use EnumChildWindows to read from the second textbox?
you can use FindWindowEx:VB Code:
lhWnd = FindWindowEx([i]CalculatorhWnd[/i], 0&, "Edit", vbNullString) ' lhWnd is now first textbox lhWnd = FindWindowEx([i]CalculatorhWnd[/i], [B]lhWnd[/B], "Edit", vbNullString) ' lhWnd is now second textbox
Dosent seem to work...
here is the code...
pls tell what is wrong and how to move on to the next textbox
VB Code:
Option Explicit Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _ (ByVal lpClassName As String, ByVal lpWindowName As String) As Long 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 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 EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long Private Const WM_GETTEXTLENGTH = &HE Private Const WM_GETTEXT = &HD Private Sub Command1_Click() Dim lHwnd As Long Dim sWindowText As String, lTextLen As Long lHwnd = FindWindow(vbNullString, "PORTAL.NET") lHwnd = FindWindowEx(lHwnd, ByVal 0&, "ThunderRT6Textbox", vbNullString) lTextLen = SendMessage(lHwnd, WM_GETTEXTLENGTH, ByVal 0&, ByVal 0&) sWindowText = Space$(lTextLen + 1) Call SendMessage(lHwnd, WM_GETTEXT, ByVal lTextLen + 1, ByVal sWindowText) sWindowText = Left$(sWindowText, lTextLen) Text1 = sWindowText End Sub
EDIT: you're overwriting the parent lhWnd, so you'd have to do it like this:VB Code:
Dim lhWnd As Long, lhWndChild As Long lhWnd = FindWindow(vbNullString, "PORTAL.NET") lhWndChild = FindWindowEx(lhWnd, 0&, "ThunderRT6Textbox", vbNullString) lhWndChild = FindWindowEx(lhWnd, lhWndChild, "ThunderRT6Textbox", vbNullString) ' 'then work with lhWndChild
Dosn't seem like you added bushmobile's suggestion.......
i added it...but get the same results.
VB Code:
Option Explicit Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _ (ByVal lpClassName As String, ByVal lpWindowName As String) As Long 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 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 EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long Private Const WM_GETTEXTLENGTH = &HE Private Const WM_GETTEXT = &HD Private Sub Command1_Click() Dim lHwnd As Long Dim sWindowText As String, lTextLen As Long lHwnd = FindWindowEx(lHwnd, ByVal 0&, "ThunderRT6Textbox", vbNullString) lHwnd = FindWindowEx(lHwnd, lHwnd, "ThunderRT6Textbox", vbNullString) lHwnd = FindWindow(vbNullString, "PORTAL.NET") lHwnd = FindWindowEx(lHwnd, ByVal 0&, "ThunderRT6Textbox", vbNullString) lTextLen = SendMessage(lHwnd, WM_GETTEXTLENGTH, ByVal 0&, ByVal 0&) sWindowText = Space$(lTextLen + 1) Call SendMessage(lHwnd, WM_GETTEXT, ByVal lTextLen + 1, ByVal sWindowText) sWindowText = Left$(sWindowText, lTextLen) Text1 = sWindowText End Sub
test app test succeeded
did you see my edit to post #9?
okay i m gettin a lil confused here...
bushmobile, can u post the whole [Edited by MartinLiss] code?
Imagine that the windows are stacked like a deck of cards. The first argument of FindWindowEx tells it which deck to look in. The second argument tells it where to start looking in that deck. The last two arguments are what to look for.VB Code:
Private Sub Command1_Click() Dim lhWnd As Long, lhWndChild As Long, lTextLen As Long Dim sWindowText As String lhWnd = FindWindowEx(0&, 0&, vbNullString, "PORTAL.NET") lhWndChild = FindWindowEx(lhWnd, 0&, "ThunderRT6Textbox", vbNullString) lhWndChild = FindWindowEx(lhWnd, lhWndChild, "ThunderRT6Textbox", vbNullString) lTextLen = SendMessage(lhWndChild, WM_GETTEXTLENGTH, ByVal 0&, ByVal 0&) sWindowText = Space$(lTextLen + 1) Call SendMessage(lhWndChild, WM_GETTEXT, ByVal lTextLen + 1, ByVal sWindowText) sWindowText = Left$(sWindowText, lTextLen) Text1.Text = sWindowText End Sub
by passing the first textboxes hWnd as the second argument, the API will start looking for a textbox from there, and hence find the second. If you pass the hwnd of the second it will then go on to find the third, etc.
aah i c the problem...
class name "ThunderRT6Textbox" is used in the test exe i made to test the code.
in the real app that i am trying to win over PORTAL.NET the class is "Edit" sooo when i turn all the "ThunderRT6Textbox" into "Edit" the code duznt work...
also the 2 textboxes are in to separate frames...
soo what do you think?
this time also i think i cant get it over by myself
but as bushmobile said i am imagining and coding.....................
if the textbox is in a frame then it's in a different deck of cards - you'll have to do something like:you need to check the class of the relevant windows, and make sure you move through the structure of the window correctly (check using Spy++ or some other WinSpy)VB Code:
Dim lhWnd As Long, lhWndFrame As Long, lhWndChild As Long ' Find Parent lhWnd = FindWindowEx(0&, 0&, vbNullString, "PORTAL.NET") ' Find Frame lhWndFrame = FindWindowEx(lhWnd, 0&, [i]FrameClass[/i], vbNullString) ' Find TextBox lhWndChild = FindWindowEx(lhWndFrame, 0&, [i]TextBoxClass[/i], vbNullString)
i cant put it 2gether and and its bugging me too much. so i give up on the second box for now. i will do it sometime again.
@least i learnt a lot while trying to learn this. thanx bushmobile and all others.