Results 1 to 4 of 4

Thread: losefocus, get handle - (hard to explain)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Pittsburgh, PA
    Posts
    329
    i need to interface with another app. i'll take an answer to either question.

    simple:

    when my program loses focus to another program i need to get that program's handle.



    advanced:

    when the user sets the focus on another program, my program needs to ask the user if it wants my program to interface with the program that just got the focus. if the user says yes, then i need to get the text off of its textbox and put it on my form.



    thanks for any help i get.
    ______________

  2. #2
    Guest
    Ok, this may be the answer.

    This code will detect whether your form has lost focus or not (subclassing).

    Code:
    Option Explicit
    
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal ndx As Long, ByVal newValue As Long) As Long
    Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    ' This is used with the SetWindowLong API function.
    Const GWL_WNDPROC = -4
    
    Public Const WM_KILLFOCUS = &H8
    
    Dim saveHWnd As Long        ' The handle of the subclassed window.
    Dim oldProcAddr As Long     ' The address of the original window procedure
    
    Sub StartSubclassing(ByVal hWnd As Long)
        saveHWnd = hWnd
        oldProcAddr = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WndProc)
    End Sub
    
    Sub StopSubclassing()
        SetWindowLong saveHWnd, GWL_WNDPROC, oldProcAddr
    End Sub
    
    Function WndProc(ByVal hWnd As Long, ByVal uMsg As Long, _
        ByVal wParam As Long, ByVal lParam As Long) As Long
        ' Send the message to the original window procedure, and then
        ' return Windows the return value from the original procedure.
        WndProc = CallWindowProc(oldProcAddr, hWnd, uMsg, wParam, lParam)
        
    Select Case uMsg
        Case WM_KILLFOCUS
                'event fired here when form loses focus
        End Select
    End Function
    
    Private Sub Form_Load()
        StartSubclassing Me.hWnd
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        StopSubclassing
    End Sub
    And then you can use this code to get text from where ever the mouse is moved.

    Code:
    Type POINTAPI 'Declare types
    X As Long
    Y As Long
    End Type
    
    Declare Function GetCursorPos Lib "user32" _
    (lpPoint As POINTAPI) As Long
    Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Public Declare Function WindowFromPointXY Lib "user32" Alias "WindowFromPoint" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    
    
    Function GetCaption(hWnd)
    hwndLength% = GetWindowTextLength(hWnd)
    hwndTitle$ = String$(hwndLength%, 0)
    a% = GetWindowText(hWnd, hwndTitle$, (hwndLength% + 1))
    
    GetCaption = hwndTitle$
    End Function
    
    Private Sub Timer1_Timer()
    Dim pnt As POINTAPI
    MousePointer = 0
    GetCursorPos pnt
    yhwnd% = WindowFromPointXY(pnt.X, pnt.Y)
    Text1.text = GetCaption(yhwnd%)
    End Sub
    Sorry if that's not useful, but that's what came into mind.
    I may have to dream something else up.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Pittsburgh, PA
    Posts
    329
    thanks for responding matt.

    im having a problem with this part

    hwndLength% = GetWindowTextLength(hWnd)
    hwndTitle$ = String$(hwndLength%, 0)
    a% = GetWindowText(hWnd, hwndTitle$, (hwndLength% + 1))

    GetCaption = hwndTitle$

    it says hwndLength% and a% are not defined variables.
    i tried dim a% but it says that it wasnt the right datatype


    second, could you look at this thread for me:

    http://forums.vb-world.net/showthrea...threadid=29946


    thanks!
    ______________

  4. #4
    Guest
    Remove the statement Option Explicit and everything should work fine.

    Or if you want to declare them, declare them as an integer (not %).
    Code:
    Dim a As Integer

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