HitTest Method

Purpose

Returns a value when the mouse is located at the coordinates of x and y.

Syntax

value = Object.HitTest(x!, y!)

value = MonthView.HitTest(x!, y!, Date As Date)

Object:Label, ListView, TreeView
x!, y!:Single exp

Description

The coordinates should be in the Form's ScaleMode units.

Label - HitTest returns a Boolean = True if the mouse pointer is over a character pixel.

ListView - Returns a reference to a ListItem when the coordinate is over a ListView element. If no object exists at the specified coordinates, the HitTest method returns Nothing.

TreeView - Returns a reference to a Node object located at the coordinates x and y. If no object exists at the specified coordinates, the HitTest method returns Nothing.

MonthView - Returns a date located at the set of coordinates x!, y! in the ByRef variable Date. Most often used with drag-and-drop operations to determine if a drop target item is available at the present location. (x!, y!) are the coordinates of a target date in Twips and Date is the variable which receives the date under the mouse.

The HitTest method returns the following values which specify the part of the calendar over which the mouse pointer is hovering:

mvwCalendarBack(0) - The calendar background.

mvwCalendarDate(1) - Calendar date.

mvwCalendarDateNext(2) - When this area is clicked, the calendar displays the following month.

mvwCalendarDatePrev(3) - When this area is clicked, the calendar displays the previous month.

mvwCalendarDay(4) - The day labels above the dates.

mvwCalendarWeekNum(5) - The week number, if ShowWeekNumbers is set to True.

mvwNoWhere(6) - Bottom edge of the calendar.

mvwTitleBack(7) - Background of the calendar.

mvwTitleBtnNext(8) - The Next button in the title area.

mvwTitleBtnPrev(9) - The Previous button in the title area.

mvwTitleMonth(10) - The month string in the title.

mvwTitleYear(11) - The year string in the title.

mvwTodayLink(12) - When this area is clicked, the calendar displays the current month and day. Only available if ShowToday is set to True.

Example

An example with ListView

OpenW 1 : Set Me = Win_1

Global a$, m As Int, n As Int

Dim li As ListItem

Ocx ListView lv1 = , 10, 10, 500, 150 : lv1.View = 3

For n = 1 To 5 : lv1.ColumnHeaders.Add , , "Column" & n : Next n

For n = 1 To 5 :

a$ = "" : For m = 1 To 5 : a$ = a$ & "Item " & ((n - 1) * 5) + m & Iif(m <> 5, ";", "") : Next m

lv1.Add , , "" : lv1(n).AllText = a$ : If n = 2 Then lv1(n).Ghosted = True

Next n

lv1.FullRowSelect = True // If this is omitted then HitTest only works on the first column

Ocx Label res = "", 10, 200, 150, 15  : res.BackColor = RGB(255, 255, 255)

Do : Sleep : Until Me Is Nothing

 

Sub lv1_MouseMove(Button&, Shift&, x!, y!)

// This is not called if you are hovering over a column header

x! = TwipsToPixelX(x!) : y! = TwipsToPixelY(y!)

If lv1.HitTest(x!, y!) Is Nothing

res.Caption = ""

Else

Set li = lv1.HitTest(x!, y!)

res.Caption = "Result: Line" & li.Index

EndIf

EndSub

Known Issues

The HitTest method does not appear to work with labels. There is a workaround (listed below), although it only works for single line labels:

OpenW 1

Ocx Label lbl = "A plain old label", 10, 10, 150, 15

Ocx Label res = "", 10, 30, 150, 15 : res.BackColor = RGB(255, 255, 255)

Do : Sleep : Until Me Is Nothing

 

Sub lbl_MouseMove(Button&, Shift&, x!, y!)

x! = TwipsToPixelX(x!) : y! = TwipsToPixelY(y!)

If x! < TwipsToPixelX(lbl.TextWidth(lbl.Text)) And y! < TwipsToPixelY(lbl.TextHeight(lbl.Text))

// instead of: If lbl.HitTest(x!, y!)

res.Caption = "Hovering over label text"

Else

res.Caption = ""

EndIf

EndSub

 

Sub Win_1_MouseMove(Button&, Shift&, x!, y!)

If Not res Is Nothing Then res.Caption = ""

EndSub

A crude option for labels with more than one line is to check that the pixel under the mousepointer is not the BackColor of the label. In this case, the lbl_MouseMove sub-routine would look like this:

 

Sub lbl_MouseMove(Button&, Shift&, x!, y!)

x! = TwipsToPixelX(x!) : y! = TwipsToPixelY(y!)

Local hdc As Long = GetWindowDC(lbl.hWnd), bcol As Int32 = lbl.BackColor, col As Int32 = GetPixel(hdc, x!, y!)

// Check to see if bcol is a system colour and, if so, convert

If (bcol And $FF000000) = $80000000 Then bcol = SysCol(bcol And $FF)

If col <> bcol  // instead of: If lbl.HitTest(x!, y!)

res.Caption = "Hovering over label text"

Else

res.Caption = ""

EndIf

EndSub

See Also

Label, ListView, ListItem, TreeView, Node

{Created by Sjouke Hamstra; Last updated: 12/10/2014 by James Gaite}