Findwindow 在 64 位 VBA7 中不起作用

vba

1个回答

写回答

Promiser

2025-09-09 12:00

+ 关注

VBA
VBA

在64位VBA7中,Findwindow函数不起作用是一个常见的问题。Findwindow函数是一个用于查找窗口句柄的API函数,它在32位VBA中可以正常工作,但在64位VBA7中却无法正常运行。这个问题可能导致在64位VBA7中无法使用Findwindow函数来查找特定窗口,从而影响到一些需要使用窗口句柄的功能。

为了解决这个问题,我们可以使用其他的方法来替代Findwindow函数。下面我们将介绍两种常用的替代方案,并提供相应的案例代码。

方法一:使用EnumWindows函数

EnumWindows函数是另一个API函数,它可以枚举系统中所有的顶级窗口,并将窗口句柄作为参数传递给一个回调函数。通过在回调函数中判断窗口的标题或类名等属性,我们可以找到我们需要的窗口。

下面是一个示例代码,演示了如何使用EnumWindows函数来查找窗口句柄:

VBA

Option Explicit

Private Declare PtrSafe Function EnumWindows Lib "user32" (ByVal lpEnumFunc As LongPtr, ByVal lParam As LongPtr) As Long

Private Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As LongPtr, ByVal lpString As String, ByVal cch As Long) As Long

Private Declare PtrSafe Function GetclassName Lib "user32" Alias "GetclassNameA" (ByVal hwnd As LongPtr, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

Private Function EnumWindowsProc(ByVal hwnd As LongPtr, ByVal lParam As LongPtr) As Long

Dim sWindowText As String

Dim sClassName As String

sWindowText = Space(256)

GetWindowText hwnd, sWindowText, Len(sWindowText)

sClassName = Space(256)

GetclassName hwnd, sClassName, Len(sClassName)

'判断窗口的标题和类名

If InStr(1, sWindowText, "需要查找的窗口标题") > 0 And InStr(1, sClassName, "需要查找的窗口类名") > 0 Then

'找到了目标窗口

MsgBox "找到了目标窗口,句柄为:" & hwnd

EnumWindowsProc = 0 '返回0表示继续枚举窗口

Else

EnumWindowsProc = 1 '返回1表示继续枚举窗口

End If

End Function

Sub FindWindowExample()

EnumWindows AddressOf EnumWindowsProc, 0

End Sub

通过修改EnumWindowsProc函数中的判断条件,您可以根据窗口的标题和类名来自定义查找的条件。

方法二:使用FindWindowEx函数

FindWindowEx函数是另一个API函数,它可以在指定的父窗口下查找具有指定类名或窗口名的窗口。通过使用FindWindowEx函数,我们可以在父窗口下查找特定的子窗口。

下面是一个示例代码,演示了如何使用FindWindowEx函数来查找子窗口句柄:

VBA

Option Explicit

Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWndParent As LongPtr, ByVal hWndChildAfter As LongPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As LongPtr

Sub FindWindowExExample()

Dim hwndParent As LongPtr

Dim hwndChild As LongPtr

hwndParent = FindWindowEx(0, 0, "父窗口类名", "父窗口标题")

If hwndParent <> 0 Then

hwndChild = FindWindowEx(hwndParent, 0, "子窗口类名", "子窗口标题")

If hwndChild <> 0 Then

MsgBox "找到了目标窗口,句柄为:" & hwndChild

Else

MsgBox "未找到目标窗口"

End If

Else

MsgBox "未找到父窗口"

End If

End Sub

通过修改FindWindowEx函数中的参数,您可以根据窗口的类名和标题来自定义查找的条件。

在64位VBA7中,Findwindow函数不起作用是一个常见的问题。为了解决这个问题,我们可以使用EnumWindows函数或FindWindowEx函数来替代Findwindow函数。通过这两种方法,我们可以在64位VBA7中正常地查找窗口句柄,从而实现需要使用窗口句柄的功能。

希望本文对您有所帮助!

举报有用(4分享收藏

Copyright © 2025 IZhiDa.com All Rights Reserved.

知答 版权所有 粤ICP备2023042255号