MouseEventArgs.X 属性

定义

获取鼠标在产生鼠标事件时的 x 坐标。Gets the x-coordinate of the mouse during the generating mouse event.

public:
 property int X { int get(); };
public int X { get; }
member this.X : int
Public ReadOnly Property X As Integer

属性值

鼠标的 X 坐标(以像素为单位)。The x-coordinate of the mouse, in pixels.

示例

下面的代码示例使用XY属性将鼠标指针的当前位置显示在ToolTip窗口中。The following code example uses the X and Y properties to display the current position of the mouse pointer in a ToolTip window.

ToolTip trackTip;

private void TrackCoordinates()
{
    trackTip = new ToolTip();
    this.MouseMove += new MouseEventHandler(Form1_MouseMove);
}

void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
    String tipText = String.Format("({0}, {1})", e.X, e.Y);
    trackTip.Show(tipText, this, e.Location);
}
Dim TrackTip As ToolTip

Private Sub TrackCoordinates()
    TrackTip = New ToolTip()
End Sub

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
    Dim TipText As String = String.Format("({0}, {1})", e.X, e.Y)
    TrackTip.Show(TipText, Me, e.Location)
End Sub

注解

鼠标坐标根据所引发的事件而有所不同。The mouse coordinates vary based on the event being raised. 例如, 处理Control.MouseMove事件时, 鼠标坐标值相对于引发事件的控件的坐标。For example, when the Control.MouseMove event is handled, the mouse coordinate values are relative to the coordinates of the control that raised the event. 与拖放操作相关的一些事件具有相对于窗体原点或屏幕原点的关联鼠标坐标值。Some events related to drag-and-drop operations have associated mouse-coordinate values that are relative to the form origin or the screen origin.

适用于

另请参阅