Skip to main content

How to Determine the Time Block Under the Mouse Pointer

  • 2 minutes to read

The HitAtTime and Time properties of a time View’s HitTest object provide all the necessary functionality for the solution. HitAtTime determines whether the mouse pointer is currently located within a time block’s boundaries. Time returns the time corresponding to the tested time block.

A TcxHintStyleController component is used in the example to display real-time hit test results in the hint window at the mouse pointer location.

The scheduler’s hit testing logic calculates a coordinate point (see the hit test object’s HitPoint property) relative to the control’s top-left corner. To display a hint at the specified position, the TcxHintStyleController component requires screen coordinates (see the component’s ShowHint method). To supply this information, a GetMouseCursorPos routine from the cxControls unit is used in the example.

// ...
uses
  cxControls, cxHint;
// ...
procedure TForm1.cxScheduler1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
  AHitTest: TcxSchedulerDayViewHitTest;
  AHintPoint: TPoint;
begin
  with TcxScheduler(Sender) do
  begin
    AHitTest := ViewDay.HitTest;
    if AHitTest.HitAtTime then
    begin
      // obtain the mouse pointer position in screen coordinates
      AHintPoint := GetMouseCursorPos;
      cxHintStyleController1.ShowHint(AHintPoint.X, AHintPoint.Y + 25, 'Current time block:', DateTimeToStr(AHitTest.Time));
    end
    else
      cxHintStyleController1.HideHint;
  end;
end;
// hide the hint if the mouse pointer is out of the scheduler boundaries
procedure TForm1.cxScheduler1MouseLeave(Sender: TObject);
begin
  cxHintStyleController1.HideHint;
end;

The following image shows the result:

See Also