--GetActiveWindow The GetActiveWindow function retrieves the window handle to the active window associated with the calling thread's message queue. --HWND GetActiveWindow(VOID)--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)
'This Project needs '- two timers, interval=100 '- a button'in general section Private Type POINTAPI x As Long y As Long End TypePrivate Declare Function GetActiveWindow Lib "user32" () As Long Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function Ellipse Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As LongPrivate Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long Private Sub Form_Load() Timer1.Interval = 100 Timer1.Enabled = True Timer2.Interval = 100 Timer2.Enabled = True Command1.Caption = "Draw Text" End Sub 'This will draw an Ellipse on the active window Sub Timer1_Timer() Dim Position As POINTAPI 'Get the cursor position GetCursorPos Position 'Draw the Ellipse on the Screen's DC Ellipse GetWindowDC(0), Position.x - 5, Position.y - 5, Position.x + 5, Position.y + 5 End Sub Sub Command1_Click() 'KPD-Team 1998 'URL: http://www.allapi.net/ 'E-Mail: [email protected] Dim intCount As Integer, strString As String strString = "Cool, text on screen !" For intCount = 0 To 30 'Draw the text on the screen TextOut GetWindowDC(0), intCount * 20, intCount * 20, strString, Len(strString) Next intCount End Sub Private Sub Timer2_Timer() 'Draw the text to the active window TextOut GetWindowDC(GetActiveWindow), 50, 50, "This is a form", 14 End Sub
GetActiveWindow GetForeGroundWindow
不好意思,我的要求是要得到所有打开的窗口的句柄或TITLE
'Add this code to a module Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As LongPublic Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean Dim sSave As String, Ret As Long Ret = GetWindowTextLength(hwnd) sSave = Space(Ret) GetWindowText hwnd, sSave, Ret + 1 Form1.List1.AddItem Str$(hwnd) + " " + sSave 'continue enumeration EnumWindowsProc = True End Function'Add this code to a form Private Sub Form_Load() 'KPD-Team 2000 'URL: http://www.allapi.net/ 'E-Mail: [email protected] 'Set the form's graphics mode to persistent 'call the Enumwindows-function EnumWindows AddressOf EnumWindowsProc, ByVal 0& End Sub
'in a form Private Sub Form_Load() Me.AutoRedraw = True EnumChildWindows GetDesktopWindow, AddressOf EnumChildProc, ByVal 0& End Sub 'in a module Declare Function GetDesktopWindow Lib "user32" () As Long Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long Public Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Long Dim sSave As String 'Get the windowtext length sSave = Space$(GetWindowTextLength(hwnd) + 1) 'get the window text GetWindowText hwnd, sSave, Len(sSave) 'remove the last Chr$(0) sSave = Left$(sSave, Len(sSave) - 1) If sSave <> "" Then Form1.Print sSave 'continue enumeration EnumChildProc = 1 End Function
The GetActiveWindow function retrieves the window handle to the active window associated with the calling thread's message queue.
--HWND GetActiveWindow(VOID)--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)
'- two timers, interval=100
'- a button'in general section
Private Type POINTAPI
x As Long
y As Long
End TypePrivate Declare Function GetActiveWindow Lib "user32" () As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function Ellipse Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As LongPrivate Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Sub Form_Load()
Timer1.Interval = 100
Timer1.Enabled = True
Timer2.Interval = 100
Timer2.Enabled = True
Command1.Caption = "Draw Text"
End Sub
'This will draw an Ellipse on the active window
Sub Timer1_Timer()
Dim Position As POINTAPI
'Get the cursor position
GetCursorPos Position
'Draw the Ellipse on the Screen's DC
Ellipse GetWindowDC(0), Position.x - 5, Position.y - 5, Position.x + 5, Position.y + 5
End Sub
Sub Command1_Click()
'KPD-Team 1998
'URL: http://www.allapi.net/
'E-Mail: [email protected] Dim intCount As Integer, strString As String
strString = "Cool, text on screen !"
For intCount = 0 To 30
'Draw the text on the screen
TextOut GetWindowDC(0), intCount * 20, intCount * 20, strString, Len(strString)
Next intCount
End Sub
Private Sub Timer2_Timer()
'Draw the text to the active window
TextOut GetWindowDC(GetActiveWindow), 50, 50, "This is a form", 14
End Sub
GetForeGroundWindow
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As LongPublic Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim sSave As String, Ret As Long
Ret = GetWindowTextLength(hwnd)
sSave = Space(Ret)
GetWindowText hwnd, sSave, Ret + 1
Form1.List1.AddItem Str$(hwnd) + " " + sSave
'continue enumeration
EnumWindowsProc = True
End Function'Add this code to a form
Private Sub Form_Load()
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: [email protected]
'Set the form's graphics mode to persistent
'call the Enumwindows-function
EnumWindows AddressOf EnumWindowsProc, ByVal 0&
End Sub
Private Sub Form_Load()
Me.AutoRedraw = True
EnumChildWindows GetDesktopWindow, AddressOf EnumChildProc, ByVal 0&
End Sub
'in a module
Declare Function GetDesktopWindow Lib "user32" () As Long
Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim sSave As String
'Get the windowtext length
sSave = Space$(GetWindowTextLength(hwnd) + 1)
'get the window text
GetWindowText hwnd, sSave, Len(sSave)
'remove the last Chr$(0)
sSave = Left$(sSave, Len(sSave) - 1)
If sSave <> "" Then Form1.Print sSave
'continue enumeration
EnumChildProc = 1
End Function