大家帮我分析一下,我那里做错了~~`
我的程序思想是通过EnumWindows遍历所有窗口。然后用IsWindowVisible(hwnd)和(IsIconic(hwnd))分别统计出显示和最小化的窗体,并赋值给I_O。再传回TIME控件。执行相应的程序。但是不知道为什么统计结果总是不是自已想的,大家帮我看看到底出了什么漏
模块代码:
'这个程序获得当前运行的所有窗口、模块、程序的列表
'
Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
Declare Function IsWindowVisible Lib "user32" (ByVal hwnd 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
Public i_o As Integer
'该函数是EnumWindows的回调函数,EnumWindows函数将遍历的窗口句柄传递到hwnd参数中
Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim astr As String * 256
Dim l As Long
l = GetWindowText(hwnd, astr, Len(astr)) '得到窗口的标题
If (InStr(astr, " ") > 1) And (IsWindowVisible(hwnd) Or (IsIconic(hwnd)) = 1) Then '(InStr(astr, " ") > 1)
nbs.List1.AddItem astr
i_o = i_o + 1
End If
EnumWindowsProc = True
End Function
主窗体程序代码:
Option Explicit
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SetBkMode Lib "gdi32" (ByVal hdc As Long, ByVal nBkMode As Long) As Long
Private Declare Function TextOut Lib "gdi32.dll" 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 GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetActiveWindow Lib "user32" () As Long
Private Const TRANSPARENT = 1
Dim mHdc As Long
Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private Sub Form_Load()
mHdc = GetDC(0)
End Sub
Private Sub Timer1_Timer()
Dim l As Long
List1.Clear
'遍历所有的窗口
i_o = 0
l = EnumWindows(AddressOf EnumWindowsProc, 0)
Label1.Caption = i_o
If i_o < 3 Then
SetBkMode mHdc, TRANSPARENT '设置透明
TextOut mHdc, 550, 150, "桌面写字测试~~", 14 '写字
DoEvents
End If
End Sub