软件下载 | 资讯教程 | 最近更新 | 下载排行 | 一键转帖 | 发布投稿
您的位置:最火下载站 > 电脑教程 > 编程开发 > vb开发 > VB实例:利用API产生控件气泡提示效果

VB实例:利用API产生控件气泡提示效果

先看下效果

在窗体上建立一个TextBox命名为Text1吧

首先声明

Option Explicit
Dim tooltip As New clsTooptip

然后Form_Load代码

Private Sub Form_Load()
    Set tooltip.ParentControl = Text1 '气泡应用于哪个控件(要有Hwnd)
    tooltip.ToolTipTitle = "气泡标题" '气泡标题(不允许换行/字体粗体)
    tooltip.ToolTipText = "气泡内容" & vbCrLf & "123" '气泡内容(允许换行)
    tooltip.Create '创建气泡
End Sub

最后别忘记了将以下代码另存为CS类文件

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, TTParentControl.hwnd, 0, App.hInstance, 0)
    GetClientRect TTParentControl.hwnd, lpRect
    '设置tooltip
    With TI
        .lFlags = TTF_SUBCLASS
        .lHwnd = TTParentControl.hwnd
        .lId = 0
        .hInstance = App.hInstance
        .lpRect = lpRect
    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
    相关阅读
    栏目导航
    推荐软件