Attribute VB_Name = "ModOnTop"
Option Explicit
'*************************************************************************
'**模 块 名：ModOnTop
'**说    明：将窗体放到Z轴最上层,设置窗体透明度,鼠标是否能穿透
'**创 建 人：嗷嗷叫的老马
'**日    期：2003年12月17日
'**备    注: 紫水晶工作室 版权所有
'**          更多模块/类模块请访问我站:  http://www.m5home.com
'**版    本：V1.0
'*************************************************************************

Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hWnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

Private Const LWA_ALPHA = &H2
Private Const GWL_EXSTYLE = (-20)
Private Const WS_EX_LAYERED = &H80000
Private Const WS_EX_TRANSPARENT As Long = &H20&
Private Const SWP_NOSIZE& = &H1
Private Const SWP_NOMOVE& = &H2
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private Const HWND_BROADCAST = &HFFFF&
Private Const HWND_TOP = 0

Public Sub OnTop(ByVal tForm As Form, Optional ByVal top As Boolean = True, Optional TouMing As Long = 255, Optional cMouse As Boolean = False)
    '*************************************************************************
    '**参 数 名：Top
    '**说    明：是否将窗体放到Z轴最上层
    '**参 数 名：TouMing
    '**说    明：窗体透明度(WIN2000及以上有效)
    '**参 数 名：cMouse
    '**说    明：鼠标是否能穿透
    '*************************************************************************
    Dim Ret As Long

    Ret = GetWindowLong(tForm.hWnd, GWL_EXSTYLE)
    Ret = Ret Or WS_EX_LAYERED

    If cMouse Then
        Ret = Ret Or WS_EX_TRANSPARENT
    Else
        If Ret And WS_EX_TRANSPARENT Then Ret = Ret - WS_EX_TRANSPARENT
    End If

    SetWindowLong tForm.hWnd, GWL_EXSTYLE, Ret

    If TouMing < 0 Then TouMing = 0
    If TouMing > 255 Then TouMing = 255

    SetLayeredWindowAttributes tForm.hWnd, 0, TouMing, LWA_ALPHA

    If top = True Then
        SetWindowPos tForm.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
    Else
        SetWindowPos tForm.hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
    End If
End Sub
