|
修改了一个网上找的VB代码,使之适用于VBA,做了一个附件。可以实现大部分控件的提示。- '---------------说明 修改者:Excelhome-Moneky ------------------
- '------------------------20120419---------------------------------
- '类模块是网上前辈的VB代码,本人是在原来的代码基础上做了一点点修改,使之能够用于VBA,下面是修改说明
- '1、原来的模块中没有Destroy方法——已添加
- '2、VBA窗体控件的hwnd无法取得,即使用Getfcous得到的也只是窗口客户区的hwnd(或许),本例子中,一律使用客户区的hwnd,并把该句柄存入控件的tag属性中,
- ' 然后在本类中用clng函数转换成long
- '3、在vb下可以方便取得控件的rect,在本例中是用客户区的rect与控件的top,left,width,height综合运算得出实际控件的rect(另外ListBox可以正常取得客户区rect,所以在类中特别处理了)
- ' (其中坐标转换代码是直接引用的http://club.excelhome.net/forum.php?mod=viewthread&tid=203573,Excelhome——winland大侠的代码)
- '4、Image与Label控件目前没有办法实现,但可能性还是有的。
- '5、在创建提示的过程中,会转换焦点,因此请把Tab编号小的控件(默认控件)放在最后创建
- '6、VBA调用范例,见窗体代码(下面为原样复制的窗体代码)
- '------------VBA窗体代码,窗体上有两个commandbutton控件,name分别为:cmdOK,cmdNOOK------------
- 'Private Declare Function GetFocus Lib "user32" () As Long
- 'Dim myTip(1) As New cToolTip
- 'Sub mySetTip(vObj As Object, vTip As cToolTip, strTitle As String, strTipText As String) '这是一个自定义创建提示的过程参数分别为(控件,提示变量,标题,提示文本)
- ' vObj.SetFocus '控件获取焦点——一些控件无法获取焦点,所以目前无法实现他们的冒泡提示
- ' vObj.Tag = GetFocus '将窗口客户区hwnd存入控件Tag
- ' Set vTip.ParentControl = vObj '控件绑定
- ' vTip.ToolTipTitle = strTitle '标题
- ' vTip.ToolTipText = strTipText '提示文字
- ' vTip.Create '创建
- 'End Sub
- 'Private Sub UserForm_Initialize()
- ' mySetTip Me.cmdOK, myTip(0), "气泡标题_cmdOK", "这是一个演示用的提示文字" & vbNewLine & "它还可以换行显示,这是第二行提示文字!"
- ' mySetTip Me.cmdNoOk, myTip(1), "气泡标题_cmdNoOk", "这是一个演示用的提示文字" & vbNewLine & "它还可以换行显示,这是第二行提示文字!"
- 'End Sub
- 'Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
- ' myTip(0).Destroy
- ' myTip(1).Destroy
- 'End Sub
- '--------------VBA窗体代码结束------
- '--------------修改说明结束---------
- 'clsTooptip
- '使用范例:
- 'Dim tooltip As New Class1
- 'Set tooltip.ParentControl = Text1 '气泡应用于哪个控件(要有Hwnd)
- 'tooltip.ToolTipTitle = "气泡标题" '气泡标题(不允许换行/字体粗体)
- 'tooltip.ToolTipText = "气泡内容" & vbCrLf & "123" '气泡内容(允许换行)
- 'tooltip.Create '创建气泡
- Option Explicit
- Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
- Private Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long '创建窗口
- Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long '发出消息
- Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
- Private Const WM_USER = &H400
- Private Const CW_USEDEFAULT = &H80000000
- Private Type RECT
- Left As Long
- Top As Long
- Right As Long
- Bottom As Long
- End Type
- Private Const TTS_NOPREFIX = &H2
- Private Const TTF_TRANSPARENT = &H100
- Private Const TTF_CENTERTIP = &H2
- Private Const TTM_ADDTOOLA = (WM_USER + 4)
- Private Const TTM_ACTIVATE = WM_USER + 1
- Private Const TTM_UPDATETIPTEXTA = (WM_USER + 12)
- Private Const TTM_SETMAXTIPWIDTH = (WM_USER + 24)
- Private Const TTM_SETTITLE = (WM_USER + 32)
- Private Const TTS_BALLOON = &H40
- Private Const TTF_SUBCLASS = &H10
- Private Const TOOLTIPS_CLASSA = "tooltips_class32"
- Private Type TOOLINFO
- lSize As Long
- lFlags As Long
- lHwnd As Long
- lId As Long
- lpRect As RECT
- hInstance As Long
- lpStr As String
- lParam As Long
- End Type
- Private TTTitle As String
- Private TTParentControl As Object
- Private TTStyle As TTStyleEnum
- Public Enum TTStyleEnum
- TTStandard
- TTBalloon
- End Enum
- Private hToolTipHwnd As Long
- Private TI As TOOLINFO
- Public Function Create() As Boolean '创建气泡函数
- Dim lpRect As RECT
- ' DestroyWindow hToolTipHwnd
- '建立tooltip窗口
- hToolTipHwnd = CreateWindowEx(0, TOOLTIPS_CLASSA, vbNullString, TTS_BALLOON, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CLng(TTParentControl.Tag), 0, Application.hInstance, 0)
- ' Debug.Print TTParentControl.Name, TTParentControl.Tag, hToolTipHwnd
- '设置tooltip
- With TI
- .lFlags = TTF_SUBCLASS
- .lHwnd = CLng(TTParentControl.Tag)
- .lId = 0
- .hInstance = Application.hInstance
- .lpRect.Top = Point2PixelY(TTParentControl.Top)
- .lpRect.Left = Point2PixelX(TTParentControl.Left)
- .lpRect.Bottom = Point2PixelY(TTParentControl.Height) + .lpRect.Top
- .lpRect.Right = Point2PixelX(TTParentControl.Width) + .lpRect.Left
- ' Debug.Print TypeName(TTParentControl)
- If TypeName(TTParentControl) = "ListBox" Then GetClientRect CLng(TTParentControl.Tag), .lpRect '特殊处理listbox
- End With
- SendMessage hToolTipHwnd, TTM_ADDTOOLA, 0, TI
- '给tooltip加上标题
- SendMessage hToolTipHwnd, TTM_SETTITLE, 0, ByVal TTTitle
- End Function
- Public Property Set ParentControl(ByVal vData As Object) '确定tooltip对象(要求有hwnd的控件)
- Set TTParentControl = vData
- End Property
- Public Property Let ToolTipTitle(ByVal vData As String) '设置tooltip的标题
- TTTitle = vData
- SendMessage hToolTipHwnd, TTM_SETTITLE, 0, ByVal TTTitle
- End Property
- Public Property Let ToolTipText(ByVal vData As String) '设置tooltip的文本(支持多行)
- TI.lpStr = vData
- SendMessage hToolTipHwnd, TTM_UPDATETIPTEXTA, 0, TI
- End Property
- Public Function Destroy() '销毁
- DestroyWindow hToolTipHwnd
- End Function
复制代码
ToolTip.rar
(22.88 KB, 下载次数: 1021)
|
评分
-
5
查看全部评分
-
|