关于汽泡式ToolTiptext的问题已困扰我好几天,请大家帮忙!!!!!!!!

cmt123 2005-05-13 07:51:31
我有一段原码,可以实现汽泡式ToolTiptext,字体颜色、背景等都可以实现,但字体大小、字型等不能实现!请大家帮忙!!我的代码如下:

类模块:
Option Explicit

Private Const ICC_BAR_CLASSES = &H4 'toolbar, statusbar, trackbar, tooltips

Private Type FONTSIGNATURE
fsUsb(4) As Long
fsCsb(2) As Long
End Type
Private Type CHARSETINFO
ciCharset As Long
ciACP As Long
fs As FONTSIGNATURE
End Type

Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
Private Declare Function MulDiv Lib "kernel32" (ByVal nNumber As Long, ByVal nNumerator As Long, ByVal nDenominator As Long) As Long
Private Declare Function GetLocaleInfo Lib "kernel32" Alias "GetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String, ByVal cchData As Long) As Long
Private Declare Function TranslateCharsetInfo Lib "gdi32" (ByVal lpSrc As Long, lpcs As CHARSETINFO, ByVal dwFlags As Long) As Long
Private Declare Function GetSystemDefaultLCID Lib "kernel32" () As Long
Private Declare Function CreateFont Lib "gdi32" Alias "CreateFontA" _
(ByVal H As Long, _
ByVal W As Long, _
ByVal E As Long, _
ByVal O As Long, _
ByVal W As Long, _
ByVal i As Long, _
ByVal u As Long, _
ByVal S As Long, _
ByVal C As Long, _
ByVal OP As Long, _
ByVal CP As Long, _
ByVal Q As Long, _
ByVal PAF As Long, _
ByVal F As String) _
As Long

Private hTT As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Const WM_SETFONT = &H30
Private Const LOGPIXELSY = 90
Private Const LOCALE_USER_DEFAULT = &H400
Private Const LOCALE_IDEFAULTANSICODEPAGE = &H1004 'default ansi code page
Private Const TCI_SRCCODEPAGE = 2
Private Const TCI_SRCCHARSET = 1
Private Const OUT_DEFAULT_PRECIS = 0
Private Const CLIP_DEFAULT_PRECIS = 0
Private Const DEFAULT_QUALITY = 0
Private Const DEFAULT_PITCH = 0
Private Const DEFAULT_CHARSET = 1

Private Declare Sub InitCommonControls Lib "comctl32.dll" ()
''Windows API Functions
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 SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long

''Windows API Constants
Private Const WM_USER = &H400
Private Const CW_USEDEFAULT = &H80000000

''Windows API Types
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

''Tooltip Window Constants
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_SETTIPBKCOLOR = (WM_USER + 19)

Private Const TTM_SETTIPBKSize = (WM_USER + 100)

Private Const TTM_SETTIPTEXTCOLOR = (WM_USER + 20)
Private Const TTM_SETTITLE = (WM_USER + 32)
Private Const TTS_BALLOON = &H40
Private Const TTS_ALWAYSTIP = &H1
Private Const TTF_SUBCLASS = &H10
Private Const TTF_IDISHWND = &H1
Private Const TTM_SETDELAYTIME = (WM_USER + 3)
Private Const TTDT_AUTOPOP = 2
Private Const TTDT_INITIAL = 3

Private Const TOOLTIPS_CLASSA = "tooltips_class32"

''Tooltip Window Types
Private Type TOOLINFO
lSize As Long
lFlags As Long
hwnd As Long
lId As Long
lpRect As RECT
hInstance As Long
lpStr As String
lParam As Long
End Type


Public Enum ttIconType
TTNoIcon = 0
TTIconInfo = 1
TTIconWarning = 2
TTIconError = 3
End Enum

Public Enum ttStyleEnum
TTStandard
TTBalloon
End Enum

'local variable(s) to hold property value(s)
Private mvarBackColor As Long
Private mvarTitle As String
Private mvarForeColor As Long
Private mvarForeSize As Long
Private mvarIcon As ttIconType
Private mvarCentered As Boolean
Private mvarStyle As ttStyleEnum
Private mvarTipText As String
Private mvarVisibleTime As Long
Private mvarDelayTime As Long
Private m_hFont As Long 'local cop
Private m_PFont As Long
Private m_reture As Long
Private Const GWL_WNDPROC = (-4)
'private data
Private m_lTTHwnd As Long ' hwnd of the tooltip
Private m_lParentHwnd As Long ' hwnd of the window the tooltip attached to
Private ti As TOOLINFO

Public Property Let Style(ByVal vData As ttStyleEnum)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.Style = 5
mvarStyle = vData
End Property

Public Property Get Style() As ttStyleEnum
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.Style
Style = mvarStyle
End Property

Public Property Let Centered(ByVal vData As Boolean)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.Centered = 5
mvarCentered = vData
End Property

Public Property Get Centered() As Boolean
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.Centered
Centered = mvarCentered
End Property

Public Function Create(ByVal ParentHwnd As Long) As Boolean
Dim lWinStyle As Long
If m_lTTHwnd <> 0 Then
DestroyWindow m_lTTHwnd
End If
m_lParentHwnd = ParentHwnd
lWinStyle = TTS_ALWAYSTIP Or TTS_NOPREFIX
''create baloon style if desired
If mvarStyle = TTBalloon Then lWinStyle = lWinStyle Or TTS_BALLOON
m_lTTHwnd = CreateWindowEx(0&, _
TOOLTIPS_CLASSA, _
vbNullString, _
lWinStyle, _
CW_USEDEFAULT, _
CW_USEDEFAULT, _
CW_USEDEFAULT, _
CW_USEDEFAULT, _
0&, _
0&, _
App.hInstance, _
0&)
''now set our tooltip info structure
With ti
''if we want it centered, then set that flag
If mvarCentered Then
.lFlags = TTF_SUBCLASS Or TTF_CENTERTIP Or TTF_IDISHWND
Else
.lFlags = TTF_SUBCLASS Or TTF_IDISHWND
End If

''set the hwnd prop to our parent control's hwnd
.hwnd = m_lParentHwnd
.lId = m_lParentHwnd '0
.hInstance = App.hInstance
'.lpstr = ALREADY SET
'.lpRect = lpRect
.lSize = Len(ti)
End With

''add the tooltip structure
SendMessage m_lTTHwnd, TTM_ADDTOOLA, 0&, ti

''if we want a title or we want an icon
If mvarTitle <> vbNullString Or mvarIcon <> TTNoIcon Then
SendMessage m_lTTHwnd, TTM_SETTITLE, CLng(mvarIcon), ByVal mvarTitle
End If

If mvarForeColor <> Empty Then
SendMessage m_lTTHwnd, TTM_SETTIPTEXTCOLOR, mvarForeColor, 0&
End If

If mvarBackColor <> Empty Then
SendMessage m_lTTHwnd, TTM_SETTIPBKCOLOR, mvarBackColor, 0&
End If

If mvarForeSize <> Empty Then
SendMessage m_lTTHwnd, TTM_SETTITLE + 27, mvarForeSize, 0&
End If

SendMessageLong m_lTTHwnd, TTM_SETDELAYTIME, TTDT_AUTOPOP, mvarVisibleTime
SendMessageLong m_lTTHwnd, TTM_SETDELAYTIME, TTDT_INITIAL, mvarDelayTime
' m_reture = SendMessageLong(hTT, WM_SETFONT, m_PFont, 1&)
' ReturnVal = SendMessageLong(hTT, WM_SETFONT, hFont, 1&)
End Function

...全文
150 点赞 收藏 6
写回复
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
balloonman2002 2005-05-16
我后来查了一下资料,wm_setfont可以更改tooltip的字体,这在你代码中已经有了
回复
cmt123 2005-05-14
To mndsoft(枕善居(http://www.mndsoft.com/)
这些程序都没有没有对汽泡式ToolTiptext的字体大小、字型设置!
回复
mndsoft 2005-05-13
以下下载到枕善居:
http://www.mndsoft.com

在Listview中如果内容太长,需要调整列宽才能看完整信息。现在用这个气泡提示信息,就不要调整列宽了。
即时在气泡中显示内容。
Listview 多行气泡提示:
界面:
http://www.mndsoft.com/softimg/ListviewTooltip.png

基于MSFlexgrid控件的气泡提示:
http://www.mndsoft.com/softimg/MSFlexGridBalloonTip.png
回复
MmMVP 2005-05-13
http://www.aivisoft.net/fp/FPSource.rar
这个90m的大代码里有tooltip类
回复
balloonman2002 2005-05-13
不知道CREATEFONT+GETDC+SelectObject行不行
回复
cmt123 2005-05-13
继续类模块代码:
Public Property Let Icon(ByVal vData As ttIconType)
mvarIcon = vData
If m_lTTHwnd <> 0 And mvarTitle <> Empty And mvarIcon <> TTNoIcon Then
SendMessage m_lTTHwnd, TTM_SETTITLE, CLng(mvarIcon), ByVal mvarTitle
End If
End Property

Public Property Get Icon() As ttIconType
Icon = mvarIcon
End Property

Public Property Let ForeColor(ByVal vData As Long)
mvarForeColor = vData
If m_lTTHwnd <> 0 Then
SendMessage m_lTTHwnd, TTM_SETTIPTEXTCOLOR, mvarForeColor, 0&
End If
End Property

Public Property Get ForeColor() As Long
ForeColor = mvarForeColor
End Property

Public Property Let Title(ByVal vData As String)
mvarTitle = vData
If m_lTTHwnd <> 0 And mvarTitle <> Empty And mvarIcon <> TTNoIcon Then
SendMessage m_lTTHwnd, TTM_SETTITLE, CLng(mvarIcon), ByVal mvarTitle
End If
End Property

Public Property Get Title() As String
Title = ti.lpStr
End Property

Public Property Let BackColor(ByVal vData As Long)
mvarBackColor = vData
If m_lTTHwnd <> 0 Then
SendMessage m_lTTHwnd, TTM_SETTIPBKCOLOR, mvarBackColor, 0&
End If
End Property

Public Property Get BackColor() As Long
BackColor = mvarBackColor
End Property

Public Property Let TipText(ByVal vData As String)
mvarTipText = vData
ti.lpStr = vData
If m_lTTHwnd <> 0 Then
SendMessage m_lTTHwnd, TTM_UPDATETIPTEXTA, 0&, ti
End If
End Property

Public Property Get TipText() As String
TipText = mvarTipText
End Property

Private Sub Class_Initialize()
InitCommonControls
mvarDelayTime = 100
mvarVisibleTime = 1000000
End Sub

Private Sub Class_Terminate()
Destroy
End Sub

Public Sub Destroy()
If m_lTTHwnd <> 0 Then
DestroyWindow m_lTTHwnd
End If
End Sub

Public Property Get VisibleTime() As Long
VisibleTime = mvarVisibleTime
End Property

Public Property Let VisibleTime(ByVal lData As Long)
mvarVisibleTime = lData
End Property

Public Property Get DelayTime() As Long
DelayTime = mvarDelayTime
End Property

Public Property Let DelayTime(ByVal lData As Long)
mvarDelayTime = lData
End Property

Public Property Set Font(objFont As StdFont)
Dim hFont As Long
Dim lHeight As Long
Dim lItalic As Long
Dim lBold As Long
Dim lUnderline As Long
Dim lStrikeOut As Long
Dim ReturnVal As Long
Dim WndRect As RECT
Dim sCodePage As String * 6
Dim lCodePage As Long
Dim cSetInfo As CHARSETINFO
Dim lRetVal As Long
Dim hOldFont As Long
Dim lPixelsY As Long

If objFont Is Nothing Then Exit Property

lHeight = -MulDiv(objFont.Size, GetDeviceCaps(GetDC(hTT), LOGPIXELSY), 72)

If objFont.Italic = True Then
lItalic = 1
Else
lItalic = 0
End If

If objFont.Bold = True Then
lBold = 1
Else
lBold = 0
End If

If objFont.Underline = True Then
lUnderline = 1
Else
lUnderline = 0
End If

If objFont.Strikethrough = True Then
lStrikeOut = 1
Else
lStrikeOut = 0
End If

' Determine the character set for the current locale
GetLocaleInfo GetSystemDefaultLCID(), LOCALE_IDEFAULTANSICODEPAGE, sCodePage, 6
sCodePage = Left$(sCodePage, InStr(sCodePage, Chr$(0)) - 1)
lCodePage = CLng(sCodePage)
lRetVal = TranslateCharsetInfo(lCodePage, cSetInfo, TCI_SRCCODEPAGE)
hFont = CreateFont(lHeight, _
0, _
0, _
0, _
objFont.Weight, _
lItalic, _
lUnderline, _
lStrikeOut, _
cSetInfo.ciCharset, _
OUT_DEFAULT_PRECIS, _
CLIP_DEFAULT_PRECIS, _
DEFAULT_QUALITY, _
DEFAULT_PITCH, _
objFont.Name)
m_PFont = hFont
ReturnVal = SendMessageLong(hTT, WM_SETFONT, m_PFont, 1&)
m_reture = ReturnVal
If m_hFont <> 0 Then
DeleteObject m_hFont
End If

m_hFont = hFont

End Property

在Form窗体:
Option Explicit
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

Const LVM_FIRST = &H1000&
Const LVM_HITTEST = LVM_FIRST + 18

Private Type POINTAPI
X As Long
Y As Long
End Type

Private Type LVHITTESTINFO
pt As POINTAPI
flags As Long
iItem As Long
iSubItem As Long
End Type

Dim TT As CTooltip
Dim m_lCurItemIndex As Long

Private Sub Form_Load()
Dim comlong As Long
With ListView1.ListItems
.Add Text:="Test item #1"
.Add Text:="Test item #2"
.Add Text:="Long long long test item #3"
End With

Set TT = New CTooltip
Dim myfont As New StdFont
myfont.Name = "fdsf"
myfont.Italic = True
myfont.Size = 15
TT.Style = TTBalloon
TT.BackColor = &H800000
TT.ForeColor = &HFFFFFF
TT.Icon = TTIconWarning
Set TT.Font = myfont
Text1.Text = myfont.Size
'TT.Foresize = 20
TT.Title = "Multiline tooltip"
TT.TipText = "我爱VB啊!" & vbCrLf & "你为何爱VB啊?" & Chr(13) & "因为VB能够给我快乐!" & Chr(13) & "你觉得如何?" & Chr(13) & "我也有同感,让我们一起怒力,共同进步!"
TT.Create Command1.hwnd
End Sub

Private Sub ListView1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim lvhti As LVHITTESTINFO
Dim lItemIndex As Long

lvhti.pt.X = X / Screen.TwipsPerPixelX
lvhti.pt.Y = Y / Screen.TwipsPerPixelY

lItemIndex = SendMessage(ListView1.hwnd, LVM_HITTEST, 0, lvhti) + 1

If m_lCurItemIndex <> lItemIndex Then
m_lCurItemIndex = lItemIndex
If m_lCurItemIndex = 0 Then ' no item under the mouse pointer
TT.Destroy
Else
TT.Title = "Multiline tooltip"
TT.TipText = "我爱VB啊!" & vbCrLf & "你为何爱VB啊?" & Chr(13) & "因为VB能够给我快乐!" & Chr(13) & "你觉得如何?" & Chr(13) & "我也有同感,让我们一起怒力,共同进步!"
TT.Create ListView1.hwnd
End If
End If

End Sub
回复
发动态
发帖子
VB基础类
创建于2007-09-28

7451

社区成员

VB 基础类
申请成为版主
社区公告
暂无公告