Click to See Complete Forum and Search --> : losefocus, get handle - (hard to explain)
HAVocINCARNATE29
Sep 7th, 2000, 06:42 PM
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.
Ok, this may be the answer.
This code will detect whether your form has lost focus or not (subclassing).
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.
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. :rolleyes:
HAVocINCARNATE29
Sep 8th, 2000, 02:17 PM
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/showthread.php?threadid=29946
thanks!
Remove the statement Option Explicit and everything should work fine.
Or if you want to declare them, declare them as an integer (not %).
Dim a As Integer
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.