Author:水如烟
Namespace
LzmTW.Util
Public Class Window
' '' <summary>
' '' 窗口显示
' '' </summary>
Public Shared Sub Show( ByVal form As Form)
Show(form.Handle)
End Sub
Public Shared Sub Show( ByVal hWnd As IntPtr)
SetForegroundWindow(hWnd)
If IsIconic(hWnd) Then
ShowWindow(hWnd, SW_RESTORE)
Else
ShowWindow(hWnd, SW_SHOW)
End If
End Sub
' '' <summary>
' '' 窗口前置
' '' </summary>
Public Shared Sub SetForceForeground( ByVal form As Form)
SetForceForeground(form.Handle)
End Sub
Public Shared Sub SetForceForeground( ByVal hWnd As IntPtr)
If hWnd = GetForegroundWindow() Then Return
Dim ThreadId1 As Integer
Dim ThreadId2 As Integer
ThreadId1 = GetWindowThreadProcessId(GetForegroundWindow, 0 )
ThreadId2 = GetWindowThreadProcessId(hWnd, 0 )
If ThreadId1 <> ThreadId2 Then
AttachThreadInput(ThreadId1, ThreadId2, True )
SetForegroundWindow(hWnd)
AttachThreadInput(ThreadId1, ThreadId2, False )
Else
SetForegroundWindow(hWnd)
End If
If IsIconic(hWnd) Then
ShowWindow(hWnd, SW_RESTORE)
Else
ShowWindow(hWnd, SW_SHOW)
End If
End Sub
' '' <summary>
' '' 窗口切换
' '' </summary>
Public Shared Sub SwitchTo( ByVal form As Form, Optional ByVal normal As Boolean = True )
SwitchTo(form.Handle, normal)
End Sub
Public Shared Sub SwitchTo( ByVal hWnd As IntPtr, Optional ByVal normal As Boolean = True )
SwitchToThisWindow(hWnd, normal)
End Sub
< DllImport( " user32.dll " , CharSet: = CharSet.Auto, SetLastError: = True ) > _
Private Shared Function SwitchToThisWindow( ByVal hWnd As IntPtr, ByVal bRestore As Boolean ) As Boolean
End Function
< DllImport( " user32.dll " , CharSet: = CharSet.Auto, SetLastError: = True ) > _
Private Shared Function ShowWindow( _
ByVal hwnd As IntPtr, _
ByVal nCmdShow As Integer ) As Integer
End Function
< DllImport( " user32.dll " , CharSet: = CharSet.Auto, SetLastError: = True ) > _
Private Shared Function SetForegroundWindow( ByVal hwnd As IntPtr) As Integer
End Function
< DllImport( " user32.dll " , CharSet: = CharSet.Auto, SetLastError: = True ) > _
Private Shared Function IsIconic( ByVal hwnd As IntPtr) As Boolean
End Function
< DllImport( " user32.dll " , CharSet: = CharSet.Auto, SetLastError: = True ) > _
Private Shared Function GetForegroundWindow() As IntPtr
End Function
< DllImport( " user32.dll " , SetLastError: = True ) > _
Private Shared Function GetWindowThreadProcessId( _
ByVal handle As IntPtr, _
< Out() > ByRef processId As Integer ) As Integer
End Function
< DllImport( " user32 " , CharSet: = CharSet.Ansi, SetLastError: = True , ExactSpelling: = True ) > _
Private Shared Function AttachThreadInput( _
ByVal idAttach As Integer , _
ByVal idAttachTo As Integer , _
ByVal fAttach As Boolean ) As Integer
End Function
Private Const SW_RESTORE As Integer = 9
Private Const SW_SHOW As Integer = 5
End Class
End Namespace
Public Class Window
' '' <summary>
' '' 窗口显示
' '' </summary>
Public Shared Sub Show( ByVal form As Form)
Show(form.Handle)
End Sub
Public Shared Sub Show( ByVal hWnd As IntPtr)
SetForegroundWindow(hWnd)
If IsIconic(hWnd) Then
ShowWindow(hWnd, SW_RESTORE)
Else
ShowWindow(hWnd, SW_SHOW)
End If
End Sub
' '' <summary>
' '' 窗口前置
' '' </summary>
Public Shared Sub SetForceForeground( ByVal form As Form)
SetForceForeground(form.Handle)
End Sub
Public Shared Sub SetForceForeground( ByVal hWnd As IntPtr)
If hWnd = GetForegroundWindow() Then Return
Dim ThreadId1 As Integer
Dim ThreadId2 As Integer
ThreadId1 = GetWindowThreadProcessId(GetForegroundWindow, 0 )
ThreadId2 = GetWindowThreadProcessId(hWnd, 0 )
If ThreadId1 <> ThreadId2 Then
AttachThreadInput(ThreadId1, ThreadId2, True )
SetForegroundWindow(hWnd)
AttachThreadInput(ThreadId1, ThreadId2, False )
Else
SetForegroundWindow(hWnd)
End If
If IsIconic(hWnd) Then
ShowWindow(hWnd, SW_RESTORE)
Else
ShowWindow(hWnd, SW_SHOW)
End If
End Sub
' '' <summary>
' '' 窗口切换
' '' </summary>
Public Shared Sub SwitchTo( ByVal form As Form, Optional ByVal normal As Boolean = True )
SwitchTo(form.Handle, normal)
End Sub
Public Shared Sub SwitchTo( ByVal hWnd As IntPtr, Optional ByVal normal As Boolean = True )
SwitchToThisWindow(hWnd, normal)
End Sub
< DllImport( " user32.dll " , CharSet: = CharSet.Auto, SetLastError: = True ) > _
Private Shared Function SwitchToThisWindow( ByVal hWnd As IntPtr, ByVal bRestore As Boolean ) As Boolean
End Function
< DllImport( " user32.dll " , CharSet: = CharSet.Auto, SetLastError: = True ) > _
Private Shared Function ShowWindow( _
ByVal hwnd As IntPtr, _
ByVal nCmdShow As Integer ) As Integer
End Function
< DllImport( " user32.dll " , CharSet: = CharSet.Auto, SetLastError: = True ) > _
Private Shared Function SetForegroundWindow( ByVal hwnd As IntPtr) As Integer
End Function
< DllImport( " user32.dll " , CharSet: = CharSet.Auto, SetLastError: = True ) > _
Private Shared Function IsIconic( ByVal hwnd As IntPtr) As Boolean
End Function
< DllImport( " user32.dll " , CharSet: = CharSet.Auto, SetLastError: = True ) > _
Private Shared Function GetForegroundWindow() As IntPtr
End Function
< DllImport( " user32.dll " , SetLastError: = True ) > _
Private Shared Function GetWindowThreadProcessId( _
ByVal handle As IntPtr, _
< Out() > ByRef processId As Integer ) As Integer
End Function
< DllImport( " user32 " , CharSet: = CharSet.Ansi, SetLastError: = True , ExactSpelling: = True ) > _
Private Shared Function AttachThreadInput( _
ByVal idAttach As Integer , _
ByVal idAttachTo As Integer , _
ByVal fAttach As Boolean ) As Integer
End Function
Private Const SW_RESTORE As Integer = 9
Private Const SW_SHOW As Integer = 5
End Class
End Namespace