Skip to main content

Understanding HitTests

  • 2 minutes to read

HitTest objects (or simply HitTests) are useful when responding to mouse actions such as movements, clicks, and drag-and-drop operations. To identify a particular grid View element located at the point being inspected, examine the hit code that is returned by a HitTest object’s HitTestCode function for this point. Then, cast the HitTest object to a class that corresponds to the inspected element to access element-specific properties.

Note

One element, which is only available at design time and present in all Views, is a View Selector. This is a special element that helps you select a particular View in a form in order to access its members via the Object Inspector. The View selector is located at the top-left corner of a View. The TcxGridDesignSelectorHitTest object contains the HitTest information about the View selector and its corresponding hit code is htDesignSelector.

The following code example implements the technique described above to hide bands and columns within the Banded Table View named btvCars when an end-user clicks on them using the right mouse button. The btvCarsMouseDown procedure is the Banded Table View’s OnMouseDown event handler.

procedure TForm1.btvCarsMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  AHitTest: TcxCustomGridHitTest;
begin
  if (Button = mbRight) and (Sender is TcxGridSite) then
  begin
    AHitTest := TcxGridSite(Sender).ViewInfo.GetHitTest(X, Y);
    case AHitTest.HitTestCode of
      htBandHeader:
        TcxGridBandHeaderHitTest(AHitTest).Band.Visible := False;
      htColumnHeader:
        TcxGridColumnHeaderHitTest(AHitTest).Column.Visible := False;
    end;
  end;
end;