Results 1 to 18 of 18

Thread: Get hWnd

  1. #1

    Thread Starter
    Addicted Member Beasts's Avatar
    Join Date
    Oct 2006
    Posts
    147

    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???

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Get hWnd

    Here is an example from the API Guide.

    VB Code:
    1. Const WS_CHILD = &H40000000
    2. Const WM_LBUTTONDOWN = &H201
    3. Const WM_LBUTTONUP = &H202
    4. Const SW_HIDE = 0
    5. Const SW_NORMAL = 1
    6. Private Type RECT
    7.     Left As Long
    8.     Top As Long
    9.     Right As Long
    10.     Bottom As Long
    11. End Type
    12. 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
    13. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    14. Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    15. Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    16. 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
    17. Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
    18. Dim tWnd As Long, bWnd As Long, ncWnd As Long
    19. Private Sub Form_Load()
    20.     'KPD-Team 1998
    21.     'URL: [url]http://www.allapi.net/[/url]
    22.     'E-Mail: [email][email protected][/email]
    23.     Dim R As RECT
    24.     'Get the taskbar's window handle
    25.     tWnd = FindWindow("Shell_TrayWnd", vbNullString)
    26.     'Get the start-button's window handle
    27.     bWnd = FindWindowEx(tWnd, ByVal 0&, "BUTTON", vbNullString)
    28.     'Get the start button's position
    29.     GetWindowRect bWnd, R
    30.     'Create a new button
    31.     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&)
    32.     'Show our button
    33.     ShowWindow ncWnd, SW_NORMAL
    34.     'Hide the start button
    35.     ShowWindow bWnd, SW_HIDE
    36. End Sub
    37. Private Sub Form_Unload(Cancel As Integer)
    38.     'show the start button
    39.     ShowWindow bWnd, SW_NORMAL
    40.     'destroy our button
    41.     DestroyWindow ncWnd
    42. End Sub

  3. #3

    Thread Starter
    Addicted Member Beasts's Avatar
    Join Date
    Oct 2006
    Posts
    147

    Re: Get hWnd

    actually it duznt help v.much....
    how about a much easier one? like gettin calculator 's answer texbox?

  4. #4
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Get hWnd

    That is for the start button.

  5. #5

    Thread Starter
    Addicted Member Beasts's Avatar
    Join Date
    Oct 2006
    Posts
    147

    Re: Get hWnd

    who is doubting that?

  6. #6

    Thread Starter
    Addicted Member Beasts's Avatar
    Join Date
    Oct 2006
    Posts
    147

    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?

  7. #7
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Get hWnd

    you can use FindWindowEx:
    VB Code:
    1. lhWnd = FindWindowEx([i]CalculatorhWnd[/i], 0&, "Edit", vbNullString)
    2. ' lhWnd is now first textbox
    3. lhWnd = FindWindowEx([i]CalculatorhWnd[/i], [B]lhWnd[/B], "Edit", vbNullString)
    4. ' lhWnd is now second textbox

  8. #8

    Thread Starter
    Addicted Member Beasts's Avatar
    Join Date
    Oct 2006
    Posts
    147

    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:
    1. Option Explicit
    2. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    3. (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    4. Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
    5. (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    6. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    7. (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    8. Private Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    9.  
    10. Private Const WM_GETTEXTLENGTH = &HE
    11. Private Const WM_GETTEXT = &HD
    12.  
    13. Private Sub Command1_Click()
    14.  
    15.    
    16.     Dim lHwnd As Long
    17.     Dim sWindowText As String, lTextLen As Long
    18.  
    19.    
    20.     lHwnd = FindWindow(vbNullString, "PORTAL.NET")
    21.    
    22.    
    23.     lHwnd = FindWindowEx(lHwnd, ByVal 0&, "ThunderRT6Textbox", vbNullString)
    24.    
    25.  
    26.     lTextLen = SendMessage(lHwnd, WM_GETTEXTLENGTH, ByVal 0&, ByVal 0&)
    27.    
    28.       sWindowText = Space$(lTextLen + 1)
    29.    
    30.     Call SendMessage(lHwnd, WM_GETTEXT, ByVal lTextLen + 1, ByVal sWindowText)
    31.    
    32.     sWindowText = Left$(sWindowText, lTextLen)
    33.  
    34.     Text1 = sWindowText
    35.  
    36. End Sub

  9. #9
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Get hWnd

    EDIT: you're overwriting the parent lhWnd, so you'd have to do it like this:
    VB Code:
    1. Dim lhWnd As Long, lhWndChild As Long
    2. lhWnd = FindWindow(vbNullString, "PORTAL.NET")
    3. lhWndChild = FindWindowEx(lhWnd, 0&, "ThunderRT6Textbox", vbNullString)
    4. lhWndChild = FindWindowEx(lhWnd, lhWndChild, "ThunderRT6Textbox", vbNullString)
    5. '
    6. 'then work with lhWndChild

  10. #10
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Get hWnd

    Dosn't seem like you added bushmobile's suggestion.......

  11. #11

    Thread Starter
    Addicted Member Beasts's Avatar
    Join Date
    Oct 2006
    Posts
    147

    Re: Get hWnd

    i added it...but get the same results.


    VB Code:
    1. Option Explicit
    2. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    3. (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    4. Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
    5. (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    6. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    7. (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    8. Private Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    9.  
    10. Private Const WM_GETTEXTLENGTH = &HE
    11. Private Const WM_GETTEXT = &HD
    12.  
    13. Private Sub Command1_Click()
    14.  
    15.     Dim lHwnd As Long
    16.     Dim sWindowText As String, lTextLen As Long
    17. lHwnd = FindWindowEx(lHwnd, ByVal 0&, "ThunderRT6Textbox", vbNullString)
    18. lHwnd = FindWindowEx(lHwnd, lHwnd, "ThunderRT6Textbox", vbNullString)
    19.  
    20.     lHwnd = FindWindow(vbNullString, "PORTAL.NET")
    21.    
    22.     lHwnd = FindWindowEx(lHwnd, ByVal 0&, "ThunderRT6Textbox", vbNullString)
    23.    
    24.     lTextLen = SendMessage(lHwnd, WM_GETTEXTLENGTH, ByVal 0&, ByVal 0&)
    25.    
    26.     sWindowText = Space$(lTextLen + 1)
    27.    
    28.     Call SendMessage(lHwnd, WM_GETTEXT, ByVal lTextLen + 1, ByVal sWindowText)
    29.    
    30.     sWindowText = Left$(sWindowText, lTextLen)
    31.  
    32.     Text1 = sWindowText
    33.  
    34. End Sub

  12. #12

    Thread Starter
    Addicted Member Beasts's Avatar
    Join Date
    Oct 2006
    Posts
    147

    Re: Get hWnd

    test app test succeeded

  13. #13

  14. #14

    Thread Starter
    Addicted Member Beasts's Avatar
    Join Date
    Oct 2006
    Posts
    147

    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.

  15. #15
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Get hWnd

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim lhWnd As Long, lhWndChild As Long, lTextLen As Long
    3.     Dim sWindowText As String
    4.    
    5.     lhWnd = FindWindowEx(0&, 0&, vbNullString, "PORTAL.NET")
    6.     lhWndChild = FindWindowEx(lhWnd, 0&, "ThunderRT6Textbox", vbNullString)
    7.     lhWndChild = FindWindowEx(lhWnd, lhWndChild, "ThunderRT6Textbox", vbNullString)
    8.    
    9.     lTextLen = SendMessage(lhWndChild, WM_GETTEXTLENGTH, ByVal 0&, ByVal 0&)
    10.     sWindowText = Space$(lTextLen + 1)
    11.    
    12.     Call SendMessage(lhWndChild, WM_GETTEXT, ByVal lTextLen + 1, ByVal sWindowText)
    13.     sWindowText = Left$(sWindowText, lTextLen)
    14.  
    15.     Text1.Text = sWindowText
    16. 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.

  16. #16

    Thread Starter
    Addicted Member Beasts's Avatar
    Join Date
    Oct 2006
    Posts
    147

    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.....................

  17. #17
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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:
    1. Dim lhWnd As Long, lhWndFrame As Long, lhWndChild As Long
    2.    
    3.     ' Find Parent
    4.     lhWnd = FindWindowEx(0&, 0&, vbNullString, "PORTAL.NET")
    5.    
    6.     ' Find Frame
    7.     lhWndFrame = FindWindowEx(lhWnd, 0&, [i]FrameClass[/i], vbNullString)
    8.    
    9.     ' Find TextBox
    10.     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)

  18. #18

    Thread Starter
    Addicted Member Beasts's Avatar
    Join Date
    Oct 2006
    Posts
    147

    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
  •  



Click Here to Expand Forum to Full Width