Solved

- Application Focus Help -

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Sort Datagrdview programmatically 19 34
Starting to learn JAVA, 7 47
Duplicate a row 2 31
Close word object 13 9
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
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…

706 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

21 Experts available now in Live!

Get 1:1 Help Now