Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Where can I find : window handle of VB app?

Status
Not open for further replies.

BaDi

Programmer
May 14, 2002
32
NL
Hi!

I have an application and I would like to know the window handle of it. I need this for an API-call.

Where can I find this handle?

Thanx in advance!

 
An application does not in itself have a window handle. Only windows do. Each form in a VB application is a window, and thus has it's own window handle, which is exposed at the form's hWnd property.
 
I agree with strongm. This will close any application using API calls by getting the handle of the desired window.

Put this in a module:

Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As Long) As Long


Declare Function sendmessagebystring Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long


Declare Function getwindow Lib "user32" Alias "GetWindow" (ByVal hWnd As Long, ByVal wCmd As Long) As Long


Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long


Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
Public Const WM_CLOSE = &H10
Public Const GW_CHILD = 5
Public Const GW_HWNDFIRST = 0
Public Const GW_HWNDLAST = 1
Public Const GW_HWNDNEXT = 2
Public Const GW_HWNDPREV = 3
Public Const GW_MAX = 5
Public Const GW_OWNER = 4

Function FindWindowByTitle(Title As String)
Dim a, b, Caption
a = getwindow(Form1.hWnd, GW_OWNER)
Caption = GetCaption(a)


If InStr(1, LCase(Caption), LCase(Title)) <> 0 Then
FindWindowByTitle = b
Exit Function
End If
b = a


Do While b <> 0: DoEvents
b = getwindow(b, GW_HWNDNEXT)
Caption = GetCaption(b)


If InStr(1, LCase(Caption), LCase(Title)) <> 0 Then
FindWindowByTitle = b
Exit Do
Exit Function
End If
Loop

End Function


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

End Function

Sub KillWin(Title As String)
Dim a, hWnd
hWnd = FindWindowByTitle(Title)
a = sendmessagebystring(hWnd, WM_CLOSE, 0, 0)
End Sub


And here's the code:

KillWin (&quot;Microsoft Word&quot;)
KillWin (&quot;Microsoft Excel&quot;) Swi
 
Not sure what you need it for:

All the forms in your VB application are owned by one small hidden form.
You can get the handle of this form, and even hook into it.

Passing GWL_HWNDPARENT will get the handle of the parent window the current form which is the owner of all of the forms for the application.

Public Declare Function GetWindowLong Lib &quot;user32&quot; Alias &quot;GetWindowLongA&quot; (ByVal hWnd As Long, ByVal nIndex As Long) As Long

GetWindowLong(Me.hWnd, GWL_HWNDPARENT)

Other than that, you can use the application thread ID for some other things such as some hooks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top