製造出透明的Form

來源:cww

說實在的我不知道這所謂透明的Form用在什麼地方,它變成透明後,只要有所移動,
或Mouse移到TitleBae處時,結果總是令人感到莫名奇妙,所以我設定BorderStyle = 0
,令之沒有TitleBar。網路上見許多人問起,故有這程式;若有人知道這是用在何處,
請告訴我。

後來我在一個不小心,發現另一種作法令Form變透明的方式,而且比目前的作法好用
請參考透明的Form上顯示背景透通圖

可以用SetWindowLong(hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT)來完成

'form上有Command1, command2兩個Button並事先設定form之BorderStyle = 0

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
Const GWL_EXSTYLE = (-20)
Const WS_EX_TRANSPARENT = &H20&
Private PreValue As Long

Private Sub Command2_Click() '還原變成不透明
Call SetWindowLong(Me.hwnd, GWL_EXSTYLE, PreValue)
Me.Hide
Me.Show
End Sub

Private Sub Form_Load()
Dim i As Long

i = GetWindowLong(Me.hwnd, GWL_EXSTYLE)
'變成透明的Form
PreValue = SetWindowLong(Me.hwnd, GWL_EXSTYLE, i Or WS_EX_TRANSPARENT)
Me.Show
DoEvents
Command1.Refresh '令Command1可見
Command2.Refresh '令Command2可見
End Sub