MouseEventArgs.Location MouseEventArgs.Location MouseEventArgs.Location MouseEventArgs.Location Property

定义

获取鼠标在产生鼠标事件时的位置。Gets the location of the mouse during the generating mouse event.

public:
 property System::Drawing::Point Location { System::Drawing::Point get(); };
public System.Drawing.Point Location { get; }
member this.Location : System.Drawing.Point
Public ReadOnly Property Location As Point

属性值

一个 Point,其中包含鼠标相对于窗体左上角的 x 坐标和 y 坐标(以像素为单位)。A Point that contains the x- and y- mouse coordinates, in pixels, relative to the upper-left corner of the form.

示例

下面的代码示例使用Location属性来跟踪鼠标左键单击并绘制一系列直线段来响应用户输入。The following code example uses the Location property to track left mouse clicks and draw a series of straight line segments in response to user input. 如果隐藏了窗体, 然后重新显示, 则该示例不会保留绘制的行;省略此代码的目的是为了简单起见。The example does not persist the drawn lines if you hide the form and then redisplay it; this code was omitted for simplicity.

Point firstPoint;
Boolean haveFirstPoint;

public void EnableDrawing()
{
    this.MouseDown += new MouseEventHandler(Form1_MouseDownDrawing);
}

void Form1_MouseDownDrawing(object sender, System.Windows.Forms.MouseEventArgs e)
{
    if (haveFirstPoint)
    {
        Graphics g = this.CreateGraphics();
        g.DrawLine(Pens.Black, firstPoint, e.Location);
        haveFirstPoint = false;
    }
    else
    {
        firstPoint = e.Location;
        haveFirstPoint = true;
    }
}
Dim FirstPoint As Point
Dim HaveFirstPoint As Boolean = False

Private Sub Form1_MouseDownDrawing(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    If HaveFirstPoint Then
        Dim g As Graphics = Me.CreateGraphics()
        g.DrawLine(Pens.Black, FirstPoint, e.Location)
        HaveFirstPoint = False
    Else
        FirstPoint = e.Location
        HaveFirstPoint = True
    End If
End Sub

适用于

另请参阅