Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to get name of window with focus.

Posted on 2004-08-17
3
Medium Priority
?
1,668 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 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Screencast - Getting to Know the Pipeline

927 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