VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

VB.NET Source Code


You're Browsing As A Guest! Register Now...
Become a VB.NET Expert!

Join 358,488 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,193 people online right now.Registration is fast and FREE... Join Now!




Get the text and handle of the active window

Submitted By: jacobjordan
Actions:
Rating:
Views: 7,182

Language: VB.NET

Last Modified: July 7, 2008
Instructions: Use the GetWindowText() function. I know, there is a lot of other stuff in there, but just ignore the rest.

Snippet


  1.     ' Imports
  2.     <Runtime.InteropServices.DllImport("user32.dll")> _
  3.     Private Function GetForegroundWindow() As IntPtr
  4.     End Function
  5.     <Runtime.InteropServices.DllImport("user32.dll")> _
  6.     Private Function GetWindowText(ByVal hWnd As IntPtr, ByVal lpWindowText As Text.StringBuilder, _
  7.     ByVal nMaxCount As Integer) As Integer
  8.     End Function
  9.     ' /Imports
  10.  
  11.     ''' <summary>
  12.     ''' Get's the handle and text of the foreground window
  13.     ''' </summary>
  14.     Public Function GetWindowText() As Window
  15.         Dim title As New Text.StringBuilder(255)
  16.         Dim titleLength As Integer = GetWindowText(GetForegroundWindow(), title, title.Capacity + 1)
  17.         title.Length = titleLength
  18.         Dim w As New Window
  19.         w.Text = title.ToString()
  20.         w.Handle = GetForegroundWindow()
  21.         Return w
  22.     End Function
  23.  
  24.     ''' <summary>
  25.     ''' Represents an open, visible window
  26.     ''' </summary>
  27.     Public Structure Window
  28.         Public Handle As IntPtr
  29.         Public Text As String
  30.     End Structure

Copy & Paste


Comments

There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month