VB and VBA Users Source Code: Get the handle of the topmost window
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Get the handle of the topmost window
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Wednesday, April 04, 2001
Hits:
873
Category:
Windows Forms/GUI/Controls/Graphics
Article:
The following routines show how to get the window handles of the topmost or foreground windows. Please read the notes to ascertain the differences between them. Option Explicit Private Declare Function GetFgWindowAll Lib "user32" Alias "GetForegroundWindow" () As Long Private Declare Function GetFgWindowApp Lib "user32" Alias "GetActiveWindow" () As Long 'Purpose : Returns the handle of the foreground window i.e. ' the window with which the user is currently working. 'Inputs : N/A 'Outputs : Returns the handle of the topmost window 'Author : Andrew Baker 'Date : 03/09/2000 14:17 'Notes : 1. This function uses the GetForegroundWindow API which differs from the GetActiveWindow ' API in that the GetActiveWindow returns the handle of the topmost window created by the ' calling thread. Whereas GetForegroundWindow returns the topmost window regardless of ' which thread created this window. ' 2. The system assigns a slightly higher priority to the thread that creates the foreground ' window than it does to other threads. ' 3. The foreground window can be 0 in certain circumstances, such as when a window is losing activation. Function GetForegroundWindowAll() As Long GetForegroundWindowAll = GetFgWindowAll End Function 'Purpose : Returns the handle of the topmost window owned by this thread ' This is not the window with which the user is currently working if ' the user is currently working on a window in a different thread. 'Inputs : N/A 'Outputs : Returns the handle of the topmost window in this thread. 'Author : Andrew Baker 'Date : 03/09/2000 14:17 'Notes : A return value of 0 indicates that the calling thread’s message queue does not have an associated window with the keyboard focus. Function GetForegroundWindowApp() As Long GetForegroundWindowApp = GetFgWindowApp End Function
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder