DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2004

    Focus apliccation

    [Originally posted by Javier Balkenende]

    I have a question about setting the focus to an application that I launch with shell function.
    I need this because if the user clicks anything in the windows of my program, then it gets the focus, and the window of the application I launched is hide.

    The code is like this:

    Dim RetVal
    RetVal = Shell ("c:\path\Exename.exe", 1)

    Now I have the thread Id in RetVal, but I need the handle of the window so I can use SetForegroundWindow API call to set the application to the top level in the Windows list shown in the screen.

    What I need is a way to find the hWnd of the main window of the application that was launched from the thread id. How?

    Thank in advance,


  2. #2
    Join Date
    Aug 2004

    Re:Focus apliccation

    [Originally posted by Andrew Chin]

    why not just have the Shell function start the app in focus:
    Shell "c:\windows\calc.exe", vbNormalFocus
    you dont even need to know the return value.
    if you really need the handle, you can use the ShellExecute API call, that returns the handle. see:

  3. #3
    Join Date
    Aug 2004

    Re:Focus apliccation

    [Originally posted by Javier Balkenende]

    I have found a solution:


    Declare Function GetDesktopWindow Lib "user32" () As Long
    Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long

    Declare Function SetForegroundWindow& Lib "user32" (ByVal hwnd As Long)

    Public Const GW_HWNDFIRST = 0
    Public Const GW_HWNDLAST = 1
    Public Const GW_HWNDNEXT = 2
    Public Const GW_HWNDPREV = 3
    Public Const GW_OWNER = 4
    Public Const GW_CHILD = 5
    Public Const GW_MAX = 5

    dim hTread as long
    dim hWindows


    hThread = shell ("drive:\path\exename.exe", 1)
    ' perhaps is a good idea to include a line here: sleep 100
    hWindows = GetTheadWindows (hThread)

    Private Function GetTheadWindows (nThread as long) as Variant
    dim hWindows() as long
    dim hhWnd as long

    ReDim hWindows(0)
    hhWnd = GetDesktopWindow()
    hhWnd = GetWindow(hhWnd, GW_CHILD)
    If TheadOfWindow(hhWnd) = nThread Then
    ReDim Preserve hWindows(UBound(hWindows) + 1)
    hWindows(UBound(hWindows)) = hhWnd
    End If
    hhWnd = GetWindow(hhWnd, GW_HWNDNEXT)
    Loop While hhWnd <> 0

    GetTheadWindows = hWindows
    End Function

    Private Function TheadOfWindow(nhWnd As Long) As Long
    Dim dl&
    Dim nhWndProcess&

    dl& = GetWindowThreadProcessId(nhWnd, nhWndProcess)
    TheadOfWindow = nhWndProcess
    End Function

    So, after launching the application, I have in the array hWindows the list of Windows of the application.

    and I can do this to bring it to the front:

    dim C as long

    for C = 1 to Ubound(hWindows)
    SetForegroundWindow& (hWindows(C))

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.