如何检测当前打开的swf文件名

时间:2012-05-15 05:56:22

标签: vba vb6 vbscript

我想检测当前打开的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个文件。

1 个答案:

答案 0 :(得分: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

<强>快照

enter image description here