3

我想检测当前打开的 swf 文件名。这是我的代码:

Private Const GW_HWNDNEXT = 2
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private Sub Form_Load()
  ListWins "*.swf*"
End Sub

Sub ListWins(Optional Title = "*", Optional Class = "*")
  Dim hWndThis As Long

  hWndThis = FindWindow(vbNullString, vbNullString)

  While hWndThis
    Dim sTitle As String, sClass As String
    sTitle = Space$(255)
    sTitle = Left$(sTitle, GetWindowText(hWndThis, sTitle, Len(sTitle)))

    sClass = Space$(255)
    sClass = Left$(sClass, GetClassName(hWndThis, sClass, Len(sClass)))

    If sTitle Like Title And sClass Like Class Then
      Debug.Print sTitle, sClass
      List1.AddItem (sTitle)
    End If

    hWndThis = GetWindow(hWndThis, GW_HWNDNEXT)
  Wend
End Sub

此代码可以正常检测文件名*.doc*.xls但不适用于*.swf文件。

4

1 回答 1

2

笔记

我已经在 VBA 中对其进行了测试。我相信它也可以在 VB6 中工作。

试试这个(将此代码粘贴到模块中并运行子示例

Private Declare Function GetWindowTextLength Lib "user32" Alias _
"GetWindowTextLengthA" (ByVal HWnd As Long) As Long

Private Declare Function GetWindowText Lib "user32" Alias _
"GetWindowTextA" (ByVal HWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private Declare Function GetDesktopWindow Lib "user32" () As Long

Public Declare Function EnumChildWindows Lib "user32" _
(ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long

Public Function APIWindowCaption(ByVal HWnd As Long, ByVal lParam As Long) As Long
    Static winnum As Integer
    Dim MyStr As String

    winnum = winnum + 1

    MyStr = String(GetWindowTextLength(HWnd) + 1, Chr$(0))

    GetWindowText HWnd, MyStr, Len(MyStr)

    '~~> This will give you the caption of the window
    If InStr(1, MyStr, ".swf", vbTextCompare) Then Debug.Print MyStr

    APIWindowCaption = 1
End Function

Sub Sample()
    Dim retval As Long, DesktophWnd As Long

    DesktophWnd = GetDesktopWindow

    retval = EnumChildWindows(DesktophWnd, AddressOf APIWindowCaption, ByVal 0&)
End Sub

快照

在此处输入图像描述

于 2012-05-15T11:28:14.777 回答