请问编辑窗体时,设置controlTtipText 的属性的时候如何给提示的文字分行?
请问编辑窗体时,设置controlTtipText 的属性的时候如何给提示的文字分行? VB5 以后的 VB 版本都有提供一个属性 -- ToolTipText,目的是让使用者在执行阶段,鼠标在物件上徘徊约一秒时,就将该物件的提示字串显示在该物件下面的一个小长方形中,以协助使用者做输入动作。<br/><br/>有时候说明字串太长了,于是就有人想将提示字串分行显示,而且自然而然的使用 vbNewLine (=vbCrLf 或 =vbCr ) 来换行,因为根据以往的经验,VB都是这样做换行的,可是这一次很多人都踢到铁板了!<br/><br/>VB 用来显示 ToolTipText 的提示框,其实是一个文字框,而且 MultiLine 属性并没有设为 True,您可以自己用一个单行式的文字框来做测试,就算您用 vbCrLf 来换行也不会有作用的!<br/><br/>既然 VB 提供的 Default 功能不能满足我们的需求,而我们又想提供使用者多行式的提示框,那要怎么办呢?其实也不难,我们自己动手 DIY 一下就有了,而且程序码也不长!<br/><br/><br/>'首先在 Form 上放一个 Timer (如果需要的话),以便于叫出突现式说明框<br/><br/>Private Function TimeOut(pInterval As Single)<br/>Dim sngTimer As Single<br/>sngTimer = Timer<br/>Do While Timer < sngTimer + pInterval<br/>DoEvents<br/>Loop<br/>End Function<br/><br/>'然后在 Form 上放一个 Label,取名为 lblToolTip,在 MouseMove 中加入以下程序:<br/><br/>Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)<br/>lbltooltip.Visible = False<br/>End Sub<br/><br/>'在您想显示说明框的物件加入以下程序码: ( Textbox, listbox etc. )<br/><br/>Private Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)<br/>TimeOut 0.3 '鼠标移到物件上多久后,要显示提示框<br/>lbltooltip.Caption = "大家好 !!" & vbCrLf & "" & vbCrLf & _<br/>"您目前看到的黄色标签" & vbCrLf & "是一个多行式的提示框"<br/>lbltooltip.Left = Text1.Left + lbltooltip.Width<br/>lbltooltip.Top = Text1.Top + Text1.Height<br/>lbltooltip.Visible = True<br/>End Sub <p>'如果上面的方法不合意:<br/>'看这段代码:</p><p>Public Const TTS_ALWAYSTIP = &H1<br/>Public Const TTS_NOPREFIX = &H2<br/>Public Const TTS_BALLOON = &H40<br/>Public Const CW_USEDEFAULT = &H80000000<br/>Public Const WS_POPUP = &H80000000<br/>Public Const WM_USER = &H400<br/>' 提示的消息<br/>Public Const TTM_SETDELAYTIME = (WM_USER + 3)<br/>Public Const TTM_ADDTOOL = (WM_USER + 4)<br/>Public Const TTM_SETTIPBKCOLOR = (WM_USER + 19)<br/>Public Const TTM_SETTIPTEXTCOLOR = (WM_USER + 20)<br/>Public Const TTM_GETTIPBKCOLOR = (WM_USER + 22)<br/>Public Const TTM_GETTIPTEXTCOLOR = (WM_USER + 23)<br/>Public Const TTM_SETMAXTIPWIDTH = (WM_USER + 24)</p><p>Public Const TTDT_AUTOMATIC = 0<br/>Public Const TTDT_RESHOW = 1<br/>Public Const TTDT_AUTOPOP = 2<br/>Public Const TTDT_INITIAL = 3</p><p>Public Const TTF_IDISHWND = &H1<br/>Public Const TTF_CENTERTIP = &H2<br/>Public Const TTF_SUBCLASS = &H10</p><p>Public Type TOOLINFO<br/> cbSize As Long<br/> uFlags As Long<br/> hWnd As Long<br/> uId As Long<br/> cRect As RECT<br/> hinst As Long<br/> lpszText As String<br/>End Type</p><p>Public 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<br/>Public Declare Function DestroyWindow Lib "user32" (ByVal hWnd As Long) As Long<br/>Public 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</p><p>Declare Sub InitCommonControls Lib "comctl32.dll" ()</p><p>Public bCreated As Boolean, hTT As Long<br/>Public hCreated() As Long</p><p>Public Sub CreateTTWindow(hParent As Long, Optional bBalloon As Boolean = False)<br/> Dim h As Long, lStyle As Long<br/> lStyle = TTS_NOPREFIX Or TTS_ALWAYSTIP<br/> InitCommonControls<br/> If bBalloon Then lStyle = lStyle Or TTS_BALLOON<br/> hTT = CreateWindowEx(0, "tooltips_class32", 0, lStyle, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hParent, 0, App.hInstance, 0)<br/> If hTT = 0 Then MsgBox "错误!无法建立工具提示窗口!", vbCritical, "错误"<br/> If Not bCreated Then<br/> ReDim hCreated(0)<br/> bCreated = True<br/> Else<br/> ReDim Preserve hCreated(UBound(hCreated) + 1)<br/> End If<br/> hCreated(UBound(hCreated)) = hTT<br/>End Sub</p><p>Public Sub SetToolTip(objTT As Object, sTipText As String, Optional BKColor As Long = &HEEFFFF, Optional TxtColor As Long = vbBlack, Optional MaxWidth As Long = 300, Optional DelayTime As Long = 500, Optional VisibleTime As Long = 2000, Optional bCenter As Boolean = False)<br/> Dim TI As TOOLINFO<br/> With TI<br/> GetClientRect objTT.hWnd, .cRect<br/> .hWnd = objTT.hWnd<br/> .uFlags = TTF_IDISHWND Or TTF_SUBCLASS<br/> If bCenter Then<br/> .uFlags = .uFlags Or TTF_CENTERTIP<br/> End If<br/> .uId = objTT.hWnd<br/> .lpszText = sTipText<br/> .cbSize = Len(TI)<br/> End With<br/> SendMessageLong hTT, TTM_SETMAXTIPWIDTH, 0, MaxWidth<br/> SendMessageLong hTT, TTM_SETDELAYTIME, TTDT_INITIAL, DelayTime<br/> SendMessageLong hTT, TTM_SETDELAYTIME, TTDT_AUTOPOP, VisibleTime<br/> SendMessageLong hTT, TTM_SETTIPTEXTCOLOR, TxtColor, 0&<br/> SendMessageLong hTT, TTM_SETTIPBKCOLOR, BKColor, 0&<br/> SendMessage hTT, TTM_ADDTOOL, 0, TI<br/>End Sub</p><p>Public Sub DestroyTT()<br/> If Not bCreated Then Exit Sub<br/> Dim i As Integer<br/> For i = 0 To UBound(hCreated)<br/> DestroyWindow hCreated(i)<br/> Next<br/>End Sub</p><p>'使用方法: Form_Load里先CreateTT<br/>'然后把需要的控件统统SetTooltip<br/>'在 Form_Unload里DestoryTT<br/>'支持换行!<br/></p> <p>哈哈~~</p><p>谢谢楼上的兄弟</p><p>文章讲得十分详细</p><p>为什么我没搜索到呢,奇怪</p>
页:
[1]