Control.MousePosition 属性

定义

获取鼠标光标的位置(以屏幕坐标表示)。

C#
public static System.Drawing.Point MousePosition { get; }

属性值

Point

Point,它包含鼠标光标相对于屏幕左上角的坐标。

示例

下面的代码示例在鼠标光标位于树节点上时按 Alt+E 时,将标签置于 TreeNode 可编辑状态。 用户编辑标签后,在再次按下 ALT+E 键组合之前,无法再次编辑标签。 此示例要求你有一个 TreeView Form. 树视图还应在其Nodes集合中至少有一个TreeNode

C#
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;
}

注解

MousePosition 属性返回一个 Point 值,该值表示引用属性时鼠标光标位置。 坐标指示屏幕上的位置,而不是相对于控件的位置,无论光标是否放置在控件上,都会返回。 屏幕左上角的坐标为 0,0。

MousePosition 属性与 Cursor.Position 该属性相同。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
Windows Desktop 3.0, 3.1, 5, 6, 7

另请参阅