Solved

How to get name of window with focus.

Posted on 2004-08-17
3
1,656 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

628 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