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
相关文章推荐
- C#实现关闭其他程序窗口或进程代码分享
- 用代码关闭程序的正确做法!---向窗口发送WM_CLOSE消息
- 禁用form/窗口的关闭按钮(来自网上的vb代码)
- vb.net禁止程序窗口的“关闭”按钮(转载+亲自实践)
- [VB.NET]怎样才能用VB.NET的代码来关闭一个在运行的程序?????
- 用代码关闭程序的正确做法!---向窗口发送WM_CLOSE消息
- 用代码关闭程序的正确做法!---向窗口发送WM_CLOSE消息
- 使用VC++6关闭指定窗口标题的程序的代码
- vb程序“关闭”时内存进程不能关闭
- 一个关闭窗口不弹出提示的javascript代码
- VB 代码删除程序自身
- vc里几个关闭窗口退出程序的语句
- ubuntu系统强制关闭程序或窗口
- [编写高质量代码:改善java程序的151个建议]建议88 用枚举实现工厂方法模式更简单
- JS关闭窗口或JS关闭页面的几种代码
- 关闭IE7弹出窗口阻止程序
- 在VB中使用API关闭窗口
- 自建的C语言可执行程序运行后结果显示窗口不关闭
- IE,firefox(火狐)浏览器无提示关闭窗口js实现代码小结