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

王朝vc·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

现在有不少软件都采用了透明的窗口作为界面,如何在你的程序中实现呢?我发现在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

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航