Google Answers Logo
View Question
 
Q: .NET -- Determine Active Application on System (a.k.a. which app has focus) ( No Answer,   6 Comments )
Question  
Subject: .NET -- Determine Active Application on System (a.k.a. which app has focus)
Category: Computers > Programming
Asked by: rayrayrayray-ga
List Price: $10.00
Posted: 22 Jul 2005 13:22 PDT
Expires: 21 Aug 2005 13:22 PDT
Question ID: 546728
I've been searching on the net today for a .NET code snippit that
determines what application currently has focus.

I have run across a few examples that enumerate through Windows
(either via API or using .NET's own Process object), but have not
found the example I'm looking for.

Basically, I want a routine that'll return the name and ProcessID of the current
active application whether it be Word, Excel, notepad, etc.

VB is my preferred language, but any other .NET will do.

Request for Question Clarification by mathtalk-ga on 28 Jul 2005 13:56 PDT
Hi, rayrayray-ga:

In the Comments section you mention that this issue can now be closed.

You are in control of this; there is a button at the upper right on
this page to Close the Question.

However once the Question is Expired in this fashion, no further
Comments can be posted to it.

regards, mathtalk-ga
Answer  
There is no answer at this time.

Comments  
Subject: Re: .NET -- Determine Active Application on System (a.k.a. which app has focus)
From: pmmbala1976-ga on 23 Jul 2005 23:49 PDT
 
Hi

Using VB,you can do using this below link page. it will retun the all the deatails.

http://vbnet.mvps.org/index.html?code/system/getguithreadinfo.htm

Thanks
bala
Subject: Re: .NET -- Determine Active Application on System (a.k.a. which app has focus)
From: rayrayrayray-ga on 24 Jul 2005 06:21 PDT
 
That's EXACTLY what I needed.

Thanks! :)
Subject: Re: .NET -- Determine Active Application on System (a.k.a. which app has focus)
From: rayrayrayray-ga on 26 Jul 2005 09:35 PDT
 
{whoops}
Actually, this would be what I need if I were coding in VB6.  

The sample provided does not work in VB .NET.

I first tried migrating the whole code sample and then tried just the
API call to GetGUIThreadInfo.

Nothing works.  I don't get an error, but I also don't get the
processID and all the other goodies this function is supposed to
provide.

Any help out there? 

I either need a VB .NET sample that will successfully call
GetGUIThreadInfo or a new code sample altogether for .NET that will
give me the active application.
Subject: Re: .NET -- Determine Active Application on System (a.k.a. which app has focus)
From: pmmbala1976-ga on 27 Jul 2005 09:48 PDT
 
Ok. i converted the vb code into vb.net. Please download from it from
below link and let me know if any problems. You need to fine tuning
the code based on ur request. The project contain all the basic stpes.

http://www.transferbigfiles.com/Download.aspx?id=8103a097-6de7-48db-85e1-21d0d4c053c9

thanks
bala
Subject: Re: .NET -- Determine Active Application on System (a.k.a. which app has focus)
From: rayrayrayray-ga on 28 Jul 2005 06:25 PDT
 
NOTE:  This issue can now be closed.

Hey bala,

Thank you for taking the time to do this.  The code in my project
*looked* fine, but apparently I had something setup incorrectly.

I started a new project, copied over your code and was able to make it work.

For anyone else who's curious, do this:
1.  Start a new .NET project.
2.  Put a Listbox on the form (Listbox1)
3.  Put a timer on the form (Timer1)
4.  Insert the following code where appropriate:

  Private Const GUI_CARETBLINKING As Integer = &H1S
  Private Const GUI_INMOVESIZE As Integer = &H2S
  Private Const GUI_INMENUMODE As Integer = &H4S
  Private Const GUI_SYSTEMMENUMODE As Integer = &H8S
  Private Const GUI_POPUPMENUMODE As Integer = &H10S
  Private Const GUI_16BITTASK As Integer = &H20S 'winver >= 5.01
  Private Const LB_SETTABSTOPS As Integer = &H192S
  Private Const WM_GETTEXT As Integer = &HDS
  Private Const WM_GETTEXTLENGTH As Integer = &HES

  Private Structure RECT
    Dim Left_Renamed As Integer
    Dim Top_Renamed As Integer
    Dim Right_Renamed As Integer
    Dim Bottom_Renamed As Integer
  End Structure

  Private Structure GUITHREADINFO
    Dim cbSize As Integer
    Dim flags As Integer
    Dim hwndactive As Integer
    Dim hwndFocus As Integer
    Dim hwndCapture As Integer
    Dim hwndMenuOwner As Integer
    Dim hwndMoveSize As Integer
    Dim hwndcaret As Integer
    Dim rcCaret As RECT
  End Structure
  Private Declare Function GetGuiThreadInfo Lib "user32" Alias
"GetGUIThreadInfo" (ByVal idThread As Integer, ByRef lpgui As
GUITHREADINFO) As Integer
  Declare Function GetCurrentThreadId Lib "kernel32" () As Integer
  Private Declare Function SendMessage Lib "user32" Alias
"SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal
wParam As Integer, ByRef lParam As String) As Integer
  Private Declare Function GetWindowTextLength Lib "user32" Alias
"GetWindowTextLengthA" (ByVal hwnd As Integer) As Integer
  Private Declare Function GetWindowText Lib "user32" Alias
"GetWindowTextA" (ByVal hwnd As Integer, ByVal lpString As String,
ByVal cch As Integer) As Integer
  Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
    On Error Resume Next

    Dim gui As GUITHREADINFO
    Static numcalls As Integer


    'cbSize must be set before calling
    gui.cbSize = Len(gui)

    If GetGuiThreadInfo(0, gui) <> 0 Then

      'numcalls is just a counter to increment
      'a line in the list to show the code is
      'working when you rest in one window
      numcalls = numcalls + 1

      With ListBox1

        .Items.Add(CStr(numcalls))
        Dim ss
        ss = gui.hwndactive
        .Items.Add("active window hwnd:" & ss)
        ss = GetActiveWindowTitle(gui.hwndactive)
        .Items.Add("   active window title:" & ss)
        ss = gui.hwndCapture
        .Items.Add("mouse capture hwnd:" & ss)
        ss = gui.hwndcaret
        .Items.Add("showing caret hwnd:" & ss)
        ss = GetCaretWindowText(gui.hwndcaret)
        .Items.Add("caret window text:" & ss)
        ss = -gui.hwndFocus
        .Items.Add("keyboard focus hwnd:" & ss)
        ss = gui.hwndMenuOwner
        .Items.Add("active menu owner hwnd:" & ss)
        ss = gui.hwndMoveSize
        .Items.Add("move or size loop hwnd:" & ss)

        .Items.Add("caret rect (l/r t/b):" & vbTab &
gui.rcCaret.Left_Renamed & "/" & gui.rcCaret.Right_Renamed & " " &
gui.rcCaret.Top_Renamed & "/" & gui.rcCaret.Bottom_Renamed)

      End With 'with list1

    Else

      Me.ListBox1.Items.Add("Error")

    End If

  End Sub

  Private Function GetActiveWindowTitle(ByVal hwndactive As Integer) As String

    Dim nLength As Integer
    Dim res As Integer
    Dim buff As String

    'GetWindowText returns the title
    'of the window specified as hwndactive
    If hwndactive <> 0 Then

      nLength = GetWindowTextLength(hwndactive)

      If nLength <> 0 Then

        buff = Space(nLength + 1)

        res = GetWindowText(hwndactive, buff, nLength + 1)

        If res <> 0 Then
          GetActiveWindowTitle = buff.Substring(0, res)
          Exit Function
        End If 'if res

      End If 'if nlength

    End If 'if hwndactive

    GetActiveWindowTitle = "(not available)"

  End Function

  Private Function GetCaretWindowText(ByVal hwndcaret As Integer) As String

    Dim nLength As Integer
    Dim res As Integer
    Dim buff As String

    'WM_GETTEXT retrieves the text
    'from edit and rich text controls
    If hwndcaret <> 0 Then

      nLength = SendMessage(hwndcaret, WM_GETTEXTLENGTH, 0, 0)

      If nLength <> 0 Then

        buff = Space(nLength + 1)

        res = SendMessage(hwndcaret, WM_GETTEXT, nLength + 1, buff)

        If res <> 0 Then
          GetCaretWindowText = buff.Substring(0, res)
          Exit Function
        End If 'if res

      End If 'if nlength

    End If 'if hwndcaret

    GetCaretWindowText = "(not available)"

  End Function




'TinkerBee
Subject: Re: .NET -- Determine Active Application on System (a.k.a. which app has focus)
From: pmmbala1976-ga on 28 Jul 2005 12:20 PDT
 
In Form load event you should code this event.

Timer1.Interval = 1000 ' 1 sec interval
Timer1.Enabled = True

Thanks
Bala

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy