Solved

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

Posted on 2000-03-16
11
206 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

823 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