共用方式為


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

適用於

另請參閱