Quote:
>I am writing an application that uses SENDKEYS to switch back to the previous application
>but would like to know how to get the name of the application that will be switched
>to.
>I know that it is the always the second application in the Task List but I don't know
>how to acces that information.
>I have read the VBKB and found how to access the Master List but this is not what I
>want. Is there anybody out there who can help me with this problem.
>Thanks in advance.
>Raymond.
*******************************************************************
I recently asked how is it possible to find the title of the next window
in the task list. I was writing a program that used SENDKEYS to Alt+Tab
back to an application and insert text. I needed to know the name of the
application before using SENDKEYS. As you can imagine this could cause
problems if the user was switching back to Program Manager or File
Manager, etc.
After searching and re-searching the Visual Basic Knowledgebase, I
managed to write a solution that seems to work by combining information
from:
Q78001 How to Get Windows Master List (Task List) Using Visual Basic
Q112649 How to Get a Window's Class Name and Other Window Attributes
---------------------------------------------------------------------------
This code works for the program that I have written. If you
try it out and get unexpected results, I would be interested
in knowing. Depending on your program, you may need to change
the value of ParentWindowCount in the Do Until line.
---------------------------------------------------------------------------
To see how this works, do the following:
1. Start a new project.
2. Add the following declarations to the general declarations section
of Form 1:
'Each of the declare statements should be on one line.
Declare Function GetWindow Lib "user" (ByVal hWnd%, ByVal wCmd%) As Integer
Declare Function GetWindowText Lib "user" (ByVal hWnd%, ByVal lpSting$, ByVal nMaxCount%) As Integer
Declare Function GetWindowTextLength Lib "user" (ByVal hWnd%) As Integer
Declare Function GetParent Lib "User" (ByVal hWnd As Integer) As Integer
Declare Function GetActiveWindow Lib "User" () As Integer
'Declare constants used by GetWindow.
Const GW_HWNDFIRST = 0
Const GW_HWNDNEXT = 2
3. Add a timer control to Form1.
4. Set the Interval property of the timer to 500.
5. Add the following code to the timer's timer event:
Sub Timer1_Timer()
Call GetNextTaskName
End Sub
6. Add a label to Form1, name it lblApplication and remove the Caption.
7. Add the following code to the general declarations section of Form1:
Sub GetNextTaskName ()
Dim CurrWnd, ParentWindowCount As Integer, Length, ListItem$, x
Dim hWndParent As Integer
'Check that the current form is the TopMost Window, if not, exit the sub
If Me.hWnd <> GetActiveWindow() Then Exit Sub
'Get the hWnd of the first item in the master list
'so we can process the task list entries (top-level only).
CurrWnd = GetWindow(Me.hWnd, GW_HWNDFIRST)
'Get the details for the third window in the master list that does not
'have a parent window.
'The first window is the current form, the second window is
'the background window for the project, the third should be the next
'task in the task list.
Do Until ParentWindowCount = 3 'You may need to change this value
'Get the length of task name identified by CurrWnd in the list.
Length = GetWindowTextLength(CurrWnd)
'Get task name of the task in the master list.
ListItem$ = Space$(Length + 1)
Length = GetWindowText(CurrWnd, ListItem$, Length + 1)
'If there is a task in the list, check to see if it has a parent window
If Length > 0 Then
'Check to see if the CurrWnd has a parent window.
hWndParent = GetParent(CurrWnd)
' If there is no parent then increase the ParentWindowCount.
If hWndParent = 0 Then
ParentWindowCount = ParentWindowCount + 1
End If
End If
'Get the next task list item in the master list.
CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT)
'Process Windows events.
x = DoEvents()
Loop
'If the Application name is different to the current caption of the label,
'update it, otherwise exit the sub
If lblApplication.Caption = Left$(ListItem$, Length) Then
Exit Sub
Else
lblApplication.Caption = Left$(ListItem$, Length)
lblApplication.Refresh
End If
End Sub
8. Run the program. The application name that appears in the label's
caption should be the application that will be switched to if you
press Alt+Tab. Try switching to different applications and then switch
back to the Visual Basic application to see the label update.
---------------------------------------------------------------------------
NOTE: My application has code in it that prevents more than one instance
of the program to be run. I'm only guessing but I think this could
affect how the above code runs.
To prevent more than one instance of the program running, see
Q102480 How to Prevent Multiple Instances of a VB Application
---------------------------------------------------------------------------