?
Solved

- Application Focus Help -

Posted on 2004-09-03
4
Medium Priority
?
243 Views
Last Modified: 2010-04-23
i am useing VB.NET, and i am working within a windows form.

i have my form, one Internet Explorer window, and several other programs running at the same time. from my form, how can i tell if the Internet Explorer window is in focus?

i know how to set it to the focus, but i would like be able to check if it is in focus.

thanks.
0
Comment
Question by:moe10
[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
  • 2
4 Comments
 
LVL 34

Expert Comment

by:flavo
ID: 11979248
Dim current As Process = Process.GetCurrentProcess()
MsgBox(current.ToString)  'retun current process

Dave
0
 
LVL 4

Expert Comment

by:Javert93
ID: 11981379
This function gets a handle to the current window that has the focus:

    Public Declare Auto Function GetForegroundWindow Lib "user32.dll" () As IntPtr

Keep in mind that this function returns the handle of any window that has the focus, which may not necessarily be a top-level . So, if IE is showing a dialog box, you will get a handle to the dialog box, and not IE's main window. Also, the documentation in MSDN says that this function can return NULL (IntPtr.Zero) if it is called when the focus is in the process of changing from one window to another.
0
 
LVL 1

Author Comment

by:moe10
ID: 11993332
thx for the help.

I am having some problems when trying to compare the value that i get from the "GetForegroundWindow" to a string.

could you give me an example where i call the "GetForegroundWindow" function and then compare the value returned to a string to see if its equal?

thanks.
0
 
LVL 4

Accepted Solution

by:
Javert93 earned 436 total points
ID: 11999987
Just paste this code snippet into a module and call IsWindowInFocus() with the window handle string. It will return true if the passed window handle matches the foreground window; otherwise it will return false.

    Public Declare Auto Function GetForegroundWindow Lib "user32.dll" () As IntPtr

    Public Function IsWindowInFocus(ByVal hWnd As String) As Boolean
        ' If the window handle passed is non-numeric, then return false
        If Not IsNumeric(hWnd) Then Return False

        ' Compare the window handle to the foreground window and return the result
        Return CBool(Convert.ToInt32(hWnd) = GetForegroundWindow().ToInt32())
    End Function
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

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…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

765 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