gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:16:00 25 4
gpt4 key购买 nike

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

最佳答案

注意

我已经在 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

关于vba - 如何检测当前打开的 swf 文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10594877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com