|
楼主 |
发表于 2018-2-2 16:13
|
显示全部楼层
前面,我们已经能够获取窗体的句柄,这意味着我们可以利用它来大作文章了。
具体到我的这个主题,利用UserForm1的句柄,对它的窗体风格进行修改,使之具有“双透明特征”。
这就要用到GetWindowLong、SetWindowLong,以及SetLayeredWindowAttributes 这三个函数,分别用于获取和修改窗体的风格特征。
同时,这两个函数使用一些命名常量做为参数,以便于编写和阅读代码。这些,也与上面的函数一道,而作为外部引用的申明,加入到“模块1”的开始部分。
内容如下:
- Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
- Private Const GWL_STYLE = (-16)
- Private Const GWL_EXSTYLE = (-20)
- Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
- Private Const WS_BORDER = &H800000
- Private Const WS_CAPTION = &HC00000 ' WS_BORDER Or WS_DLGFRAME
- Private Const WS_CHILD = &H40000000
- Private Const WS_EX_TOOLWINDOW = &H80&
- Private Const WS_EX_TRANSPARENT As Long = &H20&
- Private Const WS_EX_LAYERED = &H80000
- 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 LWA_ALPHA = &H2
- Private Const LWA_COLORKEY = &H1
复制代码
然后,编写自定义过程,调用以上API函数对窗体的风格做修改。
这部分的代码如下:
'模块1的部分代码
- Function ModifyWindowStyleEx(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) as Boolean
- dim lStyle as long
- lStyle = GetWindowLong(hwnd, nIndex)
- lStyle = lStyle or dwNewLong
- ModifyWindowStyleEx = SetWindowLong(hwnd, nIndex, lStyle)
- End Function
- Sub SetWindowTRANSPARENT(ByVal hwnd As Long) '使窗口对鼠标按键透明
- ModifyWindowStyleEx hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT
- End Sub
- Sub SetWindowLayeredAlpha(ByVal hwnd As Long) '使窗口的整个图形透明(透明程度为40)
- ModifyWindowStyleEx hwnd, GWL_EXSTYLE, WS_EX_LAYERED
- SetLayeredWindowAttributes hwnd, 0, 40, LWA_ALPHA '(透明程度取值范围:0至255,取0,则全部透明,取255,则全部不透明)
- End Sub
复制代码
|
|