在Win2k下实现半透明窗口的两个步骤(修补稿1.2)

类别:VC语言 点击:0 评论:0 推荐:

 

现在有不少软件都采用了透明的窗口作为界面,如何在你的程序中实现呢?我发现在Win2k中提供了这种支持,只要简单的几行代码就可实现。(本例子目前只适用于Win2k )

拿一个对话框为例:

第一步 定义功能

typedef BOOL (FAR PASCAL * FUNC1)(
  HWND hwnd,           // handle to the layered window
  COLORREF crKey,      // specifies the color key
  BYTE bAlpha,         // value for the blend function
  DWORD dwFlags        // action
);

第二步 实现代码

在OnInitDialog中加入下列代码

.....
 HMODULE hModule = GetModuleHandle("user32.dll");
 FUNC1 SetLayeredWindowAttributes;
 SetLayeredWindowAttributes =  (FUNC1) GetProcAddress (hModule, _T( "SetLayeredWindowAttributes" ) );

 // 设置分层扩展标记
 SetWindowLong(GetSafeHwnd(), GWL_EXSTYLE, GetWindowLong(GetSafeHwnd(), GWL_EXSTYLE) | 0x80000L);
 // 70% alpha
 SetLayeredWindowAttributes(GetSafeHwnd(), 0, (255 * 70) / 100, 0x2);

工作完成,怎么样,现在你可以运行你的程序来查看效果,即使背景变化也能立刻反映到你的窗口当中,这一点比金山词霸的效果要好。

 

看到下面这段文章的人有福了,这是新加入的:

第三步:如何除去透明选项?

 // 除去分层扩展标记
SetWindowLong(GetSafeHwnd(),, GWL_EXSTYLE,
        GetWindowLong(GetSafeHwnd(), GWL_EXSTYLE) & ~ 0x80000L);
 // 重画窗口
RedrawWindow();

第四部:VB的透明窗口(摘自MSDN)

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 crey As Byte, 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

总算完整了一点。多谢观看。

本文地址:http://com.8s8s.com/it/it3190.htm