Control.MousePosition Właściwość

Definicja

Pobiera położenie kursora myszy we współrzędnych ekranu.

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

Wartość właściwości

Point

Element Point zawierający współrzędne kursora myszy względem lewego górnego rogu ekranu.

Przykłady

Poniższy przykład kodu umieszcza etykietę TreeNode w stanie edytowalnym, gdy użytkownik naciska klawisze ALT+E, gdy kursor myszy znajduje się nad węzłem drzewa. Po zakończeniu edytowania etykiety przez użytkownika nie można edytować etykiet ponownie, dopóki kombinacja klawisza ALT+E nie zostanie ponownie naciśnięta. W tym przykładzie wymagany jest element TreeView w obiekcie Form. Widok drzewa powinien również zawierać co najmniej jeden TreeNode element w swojej Nodes kolekcji.

private:
   void treeView1_KeyDown( Object^ /*sender*/, KeyEventArgs^ e )
   {
      /* If the 'Alt' and 'E' keys are pressed,
         * allow the user to edit the TreeNode label. */
      if ( e->Alt && e->KeyCode == Keys::E )
      {
         treeView1->LabelEdit = true;
         
         // If there is a TreeNode under the mouse cursor, begin editing.
         TreeNode^ editNode = treeView1->GetNodeAt( treeView1->PointToClient( Control::MousePosition ) );
         if ( editNode != nullptr )
         {
            editNode->BeginEdit();
         }
      }
   }

   void treeView1_AfterLabelEdit( Object^ /*sender*/, NodeLabelEditEventArgs^ /*e*/ )
   {
      // Disable the ability to edit the TreeNode labels.
      treeView1->LabelEdit = false;
   }
private void treeView1_KeyDown(object sender, KeyEventArgs e)
{
   /* If the 'Alt' and 'E' keys are pressed,
      * allow the user to edit the TreeNode label. */
   if(e.Alt && e.KeyCode == Keys.E)
         
   {
      treeView1.LabelEdit = true;
      // If there is a TreeNode under the mouse cursor, begin editing. 
      TreeNode editNode = treeView1.GetNodeAt(
         treeView1.PointToClient(System.Windows.Forms.Control.MousePosition));
      if(editNode != null)
      { 
         editNode.BeginEdit();
      }
   }
}

private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
   // Disable the ability to edit the TreeNode labels.
   treeView1.LabelEdit = false;
}
Private Sub treeView1_KeyDown(sender As Object, _
  e As KeyEventArgs) Handles treeView1.KeyDown
   ' If the 'Alt' and 'E' keys are pressed,
   ' allow the user to edit the TreeNode label. 
   If e.Alt And e.KeyCode = Keys.E Then
      treeView1.LabelEdit = True
      ' If there is a TreeNode under the mouse cursor, begin editing. 
      Dim editNode As TreeNode = treeView1.GetNodeAt( _
        treeView1.PointToClient(System.Windows.Forms.Control.MousePosition))
      If (editNode IsNot Nothing) Then
         editNode.BeginEdit()
      End If
   End If
End Sub

Private Sub treeView1_AfterLabelEdit(sender As Object, _
  e As NodeLabelEditEventArgs) Handles treeView1.AfterLabelEdit
   ' Disable the ability to edit the TreeNode labels.
   treeView1.LabelEdit = False
End Sub

Uwagi

Właściwość MousePosition zwraca wartość Point , która reprezentuje położenie kursora myszy w czasie odwoływanie się do właściwości . Współrzędne wskazują położenie na ekranie, a nie względem kontrolki i są zwracane niezależnie od tego, czy kursor jest umieszczony nad kontrolką. Współrzędne lewego górnego rogu ekranu to 0,0.

Właściwość jest identyczna MousePosition z właściwością Cursor.Position .

Dotyczy

Zobacz też