Results 1 to 8 of 8

Thread: API GURUS- FALL IN!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2000
    Posts
    352

    Exclamation Your Mission: AP1(actually API but close enough)

    Here is your mission should you choose to accept it:
    find the code that matches this goal.

    The Goal: Be able to detect at run time all programs running that show up in your taskbar. Once all have been detected, detect and retrieve the caption of each program.

    Possible Fallout: If I wish to use it as a background process, will it be able to detect another program has been launched and retrieve its caption as well?

    Effeciency Examination: Will it be efficient or possible, if run as a background application, to use a do... loop infinite loop, incorporating DoEvents(by the way if you know the syntax to make it even more efficient, a few lines of code that I have heard about, please include it as well), to keep the process going constantly.

    I thank you ahead of time for your hardwork and dedication. Good luck

    Joe


    This Post will self destruct in 3......2......

  2. #2
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666

    Cool

    Code:
    ' Display the title bar text of all top-level windows.  This
    ' task is given to the callback function, which will receive each handle individually.
    ' Note that if the window has no title bar text, it will not be displayed (for clarity's sake).
    Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    
    
    
    ' *** Place this code in a module.  This is the callback function. ***
    ' This function displays the title bar text of the window identified by hwnd.
    Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
      Dim slength As Long, buffer As String  ' title bar text length and buffer
      Dim retval As Long  ' return value
      Static winnum As Integer  ' counter keeps track of how many windows have been enumerated
    
      winnum = winnum + 1  ' one more window enumerated....
      slength = GetWindowTextLength(hwnd) + 1  ' get length of title bar text
      If slength > 1 Then ' if return value refers to non-empty string
        buffer = Space(slength)  ' make room in the buffer
        retval = GetWindowText(hwnd, buffer, slength)  ' get title bar text
        Debug.Print "Window #"; winnum; " : ";  ' display number of enumerated window
        Debug.Print Left(buffer, slength - 1)  ' display title bar text of enumerated window
      End If
    
      EnumWindowsProc = 1  ' return value of 1 means continue enumeration
    End Function
    
    
    
    
    ' *** Place this code wherever you want to enumerate the windows. ***
    Dim retval As Long  ' return value
    
    ' Use the above callback function to list all of the enumerated windows.  Note that lParam is
    ' set to 0 because we don't need to pass any additional information to the function.
    retval = EnumWindows(AddressOf EnumWindowsProc, 0)
    this will list all windows, some of those you don't want

    you might use a timer to check every x secods

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2000
    Posts
    352

    Exclamation Another Question

    First of all thank you very much. My first question is will the do.. loop update each window if it is run infinitely with that code checking within it? Will it slow down the system significantly when I incoporate DoEvents?

    Second, how can I find out which window is the active one? Can I check the caption of that as well? And once I have determined that, is there anyway of identifying the file currently selected in that window?

    Thank you again for your help,
    Joe

  4. #4
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666

    Question

    What do you mean by "File currently selected". Yes, you can get the active window caption, i will get the code and post it in about 5 min.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2000
    Posts
    352

    Exclamation Thanks for the prompt reply

    Thank you for the prompt reply. By file currently selected, I mean that if you click on an icon with mouse or move to it with keyboard, is there anyway of detecting what the name of that file is. Example: If I open my C drive and click once on the WinNT folder, it is the currently selected file. Which reminds me, and I apologize, but does the code work for NT? I forgot to mention that.

    Joe

  6. #6
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666
    The Code:
    Code:
    Private Sub GetActive()
    
    Dim hactive As Long  ' handle to the active window
    Dim retval As Long  ' return value
    
    hactive = GetActiveWindow()  ' get the handle of the program's active window
    ' The next three lines flash the window's title bar
    
    
    ' Display the text displayed in the title bar of window Form1
    Dim textlen As Long  ' receives length of text of the window
    Dim wintext As String  ' receives the text of the window
    Dim slength As Long  ' receives the length of the returned string
    
    ' Find out how many characters are in the window's text.
    ' Add 1 to compensate for the terminating null.
    textlen = GetWindowTextLength(hactive) + 1
    ' Make sufficient room in the buffer.
    wintext = Space(textlen)
    ' Retrieve the text of window Form1.
    slength = GetWindowText(hactive, wintext, textlen)
    ' Remove the empty space from the string, if any.
    wintext = Left(wintext, slength)
    ' Display the result.
    Debug.Print "The title bar of the active window is: "; wintext
    End Sub
    
    
    'Call With
    GetActive

  7. #7
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666
    It Should work with anything better than 3.1

    It regards to your other question(File selected), i know it's done it some programs, i will think about it.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2000
    Posts
    352

    Talking You've been a great help!

    I'll try to test that code out today; I don't know how much time I have though. Thanks very much. If you think of anything, just post here again and that'll be great. Thanks again.

    Joe

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width