MouseEventArgs.X 屬性


取得滑鼠在產生滑鼠事件期間的 X 座標。Gets the x-coordinate of the mouse during the generating mouse event.

 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.