用VB无窗口透明Usercontrol编写透明浮动按钮

这篇博客介绍了如何使用VB6创建一个无窗口透明的Usercontrol,实现透明浮动按钮。通过设置控件属性、利用API函数处理鼠标事件和坐标计算,达到XP风格的按钮效果。虽然设计时无法直接选择控件,但提供了完整的代码示例。
摘要由CSDN通过智能技术生成

        使用无窗口透明控件,好处是控件不是从窗口类继承而来的,可以节省一些内存资源,难点是鼠标控制和坐标计算,缺点是无法直接响应键盘事件。而且,需要注意的是,在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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值