VB.net 制作半透明窗体 (借鉴自中国教程在线)

 

AnimateWindow是一个窗口打开和关闭时产生动画效果的新函数,

声明:

   Public Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte,dwFlags As Long) As Long

  注释:具体可以使用的常量及其用法

  Const LWA_ALPHA=&H2 注释:表示把窗体设置成半透明样式
  Const LWA_COLORKEY=&H1 注释:表示不显示窗体中的透明色

hwnd是透明窗体的句柄
crKey为颜色值,
bAlpha是透明度,取值范围是[0,255],
dwFlags是透明方式,可以取两个值:
 当取值为LWA_ALPHA时,crKey参数无效,bAlpha参数有效;
 当取值为LWA_COLORKEY时,Alpha参数有效而窗体中的所有颜色为crKey的地方将变为透明。
 LWA_ALPHA = 0x2
 LWA_COLORKEY=0x1
 也可以取两个值的组合:LWA_ALPHA Or LWA_COLORKEY。这样crKey的地方将变为全透明,而其它地方根据bAlpha参数确定透明度。

要使使窗体拥有透明效果,首先要有WS_EX_TRANSPARENT - 透明样式,在同属窗口已重画时该窗口才可重画 扩展属性
(旧的sdk没有定义这个属性,所以可以直接指定为0x80000). 
WS_EX_TRANSPARENT = 0xH20

  具体例子

  程序代码

 

修改窗体的式样,在窗体中加入:
    Protected Overloads Overrides ReadOnly Property CreateParams() As CreateParams
        Get
            Dim cp As CreateParams = MyBase.CreateParams
                 cp.ExStyle = cp.ExStyle Or &H20
                 Return cp
        End Get
    End Property

 

设置透明度:

1. .
2.
    
3.

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 GWL_EXSTYLE = (-20)
Public Const LWA_ALPHA = &H2
Public Const LWA_COLORKEY = &H1

   SetLayeredWindowAttributes(Handle, 1, 0, Win32.ULW_ALPHA)


  第二种使用方法

  SetLayeredWindowAttributes (Me.Handle, &H0, 0, LWA_COLORKEY )
  注释:表明不显示窗体中的透明色
  注释:而第二个参数表示透明色为黑色,并且你可以用RGB函数来指定颜色

 

微软例子:http://support.microsoft.com/default.aspx?scid=kb;en-us;249341

On This Page
SUMMARY
MORE INFORMATION
Step-by-Step Example
REFERENCES

SUMMARY

Microsoft Windows 2000 has the ability to create translucent windows. These windows are called layered windows. This article describes how to create a layered window by using Visual Basic.

Back to the top

MORE INFORMATION

Step-by-Step Example

1. Create a new Standard EXE project in Visual Basic. Form1 is created by default.
2. Add the following code to the General Declarations section of Form1:
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 Const GWL_EXSTYLE = (-20)
Private Const WS_EX_LAYERED = &H80000
Private Const WS_EX_TRANSPARENT = &H20&
Private Const LWA_ALPHA = &H2&

Option Explicit

Private Sub Form_Load()
    Dim lOldStyle As Long
    Dim bTrans As Byte ' The level of transparency (0 - 255)

    bTrans = 128
    lOldStyle = GetWindowLong(Me.hWnd, GWL_EXSTYLE)
    SetWindowLong Me.hWnd, GWL_EXSTYLE, lOldStyle Or WS_EX_LAYERED
    SetLayeredWindowAttributes Me.hWnd, 0, bTrans, LWA_ALPHA
End Sub
					
3. Run the example program by pressing the F5 key. The form should look like a normal Visual Basic window. However, windows that are lower in the Z order should be partially visible through the window. It is also possible to make mouse events go through to the lower windows by changing the preceding code line
SetWindowLong Me.hwnd, GWL_EXSTYLE, lOldStyle Or WS_EX_LAYERED
to:
SetWindowLong Me.hwnd, GWL_EXSTYLE, lOldStyle Or WS_EX_LAYERED Or WS_EX_TRANSPARENT
However, the form does not remain topmost if another form is activated. See the "Reference" section for information on how to create a form that remains on top.

            Win32.UpdateLayeredWindow(Handle, screenDc, topPos, size, memDc, pointSource, 0, blend, Win32.ULW_ALPHA)

你可能感兴趣的:(VB.net 制作半透明窗体 (借鉴自中国教程在线))