Solved

- Application Focus Help -

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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 …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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