ListView.HitTest Method

Definition

在给定 X 和 Y 坐标的情况下,提供项信息。Provides item information, given an x- and y-coordinate.

Overloads

HitTest(Point)

在给定一个点的情况下,提供项信息。Provides item information, given a point.

HitTest(Int32, Int32)

在给定 X 和 Y 坐标的情况下,提供项信息。Provides item information, given x- and y-coordinates.

HitTest(Point)

在给定一个点的情况下,提供项信息。Provides item information, given a point.

public:
 System::Windows::Forms::ListViewHitTestInfo ^ HitTest(System::Drawing::Point point);
public System.Windows.Forms.ListViewHitTestInfo HitTest (System.Drawing.Point point);
member this.HitTest : System.Drawing.Point -> System.Windows.Forms.ListViewHitTestInfo
Public Function HitTest (point As Point) As ListViewHitTestInfo

Parameters

point
Point

Point,将检索该处的项信息。The Point at which to retrieve the item information. 坐标相对于控件的左上角。The coordinates are relative to the upper-left corner of the control.

Returns

ListViewHitTestInfo

项信息(在给定一个点的情况下)。The item information, given a point.

Exceptions

点包含小于 0 的坐标。The point contains coordinates that are less than 0.

Remarks

使用此方法可以确定某个点是否位于项或子项中,以及点在哪个位置(如标签或图像区域)。Use this method to determine whether a point is in an item or subitem and where in the item the point is located, such as on the label or image area. 如果坐标位于控件以外,则返回的 ListViewHitTestInfo 对象的 Location 属性为 "None";并且 ItemSubItem 属性设置为 nullIf the coordinates are located outside the control, the returned ListViewHitTestInfo object has a Location property of "None"; and the Item and SubItem properties are set to null.

HitTest(Int32, Int32)

在给定 X 和 Y 坐标的情况下,提供项信息。Provides item information, given x- and y-coordinates.

public:
 System::Windows::Forms::ListViewHitTestInfo ^ HitTest(int x, int y);
public System.Windows.Forms.ListViewHitTestInfo HitTest (int x, int y);
member this.HitTest : int * int -> System.Windows.Forms.ListViewHitTestInfo
Public Function HitTest (x As Integer, y As Integer) As ListViewHitTestInfo

Parameters

x
Int32

要在该处检索项信息的 X 坐标。The x-coordinate at which to retrieve the item information. 该坐标相对于控件的左上角。The coordinate is relative to the upper-left corner of the control.

y
Int32

要在该处检索项信息的 Y 坐标。The y-coordinate at which to retrieve the item information. 该坐标相对于控件的左上角。The coordinate is relative to the upper-left corner of the control.

Returns

ListViewHitTestInfo

项信息(在给定 X 和 Y 坐标的情况下)。The item information, given x- and y- coordinates.

Exceptions

X 坐标或 Y 坐标小于 0。The x- or y-coordinate is less than 0.

Examples

下面的代码示例演示如何使用 HitTest 方法来确定鼠标事件在 ListView中的位置。The following code example demonstrates using the HitTest method to determine the location of a mouse event in a ListView. 若要运行此示例,请将其粘贴到一个 Windows 窗体中,该窗体包含用项填充的名为 listView1ListViewTo run this example, paste it into a Windows Form that contains a ListView named listView1 that is populated with items. 在此示例中,将 listView1MouseDown 事件与 listView1_MouseDown 方法相关联。Associate the MouseDown event for listView1 with the listView1_MouseDown method in this example.

void HandleMouseDown(object sender, MouseEventArgs e)
{
    ListViewHitTestInfo info = listView1.HitTest(e.X, e.Y);
    MessageBox.Show(info.Location.ToString());
}
Private Sub HandleMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) _
        Handles Me.MouseDown, listView1.MouseDown

    Dim info As ListViewHitTestInfo = listView1.HitTest(e.X, e.Y)
    MessageBox.Show(info.Location.ToString())

End Sub

Remarks

使用此方法可以确定某个点是否位于项或子项中,以及点在哪个位置(如标签或图像区域)。Use this method to determine whether a point is in an item or subitem, and where in the item the point is located, such as on the label or image area. 如果坐标位于控件以外,则返回的 ListViewHitTestInfo 对象的 Location 属性为 "None";并且 ItemSubItem 属性设置为 nullIf the coordinates are located outside the control, the returned ListViewHitTestInfo object has a Location property of "None"; and the Item and SubItem properties are set to null.

Applies to