Solved

How do you get the App with Focus' Title Bar text?

Posted on 2000-03-16
11
205 Views
Last Modified: 2010-05-02
How do you find the title bar text for the application that has the focus?  For example, say i were to load "Notepad" by dclicking it on my desktop, giving it focus.  Now how would i go about letting my vb program knowing what that "in focus" app's title bar text is?  In this case it should return back: "Untitled - Notepad".  If i were to switch focus to another running app then my program should pick up that app's text too.

Thanks/
0
Comment
Question by:aniston
  • 5
  • 5
11 Comments
 
LVL 10

Expert Comment

by:caraf_g
ID: 2623256
Use the GetForegroundWindow and GetWindowText Apis.
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2623258
Sorry, that should've been a comment.
0
 
LVL 14

Accepted Solution

by:
mcrider earned 50 total points
ID: 2623284
Add the following to a module:


   Declare Function GetForegroundWindow Lib "user32" () As Long
   Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long


Then you can do:

   Dim ival As Long
   Dim lRet As Long
   Dim lText As String * 256

   ival = GetForegroundWindow()
   lRet = GetWindowText(ival, lText, 255)


lText will contain the title of the active window...


Cheers!®©
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2623304
er..... ?
0
 

Author Comment

by:aniston
ID: 2623362
Would you also know how to Set focus onto an app?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Expert Comment

by:caraf_g
ID: 2623395
Not recommended. But check out the SetForegroundWindow API
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2623396
You can do this with the VB native command AppActivate, however, it you want to do this with APIs, you need to do this (you need to have the window handle to do this, GetForegroundWindow returns a window handle)

Add the following to a module:

    ' ShowWindow() Commands
    Public Const SW_HIDE = 0
    Public Const SW_SHOWNORMAL = 1
    Public Const SW_NORMAL = 1
    Public Const SW_SHOWMINIMIZED = 2
    Public Const SW_SHOWMAXIMIZED = 3
    Public Const SW_MAXIMIZE = 3
    Public Const SW_SHOWNOACTIVATE = 4
    Public Const SW_SHOW = 5
    Public Const SW_MINIMIZE = 6
    Public Const SW_SHOWMINNOACTIVE = 7
    Public Const SW_SHOWNA = 8
    Public Const SW_RESTORE = 9
    Public Const SW_SHOWDEFAULT = 10
    Public Const SW_MAX = 10
   
    Declare Function SetFocusAPI Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
    Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long


Then you can do:

   SetFocusAPI iVal
   SetForegroundWindow iVal

You may also want to show the window if it's minimized:

   ShowWindow iVal, SW_SHOWNORMAL



Cheers!®©
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2623399
Private Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As Long) As Long
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2623461
By the way, if you want to know if the window is minimized and you need to show it, add the following to a module:

    Public Const WS_MINIMIZE = &H20000000
    Public Const GWL_STYLE = (-16)
    Declare Function GetWindowLong Lib "user32" _
        Alias "GetWindowLongA" (ByVal hWnd As Long, _
        ByVal nIndex As Long) As Long
    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
   

   Dim lWinfo As Long
   Dim lhWnd As Long

   lhWnd = FindWindow(vbNullString, "Win32api.txt - WordPad")
   lWinfo = GetWindowLong(lhWnd, GWL_STYLE)
   If (lWinfo And WS_MINIMIZE) = WS_MINIMIZE Then
       Debug.Print "minimized"
   Else
       Debug.Print "not minimized"
   End If



By the way, I am using the FindWindow API above to find the window handle... To use it, all you have to do is pass the title of the window as the 2nd argument...


Cheers!®©
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2623470
In my last comment, everything before the "Dim" statements go in a module.. Everything after goes in a subroutine...


Cheers!®©
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2623570
Thanks for the points! Glad I could help!


Cheers!®©
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Use Multiple Forms 4 49
add text to end of existing text in file 16 69
Updates not working for MS Windows 7 12 154
Zip Folders Using Chilkat Routines 1 12
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now