CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: How to get top most dialog.

  1. #1
    Join Date
    Oct 2002
    USA, California

    How to get top most dialog.

    I need to get a handle to the top most dialog in my application. The name of this window is unknown. The top most dialog could be a form, message box, or common dialog. The application might not be in the active foregound window.

    Are there any Win API calls that can help me here???

    EnumChildWindows(), but it is only listing my main form's controls, not its child forms/windows.

    GetWindow(MainForm.hWnd, GW_CHILD) and GetWindow(MainForm.hWnd, GW_HWNDFIRST), but they seem to be only returning the form's controls as well.

    GetTopWindow(MainForm.hWnd), but the handle it's returning does not belong to the top dialog.

    I've searched the VB, VisualC++, and MSDN forums. No luck! I can't be the only one who's ever tried this!

  2. #2
    Join Date
    Apr 2003


    Try my code
    Declare Function EnumThreadWindows Lib "user32" _
       (ByVal dwThreadId&, ByVal lpfn&, ByRef lParam&) As Long
    Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
    Declare Function IsWindowEnabled Lib "user32" (ByVal hwnd As Long) As Long
    Function EnumWndProc(ByVal hwnd&, ByRef hWndStorage&) As Long
       ' we have to look for a window that is visible and enabled
       If (IsWindowVisible(hwnd) <> 0) And (IsWindowEnabled(hwnd) <> 0) Then
          hWndStorage = hwnd
          EnumWndProc = 0 ' stop enumeration if we found 1
          Exit Function
       End If
       EnumWndProc = 1
    End Function
    hwnd = 0   ' initialize hwnd to zero
    Call EnumThreadWindows(App.ThreadID, AddressOf EnumWndProc, hwnd)
    'hwnd holds now the active dialog.
    I also found GetGUIThreadInfo but it will not work if the application is not the active application.

    Hope it will help you
    Last edited by rxbagain; June 15th, 2003 at 03:42 AM.

  3. #3
    Join Date
    Oct 2002
    USA, California
    Thanks. I will give it a try tomorrow.

  4. #4
    Join Date
    Oct 2002
    USA, California


    Hallelujah, it works!!! rxbagain, you get another 5 stars from me! You are the MAN!

    I've simplified the code here so that everyone else can use it. This code must be in a *.bas module, because callbacks can't be in forms.
    ' Define win32 API.
    Private Declare Function EnumThreadWindows Lib "user32" _
      (ByVal dwThreadId As Long, ByVal lpfn As Long, ByRef lParam As Long) As Long
    Private Declare Function IsWindowVisible Lib "user32" _
      (ByVal hWnd As Long) As Long
    Private Declare Function IsWindowEnabled Lib "user32" _
      (ByVal hWnd As Long) As Long
    ' Returns the hWnd of the top most dialog in the application.
    ' Returns zero if top dialog not found (which should never happen).
    Public Function GetTopDialog() As Long
       Dim hWnd As Long
       hWnd = 0
       EnumThreadWindows App.ThreadID, AddressOf EnumThreadWndProc, hWnd
       GetTopDialog = hWnd
    End Function
    ' Windows calls this function to enumerate all open windows in app.
    ' This function should only be called by GetTopDialog().
    ' Stops enumerating when it finds the top most dialog.
    ' hWnd of top dialog is returned via argument lParam by reference.
    Private Function EnumThreadWndProc(ByVal hWnd As Long, ByRef lParam As Long) As Long
       EnumThreadWndProc = 1
       If (IsWindowVisible(hWnd) <> 0) And (IsWindowEnabled(hWnd) <> 0) Then
          EnumThreadWndProc = 0       ' Found it!
          lParam = hWnd                ' Return hWnd to caller.
       End If
    End Function
    Last edited by JQuick; June 16th, 2003 at 01:20 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)