HOW TO:窗口的前置、切换和显示

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值