MouseEventArgs.Location 属性

定义

获取鼠标在产生鼠标事件时的位置。

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

包含 Point 相对于控件左上角的 x 和 y- 鼠标坐标(以像素为单位)。

示例

下面的代码示例使用 Location 该属性跟踪鼠标左键,并绘制一系列直线段,以响应用户输入。 如果隐藏窗体,然后重新播放该窗体,则该示例不会保留绘制的线条;为简单起见,省略了此代码。

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

适用于

另请参阅