by Raghunathan (1 Submission)
Category: Windows System Services
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Wed 15th November 2006
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Creating a VB Form with Opacity Control like Windows Forms in .NET Framework, using Win32 Api (Works only with Windows 2000 and above)
API Declarations
'1) Create a Standard EXE Application
'2) Add 1 Command Button(Command1) and 2 Timer Controls (Timer1, Timer2) in the 'Form
'3) Copy and Paste this code in the code module of the form and run
'4) !!!!!!!!!!!!!!!!ENJOY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
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 crey As Byte, ByVal bAlpha As Byte, ByVal dwFlags
As Long) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As
Long, ByVal bRevert As Long) As Long
Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As
Long) As Long
Private Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Long,
ByVal nPosition As Long, ByVal wFlags 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 MF_BYPOSITION = &H400&
Private Const GWL_EXSTYLE = (-20)
Private Const WS_EX_LAYERED = &H80000
Private Const WS_EX_TRANSPARENT = &H20&
Private Const LWA_ALPHA = &H2&
Private Const HWND_TOPMOST = -1
Option Explicit
Dim bTrans As Byte ' The level of transparency (0 - 255)
Dim lOldStyle As Long
Unload Me
End Sub
Private Sub Form_Initialize()
If App.PrevInstance Then
MsgBox "Application is Already Running", vbExclamation, "Opaque"
End
End If
End Sub
Private Sub Form_Load()
bTrans = 0
'To Set the Style of the Window as LAYERED so that opacity can be
controlled
lOldStyle = SetWindowLong(Me.hwnd, GWL_EXSTYLE, WS_EX_LAYERED)
Timer1.Enabled = True
Timer1.Interval = 25
'To Disable the Close Button (X) of the Form
Dim hMenu As Long
hMenu = GetSystemMenu(Me.hwnd, False)
DeleteMenu hMenu, 6, MF_BYPOSITION
'To Make the Window On Top Of All The Windows
SetWindowPos Me.hwnd, HWND_TOPMOST, 10, 10, 345, 100, 0
End Sub
Private Sub Form_Unload(Cancel As Integer)
Cancel = True
Timer2.Enabled = True
Timer2.Interval = 25
End Sub
Private Sub Timer1_Timer()
bTrans = bTrans + 5
If bTrans >= 255 Then
Timer1.Enabled = False
Exit Sub
End If
SetLayeredWindowAttributes Me.hwnd, 0, bTrans, LWA_ALPHA
End Sub
Private Sub Timer2_Timer()
bTrans = bTrans - 5
If bTrans <= 0 Then
Timer2.Enabled = False
End
End If
SetLayeredWindowAttributes Me.hwnd, 0, bTrans, LWA_ALPHA
End Sub
No comments have been posted about Creating a VB Form with Opacity Control like Windows Forms in .NET Framework, using Win32 Api (Work. Why not be the first to post a comment about Creating a VB Form with Opacity Control like Windows Forms in .NET Framework, using Win32 Api (Work.