|
-
Dec 12th, 2006, 05:41 PM
#1
Thread Starter
Addicted Member
Get hWnd
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???
-
Dec 12th, 2006, 06:02 PM
#2
Re: Get hWnd
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]
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
-
Dec 12th, 2006, 06:09 PM
#3
Thread Starter
Addicted Member
Re: Get hWnd
actually it duznt help v.much....
how about a much easier one? like gettin calculator 's answer texbox?
-
Dec 12th, 2006, 10:16 PM
#4
Re: Get hWnd
That is for the start button.
-
Dec 13th, 2006, 01:24 AM
#5
Thread Starter
Addicted Member
-
Dec 13th, 2006, 02:35 AM
#6
Thread Starter
Addicted Member
Re: Get hWnd
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?
-
Dec 13th, 2006, 03:58 AM
#7
Re: Get hWnd
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
-
Dec 13th, 2006, 04:20 AM
#8
Thread Starter
Addicted Member
Re: Get hWnd
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
-
Dec 13th, 2006, 04:22 AM
#9
Re: Get hWnd
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
Last edited by bushmobile; Dec 13th, 2006 at 04:27 AM.
-
Dec 13th, 2006, 04:24 AM
#10
Re: Get hWnd
Dosn't seem like you added bushmobile's suggestion.......
-
Dec 13th, 2006, 04:29 AM
#11
Thread Starter
Addicted Member
Re: Get hWnd
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
-
Dec 13th, 2006, 04:31 AM
#12
Thread Starter
Addicted Member
-
Dec 13th, 2006, 04:32 AM
#13
Re: Get hWnd
did you see my edit to post #9?
-
Dec 13th, 2006, 04:34 AM
#14
Thread Starter
Addicted Member
Re: Get hWnd
okay i m gettin a lil confused here...
bushmobile, can u post the whole [Edited by MartinLiss] code?
Last edited by MartinLiss; Dec 13th, 2006 at 10:23 AM.
-
Dec 13th, 2006, 04:41 AM
#15
Re: Get hWnd
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
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.
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.
-
Dec 13th, 2006, 04:56 AM
#16
Thread Starter
Addicted Member
Re: Get hWnd
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.....................
-
Dec 13th, 2006, 05:00 AM
#17
Re: Get hWnd
if the textbox is in a frame then it's in a different deck of cards - you'll have to do something like:
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)
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)
-
Dec 13th, 2006, 05:07 AM
#18
Thread Starter
Addicted Member
Re: Get hWnd
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|