Focus apliccation


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Focus apliccation

  1. #1
    Join Date
    Aug 2004
    Posts
    43,023

    Focus apliccation

    [Originally posted by Javier Balkenende]

    Hi,
    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,

    Javier

  2. #2
    Join Date
    Aug 2004
    Posts
    43,023

    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:
    http://www.vbapi.com/ref/s/shellexecute.html

  3. #3
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Focus apliccation

    [Originally posted by Javier Balkenende]

    I have found a solution:

    Declarations:

    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

    Proccess:

    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)
    Do
    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))
    next


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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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