如何製作 “透明表單” ( Transparent Form ) 特效

如何製作 “透明表單” ( Transparent Form ) 特效

如何製作 "透明表單" ( Transparent Form ) 特效

<< VB.Net >>

設定Opacity 屬性 : 表單的透明度等級。預設值為 1.00。

Opacity 屬性可讓您指定表單和其控制項的透明度等級。

當將這個屬性設為小於 100% (1.00) 的值時,整個表單 (包括框線) 將變得更為透明。

將這個屬性設為 0% (0.00) 的值則使表單完全不可見。

如 :

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

Me.Opacity = 0.5

End Sub

您可以使用這個屬性,提供不同等級的透明度,或提供效果,例如將表單漸層引入或引出視界。

您可以將 Opacity 屬性設為值 0% (0.00),並逐漸增加數值直至它達到 100% (1.00),將表單漸層引入視界。

如 :

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

Opacity = 0

End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint

For dblLoop As System.Double = 0.01 To 1 Step 0.01

Opacity = dblLoop

Application.DoEvents()

Threading.Thread.Sleep(12)

Next

End Sub

================================================================

<< VB6 >>

Call API SetLayeredWindowAttributes

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 Sub Command1_Click()

MakeTransparent hwnd, 155

End Sub

' Para1 傳 Handle Value , Para2 傳 1 ~ 254 之間的值 , 值越小透明度越高

Sub MakeTransparent(ByVal hwnd As Long, Perc As Byte)

If (Perc > 0 Or Perc < 255) And Len(Environ("OS")) > 0 Then

SetWindowLong hwnd, -20, GetWindowLong(hwnd, -20) + 524288

SetLayeredWindowAttributes hwnd, 0, Perc, 2

End If

End Sub

API 說明如下 :

GetWindowLong :

The GetWindowLong function retrieves information about the specified window.

The function also retrieves the 32-bit (long) value at the specified offset into the extra window memory of a window.

SetWindowLong :

The SetWindowLong function changes an attribute of the specified window.

The function also sets a 32-bit (long) value at the specified offset into the extra window memory of a window.

SetWindowLong :

The SetLayeredWindowAttributes function sets the opacity and transparency color key of a layered window.

( API 說明請參考 API Guide )

Transparent Form 圖