[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

- Application Focus Help -

Posted on 2004-09-03
4
Medium Priority
?
248 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
  • 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

Technology Partners: 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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

873 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