Control.MousePosition Control.MousePosition Control.MousePosition Control.MousePosition Property

定义

获取鼠标光标的位置(以屏幕坐标表示)。Gets the position of the mouse cursor in screen coordinates.

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

属性值

Point,它包含鼠标光标相对于屏幕左上角的坐标。A Point that contains the coordinates of the mouse cursor relative to the upper-left corner of the screen.

示例

下面的代码示例将TreeNode到可编辑状态时在用户按 ALT + E,鼠标光标位于树节点标签。The following code example puts a TreeNode label into an editable state when the user presses ALT+E while the mouse cursor is over the tree node. 用户完成操作后编辑标签,标签之前,无法编辑再次再次按下 ALT + E 键组合。After the user is done editing the label, the labels cannot be edited again until the ALT+E key combination is pressed again. 此示例要求具有TreeViewFormThis example requires that you have a TreeView on a Form. 在树视图还应具有至少一个TreeNode在其Nodes集合。The tree view should also have at least one TreeNode in its Nodes collection.

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 mose 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 mose 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 mose 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

注解

MousePosition属性返回Point在引用属性时的时间表示鼠标光标位置。The MousePosition property returns a Point that represents the mouse cursor position at the time the property was referenced. 坐标指示的位置在屏幕上,不相对于控件,并且无论是否将光标悬停在控件返回。The coordinates indicate the position on the screen, not relative to the control, and are returned regardless of whether the cursor is positioned over the control. 在屏幕的左上角的坐标为 0,0。The coordinates of the upper-left corner of the screen are 0,0.

MousePosition属性等同于Cursor.Position属性。The MousePosition property is identical to the Cursor.Position property.

适用于

另请参阅