In this video, you will learn how to obtain hit information. First, you will display tooltips indicating which element is currently under the mouse cursor. Then, you will use hit information to implement custom filtering UI.
Sometimes you may need to recognize which element is located at the specified screen coordinates in applications. For instance, you may have to determine which part of a View the user has clicked or double-clicked. For this purpose, each View type implements the CalcHitInfo method. It accepts a specific point measured in grid control client coordinates as its pt parameter and returns the newly created hit info object containing information on a View's corresponding element.
Note that different View types have corresponding types of such hit info objects. So, the GridHitInfo object type holds information about a point hit-tested within a Grid View, a CardHitInfo object is Card View specific and the BandedGridHitInfo type corresponds to a banded View.
All these types are derived from the BaseHitInfo class, which is the base one for hit info objects.
The image below shows the hit info objects' inheritance hierarchy.
Any hit info object exposes information about a tested point via a number of its properties that can be grouped into four logical categories:
-
properties identifying a View element that contains a test point (for instance, the Band, Column, RowHandle properties identify the band, column and row whose elements are under the test point);
-
properties indicating whether or not a test point resides over a particular View element (for instance, the InCard, InColumnPanel property indicates whether the test point is over a card and a column panel);
Type | Members |
CardHitInfo |
CardHitInfo.InCard, CardHitInfo.InCardButtons, CardHitInfo.InField
|
GridHitInfo
|
GridHitInfo.InColumn, GridHitInfo.InColumnPanel, GridHitInfo.InGroupColumn, GridHitInfo.InGroupPanel, GridHitInfo.InRow, GridHitInfo.InRowCell
|
BandedGridHitInfo
|
BandedGridHitInfo.InBandPanel, GridHitInfo.InColumn, GridHitInfo.InColumnPanel, GridHitInfo.InGroupColumn, GridHitInfo.InGroupPanel, GridHitInfo.InRow, GridHitInfo.InRowCell
|
-
the BaseHitInfo.HitPoint property representing a test point in coordinates relative to a grid's top-left corner;
-
the HitTest property identifying the type of element located under a test point.
In some cases you may need to find out which View is located at specified screen coordinates. For this purpose you can use the GridControl.GetViewAt method.
The following sample code shows how to identify the element located at a specific point, using the GridView.CalcHitInfo method.
In the example, the CalcHitInfo method is called when moving over a Grid Control with the mouse. The name of the current View element is displayed in the form's caption.
C# |
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Base.ViewInfo;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
private void gridControl1_MouseMove(object sender, MouseEventArgs e) {
GridControl grid = sender as GridControl;
if (grid == null) return;
BaseView view = grid.GetViewAt(e.Location);
BaseHitInfo baseHI = view.CalcHitInfo(e.Location);
GridHitInfo gridHI = baseHI as GridHitInfo;
if (gridHI != null)
Text = gridHI.HitTest.ToString();
}
|
VB |
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Views.Base.ViewInfo
Imports DevExpress.XtraGrid.Views.Grid.ViewInfo
Private Sub GridControl1_MouseMove(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles GridControl1.MouseMove
Dim grid As GridControl = sender
If grid Is Nothing Then
Return
End If
Dim view As BaseView = grid.GetViewAt(e.Location)
Dim baseHI As BaseHitInfo = view.CalcHitInfo(e.Location)
Dim gridHI As GridHitInfo = TryCast(baseHI, GridHitInfo)
If Not gridHI Is Nothing Then
Text = gridHI.HitTest.ToString()
End If
End Sub
|