Results 1 to 9 of 9

Thread: How do I find a specific control's hWnd

  1. #1

    Thread Starter
    Fanatic Member scr0p's Avatar
    Join Date
    Oct 2002
    Location
    VA
    Posts
    720

    How do I find a specific control's hWnd

    I found this app's hWnd, but now I want to send a mouse click to 1 of the buttons on it, lets say AIM's warn button? or something, how would I find a specific hWnd of a button so I can send commands to it.
    asdf

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Use Spy++ to find the child window structure to the "BUTTON" window class.
    This will also give you all the window classes that are nested to the button.
    Then use (depending upon the number of nested windows) FindWindowEx API(s)
    to obtain the handel of the button you need.
    Then SendMessage with the button handel and the BM_CLICK parameter
    to simulate the click on the button.

  3. #3

    Thread Starter
    Fanatic Member scr0p's Avatar
    Join Date
    Oct 2002
    Location
    VA
    Posts
    720
    hmm, How do I do this?

    "Use Spy++ to find the child window structure to the "BUTTON" window class.
    This will also give you all the window classes that are nested to the button."

    I made a really simple API spy that gets the classname and hWnd that I use, using WindowFromPoint or w/e, anyway - the classname of the warn button is _Oscar_IconBtn, what do I do with this? If I need it
    asdf

  4. #4

    Thread Starter
    Fanatic Member scr0p's Avatar
    Join Date
    Oct 2002
    Location
    VA
    Posts
    720
    I got it, How do I find which button I need? right now it sends to the right most button (Send).. I want it to go to the left most button.

    num2 = FindWindowEx(irc_hWnd, ByVal 0&, "_Oscar_IconBtn", vbNullString)
    asdf

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    If you have Visual Studio then its Microsoft Visual Studio 6.0 Tools - Spy++.

    If not thenyou use the window classname in the FindwindowEx API.

    Code:
    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 FindWindowEx Lib "user32" Alias "FindWindowExA" ( _ 
    	 ByVal hWnd1 As Long, ByVal hWnd2 As Long,ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Private Const BM_CLICK = &HF5
    
    'handel 1 is the parent window handel.
    'handel 2 is the child window (button)
    'lpsz1 is the class name ("BUTTON")
    'lpsz2 is the caption (if any) ("&Ok")
    'This will return the handel to the button if found.
    'Then ...
    SendMessage hwndButton, BM_CLICK, 0&, 0&

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    You need to add the button's caption to the FindWindowEx

    Code:
    num2 = FindWindowEx(irc_hWnd, ByVal 0&, "_Oscar_IconBtn", vbNullString)
    
    To...
    
    num2 = FindWindowEx(irc_hWnd, ByVal 0&, "_Oscar_IconBtn", "&Ok")
    
    Then...
    SendMessage num2 , BM_CLICK, 0&, 0&

  7. #7

    Thread Starter
    Fanatic Member scr0p's Avatar
    Join Date
    Oct 2002
    Location
    VA
    Posts
    720
    Thanks, what if the caption is a graphic? just like a Open button and it has a pic of a new file? Like this warn button doesnt work if I use "&Warn", it mihgt be a graphic.
    asdf

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    The &Warn would be if the command button's caption had an accelerator shortcut key - Warn
    So if not try "Warn"

    On the other question its a bit more difficult.

    What you need is to try to get the BUTTONs coordinates if the
    form does not allow resizing then it will be the same all the time.

    The to get the BUTTONs coordinates is to use -

    Code:
    Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type
    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    ALSO, GET THE NEXT BUTTON IF WRONG
    
    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Private Const GW_HWNDNEXT = 2
    You may have to get the handel of a control with a caption
    nearby in order to test if the .Top or .Left coordinates are within
    an accetable range for example

  9. #9

    Thread Starter
    Fanatic Member scr0p's Avatar
    Join Date
    Oct 2002
    Location
    VA
    Posts
    720
    yeah I tried both, warn and &warn.

    Thanks, will try this soon.
    asdf

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