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

<script type=text/javascript> var sectionFilter = "type != 'notice' && type != 'securedata' && type != 'querywords'"; var tocArrow = "/library/images/support/kbgraphics/public/en-us/downarrow.gif"; var depthLimit = 10; var depth3Limit = 10; var depth4Limit = 5; var depth5Limit = 3; var tocEntryMinimum = 1; </script> <script src="/common/script/gsfx/kbtoc.js?13" type=text/javascript></script>

SUMMARY

<script type=text/javascript>loadTOCNode(1, 'summary');</script>
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

<script type=text/javascript>loadTOCNode(1, 'moreinformation');</script>

Step-by-Step Example

<script type=text/javascript>loadTOCNode(2, 'moreinformation');</script>
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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值