2012-05-15 43 views
3

我想檢測當前打開swf文件名。這裏是我的代碼:如何檢測當前打開的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文件。

+0

哦,上帝,可怕的代碼! (現在格式化很好) – Deanna

回答

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

+0

非常感謝您的支持此代碼只顯示swf播放器名稱,但不顯示swf文件名我需要獲取當前的swf文件名,請再次檢查 – user757321

+0

您的意思是像'Example.swf'嗎? –

+0

是的,我需要得到example.swf,但這段代碼顯示swfplayer不是example.swf – user757321

相關問題