Solved

How to get name of window with focus.

Posted on 2004-08-17
3
1,634 Views
Last Modified: 2009-03-04
Hi,

Can somepne please give me a vb.net code sample to get the current active window.

i.e. 3 apps are running (calculator, Word, Excel), and calculator has the focus, I want my vb.net app to be able to get the window name and/or .exe name.

Regards
Mac
0
Comment
Question by:macjonesnz
3 Comments
 
LVL 27

Expert Comment

by:planocz
ID: 11819504
0
 
LVL 10

Accepted Solution

by:
prakash_prk earned 250 total points
ID: 11819618
Declare two API
-----------------------
<System.Runtime.InteropServices.DllImport("user32.dll")> _
        Shared Function GetWindowText(ByVal hWnd As IntPtr, ByVal txt As System.Text.StringBuilder, ByVal cnt As Integer) As     <System.Runtime.InteropServices.DllImport("user32.dll")> _
        Shared Function GetForegroundWindow() As IntPtr
    End Function

Use this codes
-----------------
        Dim hwnd As IntPtr = GetForegroundWindow()
        Dim sbuild As New System.Text.StringBuilder(255)
 
       GetWindowText(hwnd, sbuild, 255)

        MsgBox(sbuild.ToString)

regards
Prakash
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 11820806
Private Declare Function GetActiveWindow Lib "user32" () 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 Declare Function GetForegroundWindow Lib "user32" () As Integer


 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim foreground_hwnd As Integer
        Dim txt As String
        Dim length As Long

        foreground_hwnd = GetForegroundWindow()

        txt = Space$(1024)
        length = GetWindowText(foreground_hwnd, txt, Len(txt))
        txt = txt.Substring(0, length)
        lb.Text = txt
        lb.Refresh()
    End Sub
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now