Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

EZ (Easy) ToolTips !

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • EZ (Easy) ToolTips !

    In response to another discussion about using ToolTips, I have
    decided to post here a very simply library for creating tooltips
    in your applications. The code is Copyrighted (by me), but may be
    used Royalty free in any application (even commercial). All I ask
    is that you give me credit for the code by maintaining the copyright
    in the source code.

    I will add two mores posts here.

    The first is the Library as an include file.
    Name is "ezttip.inc"

    The second post will be a simple DDT app that uses the library.




    ------------------
    Chris Boss
    Computer Workshop
    Developer of "EZGUI"
    http://ezgui.com
    Chris Boss
    Computer Workshop
    Developer of "EZGUI"
    http://cwsof.com
    http://twitter.com/EZGUIProGuy

  • #2
    ezttip.inc:

    Code:
    ' Note:   You define the following function in your main apps code:
    DECLARE FUNCTION DefineToolTipText(BYVAL hDlg&, BYVAL IDNum&) AS STRING
    
    ' *************************************************************
    '           EZGUI Freeware ToolTip Library !!!
    ' -------------------------------------------------------------
    '   Copyright 2005, Christopher R. Boss, All Rights Reserved !
    '   This source code is offered as freeware. You may use it
    '   Royalty free, but you must maintain this copyright notice
    '   with the code. No warranty made !
    '   Provided by Computer Workshop.
    '   [url="http://ezgui.com"]http://ezgui.com[/url]    , also  [url="http://cwsof.com"]http://cwsof.com[/url] 
    ' *************************************************************
    GLOBAL g_ToolTipText AS ASCIIZ*1024
    '
    FUNCTION CreateDlgToolTip(BYVAL hDlg&, BYVAL MaxWidth&, BYVAL TextColor&, BYVAL BGColor&) AS LONG
        LOCAL WS&, EWS&, CName AS ASCIIZ*32, hCtrl&
        WS&=%WS_POPUP OR %TTS_ALWAYSTIP OR %TTS_BALLOON
        EWS&=%WS_EX_TOOLWINDOW
        CName=$TOOLTIPS_CLASS
        hCtrl&=CreateWindowEx(EWS&, CName,"", WS&,0,0,32,32,hDlg&,0,GetModuleHandle(BYVAL %NULL), BYVAL %NULL)
        SendMessage hCtrl&, %TTM_SETMAXTIPWIDTH, 0, MaxWidth&
        SendMessage hCtrl&, %TTM_SETTIPTEXTCOLOR,TextColor&,0
        SendMessage hCtrl&, %TTM_SETTIPBKCOLOR, BGColor&,0
        FUNCTION=hCtrl&
    END FUNCTION
    '
    SUB AddTool(BYVAL hToolTip&, BYVAL hDlg&, BYVAL IDNum&)
        LOCAL TT AS TOOLINFO, hCtrl&, RV&
        CONTROL HANDLE hDlg&, IDNum& TO hCtrl&
        IF hCtrl&<>0 THEN
            TT.cbSize=SIZEOF(TT)
            TT.uFlags=%TTF_IDISHWND OR %TTF_SUBCLASS
            TT.hwnd=hDlg&
            TT.uId=hCtrl&
            ' TT.rec=
            TT.hinst=GetModuleHandle(BYVAL %NULL)
            TT.lpszText=%LPSTR_TEXTCALLBACK
            TT.lParam=0
            RV&=SendMessage(hToolTip&, %TTM_ADDTOOL,0, VARPTR(TT))
        END IF
    END SUB
    '
    FUNCTION GetClassType(BYVAL hCtrl&) AS STRING
        LOCAL zC AS ASCIIZ*33, X&
        X&=GetClassName(hCtrl&, zC,32)
        FUNCTION=zC
    END FUNCTION
    '
    FUNCTION MKCRLF(BYVAL T$) AS STRING
        REPLACE "|" WITH CHR$(13)+CHR$(10) IN T$
        FUNCTION=T$
    END FUNCTION
    '
    FUNCTION TestForToolTip(BYVAL hDlg&, BYVAL Msg&, BYVAL wParam&, BYVAL lParam&) AS LONG
        LOCAL RV&, pNM AS NMHDR PTR, pTT AS NMTTDISPINFO PTR
        LOCAL hCtrl&, hTipTool&, IDNum&
        RV&=0
        IF Msg&=%WM_NOTIFY THEN
            pNM=lParam&
            hCtrl&[email protected]
            hTipTool&[email protected]
            IF GetClassType(hCtrl&)=$TOOLTIPS_CLASS THEN
                IF @pNM.code=%TTN_NEEDTEXT THEN
                    pTT=lParam&
                    IDNum&=GetDlgCtrlID(hTipTool&)
                    ' this method is limited to 80 characters
                    ' @pTT.szText=left$(DefineToolTipText(hDlg&, IDNum&),79)
                    ' this method is limited to size of my global variable !
                    g_ToolTipText=MKCRLF(DefineToolTipText(hDlg&, IDNum&))
                    @pTT.lpszText=VARPTR(g_ToolTipText)
                    RV&=1
                END IF
            END IF
        END IF
    END FUNCTION
    
    ' *************************************************************
    '   End ToolTip Library
    ' *************************************************************
    '


    ------------------
    Chris Boss
    Computer Workshop
    Developer of "EZGUI"
    http://ezgui.com
    Chris Boss
    Computer Workshop
    Developer of "EZGUI"
    http://cwsof.com
    http://twitter.com/EZGUIProGuy

    Comment


    • #3
      Sample DDT app that uses library:

      Code:
      ' ***************************************************************
      '   This code can be used Royalty Free and Freely Distributed !
      ' ***************************************************************
      
      #COMPILE EXE
      #REGISTER NONE
      #DIM ALL          '  This is helpful to prevent errors in coding
      
      
      #INCLUDE "win32api.inc"   ' Must come first before other include files !
      #INCLUDE "commctrl.inc"
      ' ----------------------------------------------------------------------
      #INCLUDE "ezttip.inc"
      ' ----------------------------------------------------------------------
      
      ' *************************************************************
      '                  Constants and Declares (#1)
      ' *************************************************************
      %FORM1_BUTTON1            = 100
      %FORM1_BUTTON2            = 105
      %FORM1_BUTTON3            = 110
      ' --------------------------------------------------
      DECLARE SUB ShowDialog_Form1(BYVAL hParent&)
      DECLARE CALLBACK FUNCTION Form1_DLGPROC
      ' --------------------------------------------------
      ' ------------------------------------------------
      
      DECLARE CALLBACK FUNCTION CBF_FORM1_BUTTON1()
      DECLARE CALLBACK FUNCTION CBF_FORM1_BUTTON2()
      DECLARE CALLBACK FUNCTION CBF_FORM1_BUTTON3()
      DECLARE SUB InitCCtrls()
      
      ' *************************************************************
      '               Application Globals Variables (#2)
      ' *************************************************************
      '
      GLOBAL hForm1&              ' Dialog handle
      GLOBAL hForm1_ToolTip&      ' ToolTip for Dialog
      '
      ' *************************************************************
      '                    Application Entrance
      ' *************************************************************
      '
      FUNCTION PBMAIN
          LOCAL Count&
          ' --------------
          InitCCtrls
          ' --------------
          ShowDialog_Form1 0
          DO
              DIALOG DOEVENTS TO Count&
          LOOP UNTIL Count&=0
      END FUNCTION
      '
      ' *************************************************************
      '                    Application Dialogs (#3)
      ' *************************************************************
      '
      SUB InitCCtrls()
          LOCAL CCF AS INIT_COMMON_CONTROLSEX
          CCF.dwSize = SIZEOF(CCF)
          CCF.dwICC  = %ICC_WIN95_CLASSES
          InitCommonControlsEx CCF
      END SUB
      '
      ' This function is required by the ezttip.inc file for setting the
      ' tooltip text. The text can be changed dynamically !
      FUNCTION DefineToolTipText(BYVAL hDlg&, BYVAL IDNum&) AS STRING
          LOCAL T$
          IF hDlg&=hForm1& THEN
              SELECT CASE IDNum&
                  CASE %FORM1_BUTTON1
                      T$="Button 1:&#0124; &#0124;My First ToolTip Text !|"
                  CASE %FORM1_BUTTON2
                      T$="Button 2:&#0124; &#0124;Another ToolTip Text !!|"
                  CASE %FORM1_BUTTON3
                      T$="Button 3:&#0124; &#0124;Last ToolTip Text !!!|"
                  CASE ELSE
              END SELECT
          END IF
          FUNCTION=T$
      END FUNCTION
      
      SUB ShowDialog_Form1(BYVAL hParent&)
          LOCAL Style&, ExStyle&
          Style& = %WS_POPUP OR %DS_MODALFRAME OR %WS_CAPTION OR %WS_MINIMIZEBOX OR %WS_SYSMENU OR %DS_CENTER
          ExStyle& = 0
          DIALOG NEW hParent&, "ToolTips", 0, 0,  133,  98, Style&, ExStyle& TO hForm1&
          CONTROL ADD "Button", hForm1&,  %FORM1_BUTTON1,  "Button  1", 19, 20, 96, 15, _
              %WS_CHILD OR %WS_VISIBLE OR %BS_PUSHBUTTON OR %WS_TABSTOP CALL CBF_FORM1_BUTTON1
          CONTROL ADD "Button", hForm1&,  %FORM1_BUTTON2,  "Button  2", 19, 44, 96, 15, _
              %WS_CHILD OR %WS_VISIBLE OR %BS_PUSHBUTTON OR %WS_TABSTOP CALL CBF_FORM1_BUTTON2
          CONTROL ADD "Button", hForm1&,  %FORM1_BUTTON3,  "Button  3", 19, 69, 96, 15, _
              %WS_CHILD OR %WS_VISIBLE OR %BS_PUSHBUTTON OR %WS_TABSTOP CALL CBF_FORM1_BUTTON3
          ' -----------------------------------------
          hForm1_ToolTip&=CreateDlgToolTip(hForm1&, 150, RGB(0,0,0), RGB(255,200,255))
          AddTool hForm1_ToolTip&, hForm1&,%FORM1_BUTTON1
          AddTool hForm1_ToolTip&, hForm1&,%FORM1_BUTTON2
          AddTool hForm1_ToolTip&, hForm1&,%FORM1_BUTTON3
          ' -----------------------------------------
          DIALOG SHOW MODELESS hForm1& , CALL Form1_DLGPROC
      END SUB
      '
      CALLBACK FUNCTION Form1_DLGPROC
          SELECT CASE CBMSG
              CASE %WM_NOTIFY
                  IF TestForToolTip(CBHNDL, CBMSG, CBWPARAM, CBLPARAM) THEN
                      FUNCTION=0
                      EXIT FUNCTION
                  END IF
              CASE ELSE
          END SELECT
      END FUNCTION
      '
      CALLBACK FUNCTION CBF_FORM1_BUTTON1
          IF CBCTLMSG=%BN_CLICKED THEN
      
          END IF
      END FUNCTION
      '
      CALLBACK FUNCTION CBF_FORM1_BUTTON2
          IF CBCTLMSG=%BN_CLICKED THEN
      
          END IF
      END FUNCTION
      '
      CALLBACK FUNCTION CBF_FORM1_BUTTON3
          IF CBCTLMSG=%BN_CLICKED THEN
      
          END IF
      END FUNCTION


      ------------------
      Chris Boss
      Computer Workshop
      Developer of "EZGUI"
      http://ezgui.com
      Chris Boss
      Computer Workshop
      Developer of "EZGUI"
      http://cwsof.com
      http://twitter.com/EZGUIProGuy

      Comment


      • #4
        Note:

        This library allows you to use one tooltip control per dialog
        for all the controls that need a tooltip.

        You can not only define the text for the tooltip dynamically,
        you can define text up to 1024 characters (you can change the
        global variable to increase this limit).

        You can also define the maximum width of the tooltip (in pixels)
        and also the text and BG colors of the tooltip.

        The text will wordwrap within the maximum width limit and you
        can add CRLF's (multiple lines) by simply putting the | character
        in the text to indicate a new line.


        ------------------
        Chris Boss
        Computer Workshop
        Developer of "EZGUI"
        http://ezgui.com
        Chris Boss
        Computer Workshop
        Developer of "EZGUI"
        http://cwsof.com
        http://twitter.com/EZGUIProGuy

        Comment


        • #5
          AlphaBlending ToolTip display

          Here is another example, which uses the SetLayeredWindowAttributes function to alphablend the tooltip so the background will show through.

          If you set the transparent color to the tooltip BG color, you can make it show through completely, except for the text.

          Use the tooltip include file noted above.

          Code:
          ' ***************************************************************
          '   This code can be used Royalty Free and Freely Distributed !
          ' ***************************************************************
          #COMPILE EXE
          #REGISTER NONE
          #DIM ALL          '  This is helpful to prevent errors in coding
          
          #INCLUDE "win32api.inc"   ' Must come first before other include files !
          #INCLUDE "commctrl.inc"
          ' ----------------------------------------------------------------------
          #INCLUDE "ezttip.inc"
          ' ----------------------------------------------------------------------
          ' *************************************************************
          '                  Constants and Declares (#1)
          ' *************************************************************
          %FORM1_BUTTON1            = 100
          %FORM1_BUTTON2            = 105
          %FORM1_BUTTON3            = 110
          ' --------------------------------------------------
          DECLARE SUB ShowDialog_Form1(BYVAL hParent&)
          DECLARE CALLBACK FUNCTION Form1_DLGPROC
          ' --------------------------------------------------
          ' ------------------------------------------------
          DECLARE CALLBACK FUNCTION CBF_FORM1_BUTTON1()
          DECLARE CALLBACK FUNCTION CBF_FORM1_BUTTON2()
          DECLARE CALLBACK FUNCTION CBF_FORM1_BUTTON3()
          DECLARE SUB InitCCtrls()
          ' *************************************************************
          '               Application Globals Variables (#2)
          ' *************************************************************
          '
          GLOBAL hForm1&              ' Dialog handle
          GLOBAL hForm1_ToolTip&      ' ToolTip for Dialog
          '
          ' *************************************************************
          '                    Application Entrance
          ' *************************************************************
          '
          FUNCTION PBMAIN
              LOCAL Count&
              ' --------------
              InitCCtrls
              ' --------------
              ShowDialog_Form1 0
              DO
                  DIALOG DOEVENTS TO Count&
              LOOP UNTIL Count&=0
          END FUNCTION
          '
          ' *************************************************************
          '                    Application Dialogs (#3)
          ' *************************************************************
          '
          SUB InitCCtrls()
              LOCAL CCF AS INIT_COMMON_CONTROLSEX
              CCF.dwSize = SIZEOF(CCF)
              CCF.dwICC  = %ICC_WIN95_CLASSES
              InitCommonControlsEx CCF
          END SUB
          '
          ' This function is required by the ezttip.inc file for setting the
          ' tooltip text. The text can be changed dynamically !
          FUNCTION DefineToolTipText(BYVAL hDlg&, BYVAL IDNum&) AS STRING
              LOCAL T$
              IF hDlg&=hForm1& THEN
                  SELECT CASE IDNum&
                      CASE %FORM1_BUTTON1
                          T$="Button 1:| |My First ToolTip Text !|"
                      CASE %FORM1_BUTTON2
                          T$="Button 2:| |Another ToolTip Text !!|"
                      CASE %FORM1_BUTTON3
                          T$="Button 3:| |Last ToolTip Text !!!|"
                      CASE ELSE
                  END SELECT
              END IF
              FUNCTION=T$
          END FUNCTION
          SUB ShowDialog_Form1(BYVAL hParent&)
              LOCAL Style&, ExStyle&
              Style& = %WS_POPUP OR %DS_MODALFRAME OR %WS_CAPTION OR %WS_MINIMIZEBOX OR %WS_SYSMENU OR %DS_CENTER
              ExStyle& = 0
              DIALOG NEW hParent&, "ToolTips", 0, 0,  133,  98, Style&, ExStyle& TO hForm1&
              CONTROL ADD "Button", hForm1&,  %FORM1_BUTTON1,  "Button  1", 19, 20, 96, 15, _
                  %WS_CHILD OR %WS_VISIBLE OR %BS_PUSHBUTTON OR %WS_TABSTOP CALL CBF_FORM1_BUTTON1
              CONTROL ADD "Button", hForm1&,  %FORM1_BUTTON2,  "Button  2", 19, 44, 96, 15, _
                  %WS_CHILD OR %WS_VISIBLE OR %BS_PUSHBUTTON OR %WS_TABSTOP CALL CBF_FORM1_BUTTON2
              CONTROL ADD "Button", hForm1&,  %FORM1_BUTTON3,  "Button  3", 19, 69, 96, 15, _
                  %WS_CHILD OR %WS_VISIBLE OR %BS_PUSHBUTTON OR %WS_TABSTOP CALL CBF_FORM1_BUTTON3
              ' -----------------------------------------
              hForm1_ToolTip&=CreateDlgToolTip(hForm1&, 150, RGB(0,0,0), RGB(255,255,255))
              ' ---------------------------------------------------------------------------
              ' Convert ToolTip to Layered Window
              ' ---------------------------------------------------------------------------
              LOCAL EWS&, TColor&, Alpha&
              Alpha&=75  ' 50% alphablend
              TColor&=RGB(255,0,255)   ' select a transparent color if you like
              Alpha&=(Alpha&*255)/100
              EWS&=GetWindowLong(hForm1_ToolTip&, %GWL_EXSTYLE)
              EWS&=EWS& OR %WS_EX_LAYERED
              SetWindowLong hForm1_ToolTip&, %GWL_EXSTYLE, EWS&
              SetLayeredWindowAttributes hForm1_ToolTip&, TColor&, Alpha&, %LWA_COLORKEY OR %LWA_ALPHA
              ' ---------------------------------------------------------------------------
              AddTool hForm1_ToolTip&, hForm1&,%FORM1_BUTTON1
              AddTool hForm1_ToolTip&, hForm1&,%FORM1_BUTTON2
              AddTool hForm1_ToolTip&, hForm1&,%FORM1_BUTTON3
              ' -----------------------------------------
              DIALOG SHOW MODELESS hForm1& , CALL Form1_DLGPROC
          END SUB
          '
          CALLBACK FUNCTION Form1_DLGPROC
              SELECT CASE CBMSG
                  CASE %WM_NOTIFY
                      IF TestForToolTip(CBHNDL, CBMSG, CBWPARAM, CBLPARAM) THEN
                          FUNCTION=0
                          EXIT FUNCTION
                      END IF
                  CASE ELSE
              END SELECT
          END FUNCTION
          '
          CALLBACK FUNCTION CBF_FORM1_BUTTON1
              IF CBCTLMSG=%BN_CLICKED THEN
              END IF
          END FUNCTION
          '
          CALLBACK FUNCTION CBF_FORM1_BUTTON2
              IF CBCTLMSG=%BN_CLICKED THEN
              END IF
          END FUNCTION
          '
          CALLBACK FUNCTION CBF_FORM1_BUTTON3
              IF CBCTLMSG=%BN_CLICKED THEN
              END IF
          END FUNCTION
          This requires Windows 2000, XP or Vista to work.
          Chris Boss
          Computer Workshop
          Developer of "EZGUI"
          http://cwsof.com
          http://twitter.com/EZGUIProGuy

          Comment


          • #6
            I am attaching a file so the code can be downloaded.
            Attached Files
            Chris Boss
            Computer Workshop
            Developer of "EZGUI"
            http://cwsof.com
            http://twitter.com/EZGUIProGuy

            Comment

            Working...
            X