激活程序的disabled的按钮

有些软件如果你不输入正确的注册吗,那该死的“下一步”按钮就一直 disable 。这个 disable 按钮使用 WindowFromPoint, FindWindowEx 均无法得到 .

不过,使用 EnumChildWindows , GetWindow 去可以枚举到每一个窗口中的所有控件,包括 disabled 的控件,找到了句柄,我们就可以操作了

测试环境: WINXP+VB6

具体的 API 函数请参考 MSDN.

新建工程,在 form 中任意添加两个按钮,两个文本框。其中按钮 2 的 Enabled 属性为 false

form 的 Caption 设为 ” 激活程序的 disabled 的按钮 ”.

编译后,运行。

1. 我们先看看使用 GetWindow 枚举句柄的

我们先用 FindWindow 找到 form 窗口,然后找到窗口中所有的子控件句柄,然后使用 EnableWindow 函数激活

添加 1 个按钮, 2 个 list 控件。

Option Explicit

Private Declare Function FindWindow Lib "user32.dll" 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 EnableWindow Lib "user32.dll" (ByVal hwnd As Long, ByVal fEnable As Long) As Long

Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long

Const GW_CHILD = 5

Const GW_HWNDNEXT = 2

Const WM_GETTEXT = &HD

Const WM_ENABLE As Long = &HA

Private Sub Command2_Click()

Dim tWnd As Long

Dim bWnd As Long

Dim lpClassName As String

Dim RetVal As Long

Dim i As Integer

Dim mName As String

tWnd = FindWindow(vbNullString, " 激活程序的 disabled 的按钮 ")

bWnd = GetWindow(tWnd, GW_CHILD)

Do While bWnd <> 0

lpClassName = Space(256)

‘ 这里得到类名主要是为了可以看出 bWnd 所对应的控件

RetVal = GetClassName(bWnd, lpClassName, 256)

i = InStr(1, lpClassName, Chr(0))

mName = Left(lpClassName, i - 1)

List1.AddItem bWnd & " " & mName

;list2 主要是为了方便操作

List2.AddItem bWnd

‘ 继续寻找下一个控件

bWnd = GetWindow(bWnd, GW_HWNDNEXT)

Loop

End Sub

‘ 单击要激活的句柄

Private Sub List2_Click()

EnableWindow List2.List(List2.ListIndex), True

End Sub

好了,运行后,点击按钮,窗口中所有的控件句柄填充到列表框中,然后点击列表框,可以发现 disabled 的按钮被激活,可以运行了

2. 使用 EnumChildWindows 来枚举

函数功能:为指定的父窗口枚举子窗口

Private Declare Function EnumChildWindows Lib "user32" Alias "EnumChildWindows" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long

【参数表】

hWndParent ----- Long ,欲枚举子窗口的父窗口的句柄

lpEnumFunc ----- Long ,为每个子窗口调用的函数的指针。用 AddressOf 运算符获得函数在一个标准模块中的地址

代码如下:

窗口

Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function EnableWindow Lib "user32.dll" (ByVal hwnd As Long, ByVal fEnable As Long) As Long

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

Const WM_ENABLE As Long = &HA

Private Sub Command1_Click()

Dim twnd As Long

twnd = FindWindow(vbNullString, " 激活程序的 disabled 的按钮 ")

EnumChildWindows twnd, AddressOf EnumChildProc, ByVal 0&

End Sub

Private Sub List1_Click()

EnableWindow List1.List(List1.ListIndex), True

End Sub

模块

Option Explicit

Public Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Long

Form1.List1.AddItem hwnd

' 继续枚举

EnumChildProc = 1

End Function

好了,运行后,点击按钮,窗口中所有的控件句柄填充到列表框中,然后点击列表框,可以发现 disabled 的按钮被激活,可以运行了

Published At
Categories with Web编程
Tagged with
comments powered by Disqus