Form Transparan
Membuat Form Transparan pada Visual Basic
Berikut ini adalah tips sederhana untuk membuat agar control Form menjadi Transparan.Dalam hal ini kita akan menggunakan module. Selanjutnya kita beri nama: modTransparan.bas
Langkah-1: Tambahkan Module baru ke project anda dan berilah nama modTransparan.bas
Langkah-2: Copy-paste script module modTransparan.bas berikut ini kedalam module tadi.
Option Explicit
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 bDefaut As Byte, ByVal dwFlags As Long) As Long
Private Const GWL_EXSTYLE As Long = (-20)
Private Const LWA_COLORKEY As Long = &H1
Private Const LWA_Defaut As Long = &H2
Private Const WS_EX_LAYERED As Long = &H80000
Dim VoirStyle As String
'
Public Function Transparency(ByVal hWnd As Long, Optional ByVal Col As Long = vbBlack, Optional ByVal PcTransp As Byte = 255, Optional ByVal TrMode As Boolean = True) As Boolean
' Return : True if there is no error.
' hWnd : hWnd of the window to make transparent
' Col : Color to make transparent if TrMode=False
' PcTransp : 0 Ã 255 >> 0 = transparent -:- 255 = Opaque
Dim DisplayStyle As Long
On Error GoTo errOK
VoirStyle = GetWindowLong(hWnd, GWL_EXSTYLE)
If DisplayStyle <> (DisplayStyle Or WS_EX_LAYERED) Then
DisplayStyle = (DisplayStyle Or WS_EX_LAYERED)
Call SetWindowLong(hWnd, GWL_EXSTYLE, DisplayStyle)
End If
Transparency = (SetLayeredWindowAttributes(hWnd, Col, PcTransp, IIf(TrMode, LWA_COLORKEY Or LWA_Defaut, LWA_COLORKEY)) <> 0)
errOK:
If Not Err.Number = 0 Then Err.Clear
End Function
Public Sub ActiveTransparency(M As Form, d As Boolean, F As Boolean, _
T_Transparency As Integer, Optional Color As Long)
Dim B As Boolean
If d And F Then
'Makes color (here the background color of the shape) transparent
'upon value of T_Transparency
B = Transparency(M.hWnd, Color, T_Transparency, False)
ElseIf d Then
'Makes form, including all components, transparent
'upon value of T_Transparency
B = Transparency(M.hWnd, 0, T_Transparency, True)
Else
'Restores the form opaque.
B = Transparency(M.hWnd, , 255, True)
End If
End Sub
Kemudian, simpanlah.
Langkah-3: Pada control Form Anda, khususnya pada jendela Properties, aturlah property BackColor bernilai: Hitam atau sbb:
BackColor: &H00000000&
Agar, tampilan form lebih elegan, anda dapat mengubah property BorderStyle pada jendela Properties ke None atau bernilai none (dengan nilai none, maka header form tidak akan muncul dan anda dapat membuat tombol-tombol sendiri sesuka hati).
Langkah-4: Pada control Form anda, klik kanan dan pilih [view code] dan pilihlah, Form_Load. Kemudian masukkan kode berikut ini ke blok kode Form_Load tersebut, sehingga menjadi:
Private Sub Form_Load()
ActiveTransparency Me, True, False, 0
Me.Show
ActiveTransparency Me, True, False, 250
Me.Refresh
End Sub
Untuk mengatur tingkat transparansi pada control Form anda tersebut, dapat mengurangi nilai pada 250 diatas.
Nilai transparansi minimum atau 'tanpa transparansi' = 255
Nilai transparansi maksimum = 0
Sehingga tingkat transparansi dapat diatur antara angka 0 s/d 255.
Post a Comment for "Form Transparan"