Author:水如烟
Imports
LzmTW.uSystem.uWindows.SafeNative
Imports LzmTW.uSystem.uWindows.SafeNative.Constant
Imports System.Text
Namespace LzmTW.uSystem.uWindows.Win32API
Public Class WindowsAction
Private Sub New ()
End Sub
Private Shared gWindowInfos( - 1 ) As WindowInfo
Private Shared gActiveWindowInfos( - 1 ) As ActiveWindowInfo
Public Shared Function EnumWindows() As WindowInfo()
ReDim gWindowInfos( - 1 )
SafeNativeMethods.EnumWindows( AddressOf FillActiveProcessList, 0 )
Return gWindowInfos
End Function
Public Shared Function EnumActiveWindows() As ActiveWindowInfo()
ReDim gActiveWindowInfos( - 1 )
SafeNativeMethods.EnumWindowsDllImport( AddressOf FillActiveWindowsList, 0 )
Return gActiveWindowInfos
End Function
Public Shared Function FindWindow( ByVal classNameOrWindowText As String ) As Integer
Dim hWnd As Integer
If classNameOrWindowText = "" Then
' FindWindowAny takes to Integer parameters and finds any available window.
hWnd = SafeNativeMethods.FindWindowAny( 0 , 0 )
Else
' FindWindowNullWindowCaption attempts to locate a window by class name alone.
hWnd = SafeNativeMethods.FindWindowNullWindowCaption(classNameOrWindowText, 0 )
If hWnd = 0 Then
' FindWindowNullClassName attempts to locate a window by window name alone.
hWnd = SafeNativeMethods.FindWindowNullClassName( 0 , classNameOrWindowText)
End If
End If
Return hWnd
End Function
Public Shared Function FindWindow( ByVal className As String , ByVal windowText As String ) As Integer
Dim hWnd As Integer
If className = "" AndAlso windowText = "" Then
' FindWindowAny takes to Integer parameters and finds any available window.
hWnd = SafeNativeMethods.FindWindowAny( 0 , 0 )
ElseIf className = "" AndAlso windowText <> "" Then
' FindWindowNullClassName attempts to locate a window by window name alone.
hWnd = SafeNativeMethods.FindWindowNullClassName( 0 , windowText)
ElseIf className <> "" AndAlso windowText = "" Then
' FindWindowNullWindowCaption attempts to locate a window by class name alone.
hWnd = SafeNativeMethods.FindWindowNullWindowCaption(className, 0 )
Else
' FindWindow searches for a window by class name and window name.
hWnd = SafeNativeMethods.FindWindow(className, windowText)
End If
Return hWnd
End Function
Public Shared Function ShowWindow( ByVal classNameOrWindowText As String ) As Boolean
Return ShowWindow(FindWindow(classNameOrWindowText))
End Function
Public Shared Function ShowWindow( ByVal className As String , ByVal windowText As String ) As Boolean
Return ShowWindow(FindWindow(className, windowText))
End Function
Public Shared Function ShowWindow( ByVal hWnd As Integer ) As Boolean
If hWnd = 0 Then Return False
' Set the window as foreground.
SafeNativeMethods.SetForegroundWindow(hWnd)
' If window is minimized, simply restore, otherwise show it. Notice the
' declaration of Win32API.IsIconic defines the return value as Boolean
' allowing .NET to marshall the integer value to a Boolean.
If SafeNativeMethods.IsIconic(hWnd) Then
SafeNativeMethods.ShowWindow(hWnd, SW_RESTORE)
Else
SafeNativeMethods.ShowWindow(hWnd, SW_SHOW)
End If
Return True
End Function
Public Shared Function ShowWindow( ByVal hWnd As IntPtr) As Boolean
Return ShowWindow(hWnd.ToInt32)
End Function
Public Shared Function ShowWindow( ByVal form As Form) As Boolean
If form Is Nothing Then Return False
Return ShowWindow(form.Handle)
End Function
Public Shared Function ProcessIsActiveWindow( ByVal hWnd As Integer ) As Boolean
Dim windowText As New StringBuilder(STRING_BUFFER_LENGTH)
Dim windowIsOwned As Boolean
Dim windowStyle As Integer
' Get the windows caption, owner information, and window style.
SafeNativeMethods.GetWindowText(hWnd, windowText, STRING_BUFFER_LENGTH)
windowIsOwned = SafeNativeMethods.GetWindow(hWnd, GW_OWNER) <> 0
windowStyle = SafeNativeMethods.GetWindowLong(hWnd, GWL_EXSTYLE)
' Do not allow invisible processes.
If Not SafeNativeMethods.IsWindowVisible(hWnd) Then
Return False
End If
' Window must have a caption
If windowText.ToString.Equals( "" ) Then
Return False
End If
' Should not have a parent
If SafeNativeMethods.GetParent(hWnd) <> 0 Then
Return False
End If
' Don't allow unowned tool tips
If (windowStyle And WS_EX_TOOLWINDOW) <> 0 And Not windowIsOwned Then
Return False
End If
' Don't allow applications with owners
If (windowStyle And WS_EX_APPWINDOW) = 0 And windowIsOwned Then
Return False
End If
' All criteria were met, window is a valid active window.
Return True
End Function
Private Shared Function FillActiveProcessList( ByVal hWnd As Integer , ByVal lParam As Integer ) As Boolean
Dim mWindowText As New StringBuilder(STRING_BUFFER_LENGTH)
Dim mClassName As New StringBuilder(STRING_BUFFER_LENGTH)
' Get the Window Caption and Class Name. Notice that the definition of Win32API
' functions in the Win32API class are declared differently than in VB6. All Longs
' have been replaced with Integers, and Strings by StringBuilders.
SafeNativeMethods.GetWindowText(hWnd, mWindowText, STRING_BUFFER_LENGTH)
SafeNativeMethods.GetClassName(hWnd, mClassName, STRING_BUFFER_LENGTH)
' Add a new process item to the Processes list view.
Dim processItem As New WindowInfo
With processItem
.WindowText = mWindowText.ToString
.ClassName = mClassName.ToString
.hWnd = hWnd
End With
Append( Of WindowInfo)(gWindowInfos, processItem)
Return True
End Function
Private Shared Function FillActiveWindowsList( ByVal hWnd As Integer , ByVal lParam As Integer ) As Boolean
Dim windowText As New StringBuilder(STRING_BUFFER_LENGTH)
' Get the Window Caption.
SafeNativeMethods.GetWindowText(hWnd, windowText, STRING_BUFFER_LENGTH)
' Only add valid windows to the active windows listbox.
If ProcessIsActiveWindow(hWnd) Then
Dim processItem As New ActiveWindowInfo
processItem.WindowText = windowText.ToString
Append( Of ActiveWindowInfo)(gActiveWindowInfos, processItem)
End If
Return True
End Function
Private Shared Sub Append( Of T)( ByRef collection As T(), ByVal value As T)
ReDim Preserve collection(collection.Length)
collection(collection.Length - 1 ) = value
End Sub
Public Shared Function SetParent( ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
Return SafeNativeMethods.SetParent(hWndChild, hWndNewParent)
End Function
End Class
End Namespace
Imports LzmTW.uSystem.uWindows.SafeNative.Constant
Imports System.Text
Namespace LzmTW.uSystem.uWindows.Win32API
Public Class WindowsAction
Private Sub New ()
End Sub
Private Shared gWindowInfos( - 1 ) As WindowInfo
Private Shared gActiveWindowInfos( - 1 ) As ActiveWindowInfo
Public Shared Function EnumWindows() As WindowInfo()
ReDim gWindowInfos( - 1 )
SafeNativeMethods.EnumWindows( AddressOf FillActiveProcessList, 0 )
Return gWindowInfos
End Function
Public Shared Function EnumActiveWindows() As ActiveWindowInfo()
ReDim gActiveWindowInfos( - 1 )
SafeNativeMethods.EnumWindowsDllImport( AddressOf FillActiveWindowsList, 0 )
Return gActiveWindowInfos
End Function
Public Shared Function FindWindow( ByVal classNameOrWindowText As String ) As Integer
Dim hWnd As Integer
If classNameOrWindowText = "" Then
' FindWindowAny takes to Integer parameters and finds any available window.
hWnd = SafeNativeMethods.FindWindowAny( 0 , 0 )
Else
' FindWindowNullWindowCaption attempts to locate a window by class name alone.
hWnd = SafeNativeMethods.FindWindowNullWindowCaption(classNameOrWindowText, 0 )
If hWnd = 0 Then
' FindWindowNullClassName attempts to locate a window by window name alone.
hWnd = SafeNativeMethods.FindWindowNullClassName( 0 , classNameOrWindowText)
End If
End If
Return hWnd
End Function
Public Shared Function FindWindow( ByVal className As String , ByVal windowText As String ) As Integer
Dim hWnd As Integer
If className = "" AndAlso windowText = "" Then
' FindWindowAny takes to Integer parameters and finds any available window.
hWnd = SafeNativeMethods.FindWindowAny( 0 , 0 )
ElseIf className = "" AndAlso windowText <> "" Then
' FindWindowNullClassName attempts to locate a window by window name alone.
hWnd = SafeNativeMethods.FindWindowNullClassName( 0 , windowText)
ElseIf className <> "" AndAlso windowText = "" Then
' FindWindowNullWindowCaption attempts to locate a window by class name alone.
hWnd = SafeNativeMethods.FindWindowNullWindowCaption(className, 0 )
Else
' FindWindow searches for a window by class name and window name.
hWnd = SafeNativeMethods.FindWindow(className, windowText)
End If
Return hWnd
End Function
Public Shared Function ShowWindow( ByVal classNameOrWindowText As String ) As Boolean
Return ShowWindow(FindWindow(classNameOrWindowText))
End Function
Public Shared Function ShowWindow( ByVal className As String , ByVal windowText As String ) As Boolean
Return ShowWindow(FindWindow(className, windowText))
End Function
Public Shared Function ShowWindow( ByVal hWnd As Integer ) As Boolean
If hWnd = 0 Then Return False
' Set the window as foreground.
SafeNativeMethods.SetForegroundWindow(hWnd)
' If window is minimized, simply restore, otherwise show it. Notice the
' declaration of Win32API.IsIconic defines the return value as Boolean
' allowing .NET to marshall the integer value to a Boolean.
If SafeNativeMethods.IsIconic(hWnd) Then
SafeNativeMethods.ShowWindow(hWnd, SW_RESTORE)
Else
SafeNativeMethods.ShowWindow(hWnd, SW_SHOW)
End If
Return True
End Function
Public Shared Function ShowWindow( ByVal hWnd As IntPtr) As Boolean
Return ShowWindow(hWnd.ToInt32)
End Function
Public Shared Function ShowWindow( ByVal form As Form) As Boolean
If form Is Nothing Then Return False
Return ShowWindow(form.Handle)
End Function
Public Shared Function ProcessIsActiveWindow( ByVal hWnd As Integer ) As Boolean
Dim windowText As New StringBuilder(STRING_BUFFER_LENGTH)
Dim windowIsOwned As Boolean
Dim windowStyle As Integer
' Get the windows caption, owner information, and window style.
SafeNativeMethods.GetWindowText(hWnd, windowText, STRING_BUFFER_LENGTH)
windowIsOwned = SafeNativeMethods.GetWindow(hWnd, GW_OWNER) <> 0
windowStyle = SafeNativeMethods.GetWindowLong(hWnd, GWL_EXSTYLE)
' Do not allow invisible processes.
If Not SafeNativeMethods.IsWindowVisible(hWnd) Then
Return False
End If
' Window must have a caption
If windowText.ToString.Equals( "" ) Then
Return False
End If
' Should not have a parent
If SafeNativeMethods.GetParent(hWnd) <> 0 Then
Return False
End If
' Don't allow unowned tool tips
If (windowStyle And WS_EX_TOOLWINDOW) <> 0 And Not windowIsOwned Then
Return False
End If
' Don't allow applications with owners
If (windowStyle And WS_EX_APPWINDOW) = 0 And windowIsOwned Then
Return False
End If
' All criteria were met, window is a valid active window.
Return True
End Function
Private Shared Function FillActiveProcessList( ByVal hWnd As Integer , ByVal lParam As Integer ) As Boolean
Dim mWindowText As New StringBuilder(STRING_BUFFER_LENGTH)
Dim mClassName As New StringBuilder(STRING_BUFFER_LENGTH)
' Get the Window Caption and Class Name. Notice that the definition of Win32API
' functions in the Win32API class are declared differently than in VB6. All Longs
' have been replaced with Integers, and Strings by StringBuilders.
SafeNativeMethods.GetWindowText(hWnd, mWindowText, STRING_BUFFER_LENGTH)
SafeNativeMethods.GetClassName(hWnd, mClassName, STRING_BUFFER_LENGTH)
' Add a new process item to the Processes list view.
Dim processItem As New WindowInfo
With processItem
.WindowText = mWindowText.ToString
.ClassName = mClassName.ToString
.hWnd = hWnd
End With
Append( Of WindowInfo)(gWindowInfos, processItem)
Return True
End Function
Private Shared Function FillActiveWindowsList( ByVal hWnd As Integer , ByVal lParam As Integer ) As Boolean
Dim windowText As New StringBuilder(STRING_BUFFER_LENGTH)
' Get the Window Caption.
SafeNativeMethods.GetWindowText(hWnd, windowText, STRING_BUFFER_LENGTH)
' Only add valid windows to the active windows listbox.
If ProcessIsActiveWindow(hWnd) Then
Dim processItem As New ActiveWindowInfo
processItem.WindowText = windowText.ToString
Append( Of ActiveWindowInfo)(gActiveWindowInfos, processItem)
End If
Return True
End Function
Private Shared Sub Append( Of T)( ByRef collection As T(), ByVal value As T)
ReDim Preserve collection(collection.Length)
collection(collection.Length - 1 ) = value
End Sub
Public Shared Function SetParent( ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
Return SafeNativeMethods.SetParent(hWndChild, hWndNewParent)
End Function
End Class
End Namespace