Compartilhar via


Control.MousePosition Propriedade

Definição

Obtém a posição do cursor do mouse nas coordenadas da tela.

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

Valor da propriedade

Um Point que contém as coordenadas do cursor do mouse em relação ao canto superior esquerdo da tela.

Exemplos

O exemplo de código a seguir coloca um TreeNode rótulo em um estado editável quando o usuário pressiona ALT+E enquanto o cursor do mouse está sobre o nó de árvore. Depois que o usuário terminar de editar o rótulo, os rótulos não poderão ser editados novamente até que a combinação de teclas ALT+E seja pressionada novamente. Este exemplo requer que você tenha um TreeViewForm. O modo de exibição de árvore também deve ter pelo menos um TreeNode em sua Nodes coleção.

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

Comentários

A MousePosition propriedade retorna um Point que representa a posição do cursor do mouse no momento em que a propriedade foi referenciada. As coordenadas indicam a posição na tela, não em relação ao controle e são retornadas independentemente de o cursor estar posicionado sobre o controle. As coordenadas do canto superior esquerdo da tela são 0,0.

A MousePosition propriedade é idêntica à Cursor.Position propriedade.

Aplica-se a

Confira também