Solved

- Application Focus Help -

Posted on 2004-09-03
4
241 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 109 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

691 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