加上这句【SetWindowLong hwnd, (-20), &H80000】运行后窗体就会不见。
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 HWND_TOPMOST& = -1
Private Const SWP_NOSIZE& = &H1
Private Const SWP_NOMOVE& = &H2 '置顶
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 GetCursorPos Lib "user32 " (lpPoint As POINTAPI) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type
Dim screen As POINTAPI '屏幕坐标
Private Sub Form_DblClick()
End
End SubPrivate Sub Form_Load()
SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE '置顶
SetWindowLong hwnd, (-20), &H80000 '加上这句,运行后窗体就会不见
End SubPrivate Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
ReleaseCapture
SendMessage hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&
End SubPrivate Sub Timer1_Timer()
GetCursorPos screen '屏幕坐标
If screen.X >= Me.Left / 15 And screen.X <= (Me.Left + Me.Width) / 15 And screen.Y >= Me.Top / 15 And screen.Y <= (Me.Top + Me.Height) / 15 Then
SetLayeredWindowAttributes Me.hwnd, vbBlack, 50, 2
Else
SetLayeredWindowAttributes Me.hwnd, vbBlack, 50, 1
End If
End Sub
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 HWND_TOPMOST& = -1
Private Const SWP_NOSIZE& = &H1
Private Const SWP_NOMOVE& = &H2 '置顶
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 GetCursorPos Lib "user32 " (lpPoint As POINTAPI) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type
Dim screen As POINTAPI '屏幕坐标
Private Sub Form_DblClick()
End
End SubPrivate Sub Form_Load()
SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE '置顶
SetWindowLong hwnd, (-20), &H80000 '加上这句,运行后窗体就会不见
End SubPrivate Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
ReleaseCapture
SendMessage hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&
End SubPrivate Sub Timer1_Timer()
GetCursorPos screen '屏幕坐标
If screen.X >= Me.Left / 15 And screen.X <= (Me.Left + Me.Width) / 15 And screen.Y >= Me.Top / 15 And screen.Y <= (Me.Top + Me.Height) / 15 Then
SetLayeredWindowAttributes Me.hwnd, vbBlack, 50, 2
Else
SetLayeredWindowAttributes Me.hwnd, vbBlack, 50, 1
End If
End Sub
Const LWA_ALPHA = &H2
Const GWL_EXSTYLE = (-20)
Const WS_EX_LAYERED = &H80000
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 LongPrivate Sub Form_Load()
Dim Ret As Long
'Set the window style to 'Layered'
Ret = GetWindowLong(Me.hWnd, GWL_EXSTYLE)
Ret = Ret Or WS_EX_LAYERED
SetWindowLong Me.hWnd, GWL_EXSTYLE, Ret
'Set the opacity of the layered window to 128
'我们可以设置这个数值来控制透明程度
SetLayeredWindowAttributes Me.hWnd, 0, 128, LWA_ALPHA
End Sub
Option Explicit '强制变量不能缺
Private Sub Form_Load()
On Error Resume Next
'********* 设定无边框窗体并置中,此时不能有标题需先清空,再置顶层
Me.BorderStyle = 0: Me.Caption = "" '510是任务栏的高度(大约值,在此不另写计算它的实际高度代码)
Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height - 510) \ 2
SetWindowPos Me.hwnd, -1, 0, 0, 0, 0, 3 '设定窗体置顶
'***************************************** 窗体淡进
Fadeio = 1: LVstep = 20: NowLevel = 0: TransColor = RGB(66, 66, 66)
Call TransParent(Me.hwnd, TransColor, 0)
Timer1.Enabled = True
End SubPrivate Sub Form_Click()
Fadeio = 2: LVstep = -20: NowLevel = 255
Call TransParent(Me.hwnd, TransColor, NowLevel)
Timer1.Enabled = True
End SubPrivate Sub Timer1_Timer()
On Error Resume Next
If NowLevel >= 0 And NowLevel <= 255 Then Call TransParent(Me.hwnd, TransColor, NowLevel)
NowLevel = IIf(Fadeio = 1, IIf(NowLevel + LVstep >= 255, 255, NowLevel + LVstep), IIf(NowLevel + LVstep <= 0, 0, NowLevel + LVstep))
If NowLevel = 0 Or NowLevel = 255 Then
Timer1.Enabled = False
Call TransParent(Me.hwnd, TransColor, NowLevel)
If Fadeio = 2 Then End
End If
End Sub
'************************ Module1.bas 代码
Option Explicit
'**************************************** 窗体置顶的 API
Public 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
'**************************************** 窗体淡进淡出用到的API 与常量宣告
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Public Const WS_EX_LAYERED = &H80000
Public Const GWL_EXSTYLE = (-20)
Public Const LWA_ALPHA = &H2
Public Const LWA_COLORKEY = &H1
'********************************* 全局变量的宣告
Global AppDisk$, VoiceDisk$, i&, Rtn&, NowLevel&, TransColor&, Fadeio%, LVstep&
Global LR As Boolean, UD As Boolean
Sub Main() '启动程序
If App.PrevInstance Then MsgBox "本系统已运行中, 不得重复加载!!": End
AppDisk = IIf(Right(App.Path, 1) = "\", App.Path, App.Path & "\")
Fade.Show '进入主程序
End Sub'*********** 让窗体透明并且屏蔽指定颜色
Public Sub TransParent(ByVal Phwnd As Long, ByVal TColor As Long, Tlevel As Long)
'On Error Resume Next
Rtn = GetWindowLong(Phwnd, GWL_EXSTYLE)
Rtn = Rtn Or WS_EX_LAYERED
SetWindowLong Phwnd, GWL_EXSTYLE, Rtn
SetLayeredWindowAttributes Phwnd, TColor, Tlevel, LWA_COLORKEY Or LWA_ALPHA '将扣去窗口中的指定颜色背景
End Sub
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 HWND_TOPMOST& = -1
Private Const SWP_NOSIZE& = &H1
Private Const SWP_NOMOVE& = &H2 '置顶
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 GetCursorPos Lib "user32 " (lpPoint As POINTAPI) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type
Dim screen As POINTAPI '屏幕坐标
Private Sub Form_DblClick()
End
End SubPrivate Sub Form_Load()
SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE '置顶
SetWindowLong hwnd, (-20), &H80000 '加上这句,运行后窗体就会不见
End SubPrivate Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
ReleaseCapture
SendMessage hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&
End SubPrivate Sub Timer1_Timer()
GetCursorPos screen '屏幕坐标
If screen.X >= Me.Left / 15 And screen.X <= (Me.Left + Me.Width) / 15 And screen.Y >= Me.Top / 15 And screen.Y <= (Me.Top + Me.Height) / 15 Then
SetLayeredWindowAttributes Me.hwnd, vbBlack, 50, 2
Else
SetLayeredWindowAttributes Me.hwnd, vbBlack, 50, 1
End If
End Sub
至于使窗体透明,参见我上面的代码
Option Explicit
Private Sub Form_Load()
TransColor = vbBlue '指定要屏蔽的颜色 以便于制作不规则且半透明的窗体
FadeIO = 2: NowLevel = 255
Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2 '窗体居中
End SubPrivate Sub Form_Click()
FadeIO = IIf(FadeIO = 1, 2, 1)
NowLevel = IIf(FadeIO = 1, 150, 255)
Call TransParent(Me.hwnd, TransColor, NowLevel)
End SubPrivate Sub Form_Unload(Cancel As Integer)
Call ReleaseTrans(Me.hwnd) '释放内存
End Sub
'********************* .bas 代码
Option Explicit
'**************************************** 窗体透明用到的API 与常量宣告
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Public Const WS_EX_LAYERED = &H80000
Public Const GWL_EXSTYLE = (-20)
Public Const LWA_ALPHA = &H2
Public Const LWA_COLORKEY = &H1
'********************************* 全局变量
Global TransColor&, Rtn&, FadeIO&, LVstep&, NowLevel&
Public Sub TransParent(ByVal Phwnd As Long, ByVal TColor As Long, Tlevel As Long) '让窗体透明并且屏蔽指定的透明色
On Error Resume Next
Rtn = GetWindowLong(Phwnd, GWL_EXSTYLE)
Rtn = Rtn Or WS_EX_LAYERED
SetWindowLong Phwnd, GWL_EXSTYLE, Rtn
SetLayeredWindowAttributes Phwnd, TColor, Tlevel, LWA_COLORKEY Or LWA_ALPHA '将扣去窗口中的指定颜色背景
End SubPublic Sub ReleaseTrans(ByVal Phwnd As Long) '释放影像内存
On Error Resume Next
Rtn = GetWindowLong(Phwnd, GWL_EXSTYLE)
Rtn = Rtn And Not WS_EX_LAYERED
SetWindowLong Phwnd, GWL_EXSTYLE, Rtn
End Sub