MSDN:
| Цитата | GetForegroundWindow The GetForegroundWindow function returns a handle to the foreground window (the window with which the user is currently working). The system assigns a slightly higher priority to the thread that creates the foreground window than it does to other threads.
HWND GetForegroundWindow(VOID) Parameters This function has no parameters.
Return Values The return value is a handle to the foreground window. |
Объявлять так:
| Код | Private Declare Function GetForegroundWindow Lib "user32" () As Long |
Использовать так:
| Код | Dim hWndActive As Long ' получаешь handle на active/foreground окно hWndActive = GetForegroundWindow() |
Для того чтобы затормозить процесс, например, на 2 секунды и сделать то окно активным, от которого ты хочешь handle можешь написать что-нибудь на подобии:
| Код | Dim EndTime As Date
MsgBox "Две секунды после закрытия этого окна ты получишь handle."
' ждешь две секунды EndTime = DateAdd("s", 2, Now) Do Until Now > EndTime DoEvents Loop |
Вот полный исходник:
| Код | Private Declare Function GetForegroundWindow Lib "user32" () As Long Private Declare Function PostMessage Lib "user32" _ Alias "PostMessageA" (ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ ByVal lParam As Long) As Long Private Const WM_CLOSE = &H10
Private Sub Command1_Click() Dim hWndActive As Long Dim EndTime As Date
MsgBox "Две секунды после закрытия этого окна ты получишь handle."
' ждешь две секунды EndTime = DateAdd("s", 2, Now) Do Until Now > EndTime DoEvents Loop ' получаешь handle на active/foreground окно hWndActive = GetForegroundWindow()
' PostMessage hWndActive, WM_CLOSE, 0, 0 ' это ответ на твой второй вопрос End Sub | |