Solved

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

Posted on 2000-03-16
11
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
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…

690 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