在VB窗口中放两个Command按钮,其中将Command2按钮Enable属性改为FALSE禁止属性。在Command1按钮事件中写入如下代码。
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hpWin As Long, ByVal hcWin As Long, ByVal lpClassName As Long, ByVal lpWindowName As String) As Long
Private Declare Function EnableWindow Lib "user32" (ByVal hwnd As Long, ByVal fEnable As Long) As Long
Private Sub Command1_Click()
hWin = FindWindow(0, "Form1")
hButton = FindWindowEx(hWin, 0, 0, "Command2")
i = EnableWindow(hButton, 1)
End Sub
'刷新 并不是所有的都要刷新,但是还是刷一下好
SetWindowPos hwnd, 0, 0, 0, 0, 0, SWP_FRAMECHANGED & SWP_NOSIZE & SWP_NOZORDER & SWP_NOMOVE
End Sub
Module1:
Public Declare Function GetCursor Lib "user32" () As Long
Public Declare Function WindowFromPoint Lib "user32" _
(ByVal xPoint As Long, ByVal yPoint As Long) As Long
Public Declare Function GetCursorPos Lib "user32" _
(lpPoint As POINTAPI) As Long
Public Type POINTAPI
x As Long
y As Long
End Type
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Const GWL_STYLE = (-16)
Public Const WS_DISABLED = &H8000000
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function GetForegroundWindow Lib "user32" () As Long
Public Declare Function SetWindowPos Lib "user32" _
(ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
ByVal cy As Long, ByVal wFlags As Long) As Long
Public Const SWP_FRAMECHANGED = &H20 ' The frame changed: send WM_NCCALCSIZE
Public Const SWP_NOSIZE = &H1
Public Const SWP_NOZORDER = &H4
Public Const SWP_NOMOVE = &H2
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function IsWindowEnabled Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function ChildWindowFromPoint Lib "user32" (ByVal hWndParent As Long, ByVal xpt As Long, ByVal ypt As Long) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Private Const SWP_FRAMECHANGED = &H20 ' The frame changed: send WM_NCCALCSIZE
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOZORDER = &H4
Private Const SWP_NOSIZE = &H1
Private Const GWL_STYLE = (-16)
Private Const WS_DISABLED = &H8000000
Private Const HWND_NOTOPMOST = -2
Private Const HWND_TOPMOST = -1
Private Sub Command1_Click()
If Timer1.Enabled = False Then
Timer1.Enabled = True SetWindowPos hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
Else
Timer1.Enabled = False SetWindowPos hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
End If
End Sub
Private Sub Form_Load()
Timer1.Enabled = False
End Sub
Private Sub Timer1_Timer()
Dim pt As POINTAPI GetCursorPos pt
hWindow = GetForegroundWindow ScreenToClient hWindow, pt
Text1.Text = "x:" & pt.x & " - " & "y:" & pt.y
hChildWindow = ChildWindowFromPoint(hWindow, pt.x, pt.y)
Text2.Text = hChildWindow
If IsWindowEnabled(hChildWindow) = 0 Then
nStyle = GetWindowLong(hChildWindow, GWL_STYLE)
nStyle = nStyle Xor WS_DISABLED SetWindowLong hChildWindow, GWL_STYLE, nStyle SetWindowPos hChildWindow, 0, 0, 0, 0, 0, SWP_FRAMECHANGED Or SWP_NOMOVE Or SWP_NOSIZE Or SWP_NOZORDER
End If
End Sub