Solved

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

Posted on 2000-03-16
11
203 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
Comment Utility
Use the GetForegroundWindow and GetWindowText Apis.
0
 
LVL 10

Expert Comment

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

Accepted Solution

by:
mcrider earned 50 total points
Comment Utility
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
Comment Utility
er..... ?
0
 

Author Comment

by:aniston
Comment Utility
Would you also know how to Set focus onto an app?
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 10

Expert Comment

by:caraf_g
Comment Utility
Not recommended. But check out the SetForegroundWindow API
0
 
LVL 14

Expert Comment

by:mcrider
Comment Utility
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
Comment Utility
Private Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As Long) As Long
0
 
LVL 14

Expert Comment

by:mcrider
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the points! Glad I could help!


Cheers!®©
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 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

14 Experts available now in Live!

Get 1:1 Help Now