Results 1 to 7 of 7

Thread: EnumChildWindows, SendMessage, it finally works but ,,,

  1. #1

    Thread Starter
    Hyperactive Member Al Smith's Avatar
    Join Date
    May 1999
    Location
    Marcellus, MI. USA
    Posts
    330

    Post

    Hi,
    I can finally send data to the textboxes of a third party program, thanks to several examples from Aaron Young and vbapi.com. As usual, I have more questions.
    How can I :
    1. "SetFocus?" on the 3rd-party's textbox? It will not save the new data unless selected?
    2. find out if the Parent is activated? I know its title.
    3. find the handle of the parent via its title?

    Finally, I would like a critique of the below. Be gentle as I'm only 10 months into VB and just starting trying to work with API's.
    Thanks,
    Al.

    Code:
    Public WinHandle()
    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
    Public Declare Function EnumChildWindows Lib "user32" _
    (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Private Const WM_SETTEXT = &HC
    
    Public Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
        Static winnum As Integer
        winnum = winnum + 1
        ReDim Preserve WinHandle(winnum)
        WinHandle(winnum) = hwnd
        EnumChildProc = 1
    End Function
    
    Sub Main()
        Dim retval As Long
        Parenthwnd = &H7FC
        retval = EnumChildWindows(Parenthwnd, AddressOf EnumChildProc, 0)
        EditData
    End Sub
    
    Private Sub EditData()
        Dim sText As String
        sText = "This is a test."
        Call SendMessage(WinHandle(44), WM_SETTEXT, Len(sText), ByVal sText)
    End Sub



  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    1. Knowing the hWnd of the application, you can easily set the Focus.

    Code:
    Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    
    
    Private Sub Command1_Click()
        Call SetForegroundWindow(lHwnd)
    End Sub
    Assuming that lHwnd is a valid window handle.

    2 - 3. If you know the exact caption name of the application you're looking, then it is very easy.
    Code:
    Public 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 Sub Command1_Click()
        Dim lHwnd As Long
        
        lHwnd = FindWindowEx(0, 0, vbNullString, "Caption of the Application")
    End Sub
    lHwnd is now holding the window handle of the "Caption of the Application".

    4. In your code you're ReDiming the array leaving first element empty. This line:


    Static winnum As Integer
    winnum = winnum + 1
    ReDim Preserve WinHandle(winnum)


    This will create your array as WinHandle(1), because your winnum is now 1. To fix your problem, move winnum = winnum + 1 to the end of the routine.

    Code:
    Public Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
        Static winnum As Integer
        
        WinHandle(winnum) = hwnd
        winnum = winnum + 1    
        ReDim Preserve WinHandle(winnum)
        EnumChildProc = 1
    End Function
    Also, Main routine has problems. This line:
    Parenthwnd = &H7FC.
    You can't assign hWnd to the variable like this, because each time you run the program hWnd will be different, as it being assign by Windows. So you have to use FindWindowEx from example above to get the hWnd of the Parent window you're looking for.

    Sorry for the hard critique...

    Edited by Serge on 03-12-2000 at 11:26 AM

  3. #3

    Thread Starter
    Hyperactive Member Al Smith's Avatar
    Join Date
    May 1999
    Location
    Marcellus, MI. USA
    Posts
    330

    Post

    Thanks Serge,
    Your critque wasn't harsh. I was afraid on something like "What a bunch of cr*p."
    I decided to get rid of the WinHandle variable and use lHwnd as the array with lHwnd(0) being the parent handle.
    FindWindowsEx works perfectly. (What's the difference between FindWindows and FindWindowsEx ?)
    The SetForegroundWindow isn't doing what I need. I can place the data into the box but unless I've clicked the box the data isn't saved. I'm thinking I need to send a Click or Select or ??? event to the box before sending the new data.

    Any suggestion or ideas will be greatly appreciated.

    Thanks,
    Al.




  4. #4
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    Try using the SetFocus API, declaration:
    Code:
    Private Declare Function SetFocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
    The FindWindow API allows you to search the Top Level Windows
    by comparing the Caption Text and/or the Class name.

    FindWindowEx allows you to search Window Handles at
    a specified level, ie.
    You can search all Handles belonging to a specified Parent Handle,
    (like searching for a specific control on a known Form).

    It also provides the option to choose where that search begins,
    by specifying the last window found in this parameter you
    can enumerate all windows that match the criteria,
    similar to EnumChildwindows only more specific,
    like the InStr function is to strings.

  5. #5

    Thread Starter
    Hyperactive Member Al Smith's Avatar
    Join Date
    May 1999
    Location
    Marcellus, MI. USA
    Posts
    330

    Post

    Hi Aaron,
    The SetFocus API didn't work.
    The 3rd party form I'm dealing with is a data entry/edit form (BaaN). It seems that until a textbox is physically selected the form is display-only. When any textbox is clicked, the bottom of the form changes from nothing to "Modify". There is also an "Edit" button on the toolbar of the form with a "Modify" selection in the dropdown menu.
    I'm trying to write something that will programatically update hundreds of records via the data edit forms. Manually, it involves repetitious and tedious clicks and keystrokes. Directly updating the databases is dangerous as links and indexes may not be created.

    Again, any and all suggestions will be greatly appreciated.

    Al.

  6. #6

    Thread Starter
    Hyperactive Member Al Smith's Avatar
    Join Date
    May 1999
    Location
    Marcellus, MI. USA
    Posts
    330

    Post It's me again.

    Hi,
    Ok. I can now select the textbox thus changing it into an edit box but I have to use this infernal timer loop
    before sending the actual text. (See below)
    Is there a way to tell when a SendMessage call has completed or is there something wrong with my code?

    Code:
    Private Const WM_SETTEXT = &HC
    Private Const WM_LBUTTONDOWN = &H201
    Private Const WM_LBUTTONUP = &H202
    
    Private Sub EditData()
        Dim sText As String
        sText = "This is a test."
        Call SendMessage(lHwnd(45), WM_LBUTTONDOWN, 0, 0)
        Call SendMessage(lHwnd(45), WM_LBUTTONUP, 0, 0)
            start = Timer                ' I want to
            Do Until Timer > start + 0.5 ' get rid of
            Loop                         ' this loop
        Call SendMessage(lHwnd(45), WM_SETTEXT, Len(sText), ByVal sText)
    End Sub
    Al.




  7. #7
    New Member
    Join Date
    Mar 2000
    Posts
    2

    I think Setfocus doesn't work because there is a flaw in the declaration. Try

    Public Declare Function MySetFocus Lib "user32.dll" Alias "SetFocus" (ByVal hwnd As Long) As Long

    You can then use MySetFocus(hwnd).

    Now for my problem: I want my app (which has been hiding) to jump up once a key is pressed. This is successfully done on the Win95 using VB's own form.setfocus.

    But it doesn't work on Win98 which simply 'flash' the app on the taskbar only. I've tried various APIs, like setactivewindow and setforegroundwindow without success.

    Can anyone help?

    -Samseng



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