gotop 发表于 2007-6-11 21:15:00

请问编辑窗体时,设置controlTtipText 的属性的时候如何给提示的文字分行?

请问编辑窗体时,设置controlTtipText 的属性的时候如何给提示的文字分行?

wylong 发表于 2007-6-13 15:32:00

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 &lt; 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 = "大家好 !!" &amp; vbCrLf &amp; "" &amp; vbCrLf &amp; _<br/>"您目前看到的黄色标签" &amp; vbCrLf &amp; "是一个多行式的提示框"<br/>lbltooltip.Left = Text1.Left + lbltooltip.Width<br/>lbltooltip.Top = Text1.Top + Text1.Height<br/>lbltooltip.Visible = True<br/>End Sub

wylong 发表于 2007-6-13 15:42:00

<p>'如果上面的方法不合意:<br/>'看这段代码:</p><p>Public Const TTS_ALWAYSTIP = &amp;H1<br/>Public Const TTS_NOPREFIX = &amp;H2<br/>Public Const TTS_BALLOON = &amp;H40<br/>Public Const CW_USEDEFAULT = &amp;H80000000<br/>Public Const WS_POPUP = &amp;H80000000<br/>Public Const WM_USER = &amp;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 = &amp;H1<br/>Public Const TTF_CENTERTIP = &amp;H2<br/>Public Const TTF_SUBCLASS = &amp;H10</p><p>Public Type TOOLINFO<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cbSize As Long<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uFlags As Long<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hWnd As Long<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uId As Long<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cRect As RECT<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hinst As Long<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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&nbsp; As Boolean, hTT As Long<br/>Public hCreated() As Long</p><p>Public Sub CreateTTWindow(hParent As Long, Optional bBalloon As Boolean = False)<br/>&nbsp; Dim h&nbsp;&nbsp;&nbsp;&nbsp; As Long, lStyle&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; As Long<br/>&nbsp; lStyle = TTS_NOPREFIX Or TTS_ALWAYSTIP<br/>&nbsp; InitCommonControls<br/>&nbsp; If bBalloon Then lStyle = lStyle Or TTS_BALLOON<br/>&nbsp; hTT = CreateWindowEx(0, "tooltips_class32", 0, lStyle, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hParent, 0, App.hInstance, 0)<br/>&nbsp; If hTT = 0 Then MsgBox "错误!无法建立工具提示窗口!", vbCritical, "错误"<br/>&nbsp; If Not bCreated Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ReDim hCreated(0)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bCreated = True<br/>&nbsp; Else<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ReDim Preserve hCreated(UBound(hCreated) + 1)<br/>&nbsp; End If<br/>&nbsp; hCreated(UBound(hCreated)) = hTT<br/>End Sub</p><p>Public Sub SetToolTip(objTT As Object, sTipText As String, Optional BKColor As Long = &amp;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/>&nbsp;&nbsp;&nbsp; Dim TI&nbsp;&nbsp;&nbsp;&nbsp; As TOOLINFO<br/>&nbsp;&nbsp;&nbsp; With TI<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GetClientRect objTT.hWnd, .cRect<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .hWnd = objTT.hWnd<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .uFlags = TTF_IDISHWND Or TTF_SUBCLASS<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If bCenter Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .uFlags = .uFlags Or TTF_CENTERTIP<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .uId = objTT.hWnd<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .lpszText = sTipText<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .cbSize = Len(TI)<br/>&nbsp;&nbsp;&nbsp; End With<br/>&nbsp;&nbsp;&nbsp; SendMessageLong hTT, TTM_SETMAXTIPWIDTH, 0, MaxWidth<br/>&nbsp;&nbsp;&nbsp; SendMessageLong hTT, TTM_SETDELAYTIME, TTDT_INITIAL, DelayTime<br/>&nbsp;&nbsp;&nbsp; SendMessageLong hTT, TTM_SETDELAYTIME, TTDT_AUTOPOP, VisibleTime<br/>&nbsp;&nbsp;&nbsp; SendMessageLong hTT, TTM_SETTIPTEXTCOLOR, TxtColor, 0&amp;<br/>&nbsp;&nbsp;&nbsp; SendMessageLong hTT, TTM_SETTIPBKCOLOR, BKColor, 0&amp;<br/>&nbsp;&nbsp;&nbsp; SendMessage hTT, TTM_ADDTOOL, 0, TI<br/>End Sub</p><p>Public Sub DestroyTT()<br/>&nbsp; If Not bCreated Then Exit Sub<br/>&nbsp; Dim i&nbsp;&nbsp;&nbsp;&nbsp; As Integer<br/>&nbsp; For i = 0 To UBound(hCreated)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DestroyWindow hCreated(i)<br/>&nbsp; Next<br/>End Sub</p><p>'使用方法: Form_Load里先CreateTT<br/>'然后把需要的控件统统SetTooltip<br/>'在 Form_Unload里DestoryTT<br/>'支持换行!<br/></p>

gotop 发表于 2007-6-13 19:19:00

<p>哈哈~~</p><p>谢谢楼上的兄弟</p><p>文章讲得十分详细</p><p>为什么我没搜索到呢,奇怪</p>
页: [1]
查看完整版本: 请问编辑窗体时,设置controlTtipText 的属性的时候如何给提示的文字分行?