Solved

How to get name of window with focus.

Posted on 2004-08-17
3
1,628 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
Comment Utility
0
 
LVL 10

Accepted Solution

by:
prakash_prk earned 250 total points
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

8 Experts available now in Live!

Get 1:1 Help Now