• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 135
  • Last Modified:

check if an winows is focused

How can I check if a window in VB is focused?
  • 2
1 Solution
check out Screen.ActiveForm

Returns the form that is the active window. If an MDIForm object is active or is referenced, it specifies the active MDI child form.



The object placeholder represents an object expression that evaluates to an object in the Applies To list.


Use the ActiveForm property to access a form's properties or to invoke its methods — for example, Screen.ActiveForm.MousePointer = 4.

This property is especially useful in a multiple-document interface (MDI) application where a button on a toolbar must initiate an action on a control in an MDI child form. When a user clicks the Copy button on the toolbar, your code can reference the text in the active control on the MDI child form — for example, ActiveForm.ActiveControl.SelText.

When a control on a form has the focus, that form is the active form on the screen (Screen.ActiveForm).  In addition, an MDIForm object can contain one child form that is the active form within the context of the MDI parent form (MDIForm.ActiveForm). The ActiveForm on the screen isn't necessarily the same as the ActiveForm in the MDI form, such as when a dialog box is active. For this reason, specify the MDIForm with ActiveForm when there is a chance of a dialog box being the ActiveForm property setting.

Note   When an active MDI child form isn't maximized, the title bars of both the parent form and the child form appear active.

If you plan to pass Screen.ActiveForm or MDIForm.ActiveForm to a procedure, you must declare the argument in that procedure with the generic type (As Form) rather than a specific form type (As MyForm) even if ActiveForm always refers to the same type of form.

The ActiveForm property determines the default value for the ProjectTemplate object.
evertdAuthor Commented:
And how can I check if the appllication is focused?
In your form put a timer control and paste the following code and click on the form and check out:
Option Explicit

Private Declare Function GetActiveWindow Lib "user32" () As Long

Private Sub Form_Click()
Timer1.Enabled = True
End Sub

Private Sub Form_Load()
Timer1.Interval = 2000
Timer1.Enabled = False
End Sub

Private Sub Timer1_Timer()
If GetActiveWindow = hWnd Then
    MsgBox "I am the active window"
    MsgBox "I'm not the active window"
End If
Timer1.Enabled = False
End Sub

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now