Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > API > Getting hwnd of active window by parent hwnd

Thread Tools Display Modes
Old 11-08-2011, 06:07 PM
ente25 ente25 is offline
Join Date: May 2009
Posts: 2
Default Getting hwnd of active window by parent hwnd


sorry - i have a complexe problem.
I have the hwnd of a (non visible) form .. which is part of a application. (not mine).

I'm able to identify the hwnd .. because i can identify a unique flag.

With the ShowWindow (SW_RESTORE) and SetForegroundWindow API's i can force to display the application / put the window in the foreground .. but the focus is not on the "active" window (or dialog) of this application.

I think that i must enum the child hwnd's of the parent of the known hwnd .. in order to get the active (maybe modal) window.
I have tried to use the GetTopWindow api .. but if i pass the known or the parent of the known hwnd .. i always get 0.

Can anyone give me a hint .. how i can enum the hwnd's of a application structure .. in order to identify the "topmost" window hwnd?

Thank you
Reply With Quote
Old 11-08-2011, 06:27 PM
ente25 ente25 is offline
Join Date: May 2009
Posts: 2

hmm .. 10 minutes later i found the solution.

enclosed the solution:
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Const GW_OWNER = 4

Private Declare Function GetActiveWindow Lib "user32" () As Long

Private Function FindTopVBWindow(ByVal hWNdStart As Long, ByVal hWndVB As Long) As Long
  Dim lhWnd As Long
    lhWnd = GetWindow(hWNdStart, GW_OWNER)
    If lhWnd = 0 Or lhWnd = hWndVB Then
      Exit Function
      FindTopVBWindow = lhWnd
      hWNdStart = lhWnd
    End If
End Function

'use with:

 1) focus the hidden dialog
 2) get hwnd of the Top most window .. with x = FindTopVBWindow(hwnd, GetActiveWindow())
 3) change the focus to this hwnd

done - worked properly
Reply With Quote
Old 11-08-2011, 06:43 PM
surfR2911 surfR2911 is offline
Join Date: Oct 2009
Posts: 719
Default The spy ++ tool and misc finding hwnd stuff

Glad you found a solution..
But others who happen across this thread, might find this tool useful for investigating hwnds of programs you have not written yourself:
Microsoft Spy++ or Spyxx for download
MSDN page for Spy++, Screenshot

Here's also these semi-random links:
How To Get a Window Handle Without Specifying an Exact Title
List top-level windows on the system
XVBT Forum thread: How to obtain hWnd?
Reply With Quote

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..