您的位置:首页 > 编程语言 > VB

VB 枚举窗口和关闭程序窗口的代码

2008-03-28 09:53 435 查看

Attribute VB_Name = "Enum_Window"


Option Explicit


Option Base 0
'Powered by barenx




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




Public Declare Function FindWindowEx()Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long




Public Declare Function SetParent()Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long


Public Const LWA_COLORKEY = &H1


Public Const LWA_ALPHA = &H2


Public Const GWL_EXSTYLE = (-20)


Public Const WS_EX_LAYERED = &H80000


Public Const WS_EX_TRANSPARENT As Long = &H20&




Public Declare Function GetWindowLong()Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long




Public Declare Function SetWindowLong()Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long




Public Declare Function SetLayeredWindowAttributes()Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long






Public Declare Function PostMessage()Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long




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




Public Declare Function GetDesktopWindow()Function GetDesktopWindow Lib "user32" () As Long




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




Public Declare Sub CloseHandle()Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)


Public Const GW_CHILD = 5


Public Const GW_HWNDNEXT = 2


Public Const WM_CLOSE = &H10


Public Const WM_QUIT = &H12


Public Const MAX_PATH As Integer = 260




'***************************************************************************************************************




Public Sub CloseWindow()Sub CloseWindow(ByRef WinText() As String)


On Error Resume Next


Dim i As Long, lngDeskTopHandle As Long, lngHand As Long, StrName As String


lngDeskTopHandle = GetDesktopWindow()


lngHand = GetWindow(lngDeskTopHandle, GW_CHILD)


Do While lngHand <> 0


StrName = Space(MAX_PATH)


GetWindowText lngHand, StrName, MAX_PATH


StrName = StrCutNull(Trim$(StrName))


If StrName <> vbNullString Then


For i = 0 To UBound(WinText)


If InStr(1, UCase$(StrName), UCase$(WinText(i))) > 0 Then


PostMessage lngHand, &H11, 0, 0 'public Const WM_QUERYENDSESSION = &H11


PostMessage lngHand, WM_QUIT, 0, 0


PostMessage lngHand, &H16, 0, 0 'public Const WM_ENDSESSION = &H16


PostMessage lngHand, WM_QUIT, 0, 0


End If


Next i


End If


lngHand = GetWindow(lngHand, GW_HWNDNEXT)


Loop


CloseHandle lngDeskTopHandle


CloseHandle lngHand


End Sub






Public Sub GetWindowsList()Sub GetWindowsList(ByRef WindowsName() As String)


On Error Resume Next


Dim i As Long, lngDeskTopHandle As Long, lngHand As Long, StrName As String


lngDeskTopHandle = GetDesktopWindow()


lngHand = GetWindow(lngDeskTopHandle, GW_CHILD)


i = 0


Do While lngHand <> 0


StrName = Space(MAX_PATH)


GetWindowText lngHand, StrName, MAX_PATH


StrName = StrCutNull(Trim$(StrName))


If StrName <> vbNullString Then


ReDim Preserve WindowsName(i)


WindowsName(i) = StrName


Debug.Print StrName


i = i + 1


End If


lngHand = GetWindow(lngHand, GW_HWNDNEXT)


Loop


CloseHandle lngDeskTopHandle


CloseHandle lngHand


End Sub




Public Function StrCutNull()Function StrCutNull(IStr As String) As String


Dim i As Long


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


If i <= 1 Then


StrCutNull = vbNullString


Else


StrCutNull = Left$(IStr, i - 1)


End If


End Function

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: