使用无窗口透明控件,好处是控件不是从窗口类继承而来的,可以节省一些内存资源,难点是鼠标控制和坐标计算,缺点是无法直接响应键盘事件。而且,需要注意的是,在Paint事件里,坐标是从(0,0)开始的,而在其它地方,坐标是相对于容器的。缺点是设计时,无法用鼠标点选控件(只能用键盘移动),微软公司也在MSDN里说了这是一个BUG,但至今未解决。
打开VB6,新建一个“标准 EXE”,然后添加一个用户控件,把控件的BackStyle属性设置为“透明”,把Windowless属性设置为“True”,再把俺的代码复制过去,一个美观大方的透明浮动按钮便完成了。用户还可以设置控件的Caption属性和Picture属性进一步美化。如有不解之处,可与俺联系:
代码如下:
'* ************************************************************** *
'* 程序名称:Button.ctl
'* 程序功能:透明浮动按扭
'* 作者:lyserver,最后修改日期:2009年11月
'* 联系方式:http://blog.csdn.net/lyserver
'* ************************************************************** *
Option Explicit
'----------------------------------------------------------------------
' API 声明
'----------------------------------------------------------------------
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function SetRect Lib "user32" (ByRef lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function OffsetRect Lib "user32" (ByRef lpRect As RECT, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (ByRef lpPoint As POINTAPI) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, ByRef lpPoint As POINTAPI) As Long
Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, ByRef lpPoint As POINTAPI) As Long
Private Declare Function DrawEdge Lib "user32" (ByVal hdc As Long, ByRef qrc As RECT, ByVal edge As Long, ByVal grfFlags As Long) As Long
Private Const BDR_RAISED = &H5
Private Const BDR_SUNKEN = &HA
Private Const BDR_RAISEDINNER = &H4
Private Const BDR_SUNKENINNER = &H8
Private Const BDR_RAISEDOUTER = &H1
Private Const BDR_SUNKENOUTER = &H2
Private Const BF_RECT = &HF
Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, ByRef lpRect As RECT, ByVal wFormat As Long) As Long
Private Const DT_CENTER = &H1
Private Const DT_VCENTER = &H4
Private Const DT_TOP = &H0
Private Const DT_BOTTOM = &H8
Private Const DT_LEFT = &H0
Private Const DT_RIGHT = &H2
Private Const DT_SINGLELINE = &H20
Private Declare Function SetCursor Lib "user32" (ByVal hCursor As Long) As Long
Private Declare Function LoadCursorBynum& Lib "user32" Alias "LoadCursorA" (ByVal hInstance As Long, ByVal lpCursorName As Long)
Private Const IDC_HAND = 32649&
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
'----------------------------------------------------------------------
' 公共枚举类型
'----------------------------------------------------------------------
Public Enum TextAlignConstants
[Top] = DT_TOP Or DT_CENTER Or DT_SINGLELINE
[Bottom] = DT_BOTTOM Or DT_CENTER Or DT_SINGLELINE
[Left] = DT_LEFT Or DT_VCENTER Or DT_SINGLELINE
[Right] = DT_RIGHT Or DT_VCENTER Or DT_SINGLELINE
[Center] = DT_CENTER Or DT_VCENTER Or DT_SINGLELINE
End Enum