Results 1 to 6 of 6

Thread: VB.NET code to find active window title.

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2001
    Location
    NJ, USA
    Posts
    6

    VB.NET code to find active window title.

    Hi All,

    I am looking for VB.NET code which can help me get the active window (the one user has got focus to) title. I know you can do this using Win 32 API like this:

    VB Code:
    1. Private Declare Function GetActiveWindow Lib "user32" () As Long
    2. Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    3. Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    4.  
    5. Private Sub Form_Load()
    6.     Timer1.Interval = 5000
    7.     Timer1.Enabled = True
    8. End Sub
    9.  
    10. Private Sub Timer1_Timer()
    11.     Dim lWnd As Long
    12.     Dim strName As String
    13.    
    14.     lWnd = GetActiveWindow
    15.     strName = String(GetWindowTextLength(lWnd) + 1, Chr$(0))
    16.     GetWindowText lWnd, strName, Len(strName)
    17.     Debug.Print strName
    18.  
    19. End Sub


    but I want to use, Managed code and not API calls.
    Do you know of way of achieving the same in Managed code?
    Also, is there any event which tells me that Active window has been changed by user??

    JD
    ______________________________
    d.k.jariwala
    ~ simple thought simple act ~

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    you will need to use the api's , but you must replace the " Longs " with " Integers " and the String , with StringBuilder. here's a quick example...
    VB Code:
    1. [Color=Blue]Declare[/color] [Color=Blue]Function[/color] GetActiveWindow [Color=Blue]Lib[/color] "user32.dll" () [Color=Blue]As[/color] [Color=Blue]Integer
    2. [/color]    [Color=Blue]Declare[/color] [Color=Blue]Function[/color] GetWindowText [Color=Blue]Lib[/color] "user32.dll" [Color=Blue]Alias[/color] "GetWindowTextA" ([Color=Blue]ByVal[/color] hwnd [Color=Blue]As[/color] [Color=Blue]Integer[/color], [Color=Blue]ByVal[/color] lpString [Color=Blue]As[/color] System.Text.StringBuilder, [Color=Blue]ByVal[/color] cch [Color=Blue]As[/color] [Color=Blue]Integer[/color]) [Color=Blue]As[/color] [Color=Blue]Integer
    3. [/color]    [Color=Blue]Declare[/color] [Color=Blue]Function[/color] GetWindowTextLength [Color=Blue]Lib[/color] "user32.dll" [Color=Blue]Alias[/color] "GetWindowTextLengthA" ([Color=Blue]ByVal[/color] hwnd [Color=Blue]As[/color] [Color=Blue]Integer[/color]) [Color=Blue]As[/color] [Color=Blue]Integer
    4.  
    5.  
    6. [/color]    [Color=Blue]Private[/color] [Color=Blue]Sub[/color] Button1_Click([Color=Blue]ByVal[/color] sender [Color=Blue]As[/color] System.Object, [Color=Blue]ByVal[/color] e [Color=Blue]As[/color] System.EventArgs) [Color=Blue]Handles[/color] Button1.Click
    7.  
    8.         [Color=Blue]Dim[/color] hwnd [Color=Blue]As[/color] [Color=Blue]Integer[/color] = GetActiveWindow
    9.         [Color=Blue]Dim[/color] sBuild [Color=Blue]As[/color] [Color=Blue]New[/color] System.Text.StringBuilder(GetWindowTextLength(hwnd) + 1)
    10.         GetWindowText(hwnd, sBuild, sBuild.Capacity)
    11.         MessageBox.Show(sBuild.ToString)
    12.  
    13.     [Color=Blue]End[/color] [Color=Blue]Sub[/color]
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    I would think since you can get a handle to all open windows, .net or not with the below code, there should be a way to figure which one has active focus... perhaps the Management namespace contains class or classes than can extract that info.

    You may have to buy a $40 book on WMI, because no one seems to post much information on the Net about it.

    VB Code:
    1. 'form level variable
    2.  Private c As Array
    3.  
    4.  
    5.  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.         c = Process.GetProcesses
    7.         Dim i As Integer
    8.         For i = 0 To c.Length - 1
    9.  
    10.             ListBox1.Items.Add(c(i))
    11.         Next
    12.  
    13.  
    14.  
    15.  
    16.  
    17.     End Sub
    18.  
    19.     Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    20.         Dim a As Process = c(ListBox1.SelectedIndex)
    21.         MessageBox.Show(a.MainWindowTitle & " [Handle = " _
    22.          & a.MainWindowHandle.ToString & "]")
    23.  
    24.  
    25.     End Sub
    Last edited by nemaroller; Nov 23rd, 2003 at 09:21 AM.

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2001
    Location
    NJ, USA
    Posts
    6
    Hi

    Dynamic sysop,

    Thanks for suggesting the required code changes so as to make it work in .NET. But what I am really looking for is 'managed code' to achieve the same!

    And nemaroller,

    yes, I looked in to process class. I went through all the properties but couldnt find a property/member/method, which could tell me whether the given process has got user's focus.

    and by WMI, you mean 'Windows Management I?????'

    Thanks for your help,

    JD
    ______________________________
    d.k.jariwala
    ~ simple thought simple act ~

  5. #5
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    yes I meant Windows Management. There may be other ways beside using WMI, but the only thing you lose by using the API is platform independence. And at this time, .Net really only works on Windows systems.

  6. #6

    Thread Starter
    New Member
    Join Date
    Dec 2001
    Location
    NJ, USA
    Posts
    6
    Platform depedence?

    It's not my concern anyway. We are 100% Windows shop and I am happy with it!

    Btw, I couldnt find any namespace named 'Windows Management' , could you point me to online MSDN document which talks about it??

    Thanks once again for your help!
    JD
    ______________________________
    d.k.jariwala
    ~ simple thought simple act ~

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