Control.MousePosition 屬性

定義

取得滑鼠游標在螢幕座標中的位置。

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

屬性值

Point

Point,包含與螢幕左上角相對的滑鼠游標座標。

範例

下列程式碼範例會在使用者按下 ALT+E,而滑鼠游標位於樹狀節點上方時,將標籤放入 TreeNode 可編輯的狀態。 使用者完成編輯標籤之後,必須先再次按下 ALT+E 按鍵組合,才能再次編輯標籤。 此範例需要您在 上 Form 擁有 TreeView 。 樹狀檢視在其集合中 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。

屬性 MousePositionCursor.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

另請參閱