Useful GetForegroundWindow

zhaozj2021-02-16  43

This small example is to demonstrate how to get a handle of the Windows desktop's active window.

You can get it using a Timer control. In this case, the operation after the handle is obtained by getWindowText function.

1. Newly built a standard VB6 EXE project, add Timer Control 2. API function declaration

private Declare Function GetForegroundWindow Lib "user32" () as Longprivate Declare Function GetWindowText Lib "user32" _ Alias ​​"GetWindowTextA" (byval hwnd as Long, _ byval lpString as string, byval cch as Long) as Long

3. Add code in the form of the LOAD event:

PRIVATE SUB FORM_LOAD () Timer1.Iterval = 100 'Sets Interval End Sub

4. Add code to the Timer event in the Timer Control:

Private Sub Timer1_Timer () Static CurrentHwnd As Long Dim ForegroundWindowHwnd As Long Dim sText As String * 255 ForegroundWindowHwnd = GetForegroundWindow If ForegroundWindowHwnd = CurrentHwnd Then Exit Sub CurrentHwnd = ForegroundWindowHwnd If CurrentHwnd <> hwnd Then Caption = "ActiveWidow's Caption:" & Left $ (sText , GetWindowText (CurrentHWnd, Stext, 255)) Else Caption = "ActiveWindow's Caption: Form1" End IFEND SUB

This program is debugged in Win2000 VB6

转载请注明原文地址:https://www.9cbs.com/read-27517.html

New Post(0)