DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Active Window

  1. #1
    Shin Guest

    Active Window


    I am trying to get the title of active window using API calls as shown below.
    For some reason, it works only when the application itself is active.
    When the application is inactive and some other windows are active, textbox
    is simply blank.



    Private Declare Function GetActiveWindow Lib "user32" () As Long
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA"
    _
    (ByVal Hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long


    Function ActiveWindowCaption() As String
    Dim strCaption As String
    Dim lngLen As Long

    ' Create string filled with null characters.
    strCaption = String$(255, vbNullChar)
    ' Return length of string.
    lngLen = Len(strCaption)

    ' Call GetActiveWindow to return handle to active window,
    ' and pass handle to GetWindowText, along with string and its length.
    If (GetWindowText(GetActiveWindow, strCaption, lngLen) > 0) Then
    ' Return value that Windows has written to string.
    ActiveWindowCaption = strCaption
    End If
    End Function


    Private Sub Timer1_Timer()

    'show the title of the active window in textbox
    Form1.Text1.Text = ActiveWindowCaption
    End Sub

    Does anyone know what's wrong? Thank you in advance.


    -Shin


  2. #2
    Juergen Thuemmler Guest

    Re: Active Window

    > I am trying to get the title of active window using API calls as shown
    below.
    <->
    > Does anyone know what's wrong? Thank you in advance.


    Declare Function GetForegroundWindow& Lib "user32" ()
    Obtains the handle of the foreground window. This is the active window in
    the foreground application.

    Return Value
    Long—The handle of the foreground window.

    Juergen.




  3. #3
    Eduardo A. Morcillo [MS MVP VB] Guest

    Re: Active Window

    > I am trying to get the title of active window using API calls as shown
    below.
    > For some reason, it works only when the application itself is active.
    > When the application is inactive and some other windows are active,

    textbox
    > is simply blank.


    GetActiveWindow returns the active window of the calling thread. If you want
    to get the handle of the foreground window (the window in which the user is
    working) use GetForegroundWindow.


    --
    Eduardo A. Morcillo [MS MVP VB]
    http://www.domaindlx.com/e_morcillo



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center