List View with tooltips
by Richard Russell, April 2011
A List View control has built-in support for tooltips, but this feature requires your program to respond to the LVN_EX_INFOTIP notification, and in BBC BASIC for Windows that is only possible using assembler code.
The program listed below uses an alternative technique that doesn't require assembler code. The position of the mouse cursor is constantly monitored, and when it moves over the List View control a tooltip is displayed. As shown, the displayed message depends only on what item (row) the mouse is hovering over, but it could easily be adapted so the message is specific to the subitem (column) as well.
The code requires v4.70 of the Windows Common Controls, so won't work on versions of Windows prior to XP. It is also necessary to ensure that the Use Windows XP visual styles option is selected if the program is compiled.
REM. List View with tooltips, Richard Russell, 02-Apr-2011 REM. Install libraries: INSTALL @lib$+"WINLIB5" nColumns% = 5 REM!WC Windows constants: LVCF_FMT = 1 LVCF_SUBITEM = 8 LVCF_TEXT = 4 LVCF_WIDTH = 2 LVIF_PARAM = 4 LVIF_STATE = 8 LVIF_TEXT = 1 LVM_GETITEMRECT = 4110 LVM_GETTOOLTIPS = 4174 LVM_INSERTCOLUMN = 4123 LVM_INSERTITEM = 4103 LVM_SETITEMTEXT = 4142 LVM_SUBITEMHITTEST = 4153 LVS_REPORT = 1 TTM_ADDTOOL = &404 TTM_NEWTOOLRECT = &406 TTM_UPDATETIPTEXT = &40C ON ERROR SYS "MessageBox", @hwnd%, REPORT$, 0, 48 : PROCcleanup : QUIT ON CLOSE PROCcleanup : QUIT REM. Declare and initialise structures: DIM pt{x%,y%}, rc{l%,t%,r%,b%} DIM ti{cbSize%, uFlags%, hwnd%, uId%, rect{} = rc{}, hinst%, lpszText%} DIM lvh{pt{} = pt{}, flags%, iItem%, iSubItem%, iGroup%} DIM lvc{mask%, fmt%, cx%, pszText%, cchTextMax%, iSubItem%} DIM lvi{mask%, iItem%, iSubItem%, state%, stateMask%, pszText%, \ \ cchTextMax%, iImage%, lParam%} lvc.mask% = LVCF_WIDTH OR LVCF_SUBITEM OR LVCF_FMT OR LVCF_TEXT lvi.mask% = LVIF_TEXT OR LVIF_STATE OR LVIF_PARAM REM. Create list view control: SYS "InitCommonControls" dx% = @vdu.tr% dy% = @vdu.tb% hwndLV% = FN_createwindow("SysListView32", "", 0, 0, dx%, dy%, 0, LVS_REPORT, 0) FOR col% = 0 TO nColumns%-1 title$ = "Column " + STR$col% + " header" + CHR$0 lvc.cx% = dx% DIV nColumns% lvc.pszText% = !^title$ lvc.cchTextMax% = LEN(title$) lvc.iSubItem% = col% SYS "SendMessage", hwndLV%, LVM_INSERTCOLUMN, col%, lvc{} NEXT SYS "SendMessage", hwndLV%, LVM_GETTOOLTIPS, 0, 0 TO hwndTT% ti.cbSize% = DIM(ti{}) ti.hwnd% = hwndLV% SYS "SendMessage", hwndTT%, TTM_ADDTOOL, 0, ti{} REM. Populate list view control: FOR item% = 0 TO 10 FOR col% = 0 TO nColumns% text$ = "Item " + STR$item% + " column " + STR$col% + CHR$0 lvi.iItem% = item% lvi.iSubItem% = col% lvi.pszText% = !^text$ lvi.cchTextMax% = LEN(text$) IF col% THEN SYS "SendMessage", hwndLV%, LVM_SETITEMTEXT, item%, lvi{} ELSE SYS "SendMessage", hwndLV%, LVM_INSERTITEM, item%, lvi{} ENDIF NEXT col% NEXT item% REM. Polling loop, monitoring mouse position: oldx% = -1 oldy% = -1 REPEAT WAIT 1 SYS "GetCursorPos", pt{} SYS "ScreenToClient", hwndLV%, pt{} IF pt.x%<>oldx% OR pt.y%<>oldy% THEN oldx% = pt.x% oldy% = pt.y% PROCtooltip(pt.x%, pt.y%) ENDIF UNTIL FALSE DEF PROCtooltip(x%, y%) LOCAL tip$ lvh.pt.x% = x% lvh.pt.y% = y% SYS "SendMessage", hwndLV%, LVM_SUBITEMHITTEST, 0, lvh{} IF lvh.iItem% >= 0 THEN SYS "SendMessage", hwndLV%, LVM_GETITEMRECT, lvh.iItem%, rc{} tip$ = "Tooltip for item " + STR$(lvh.iItem%) + CHR$0 ti.lpszText% = !^tip$ ti.rect.l% = rc.l% ti.rect.r% = rc.r% ti.rect.t% = rc.t% ti.rect.b% = rc.b% SYS "SendMessage", hwndTT%, TTM_UPDATETIPTEXT, 0, ti{} SYS "SendMessage", hwndTT%, TTM_NEWTOOLRECT, 0, ti{} ENDIF ENDPROC DEF PROCcleanup hwndLV% += 0 : IF hwndLV% PROC_closewindow(hwndLV%) : hwndLV% = 0 ENDPROC