Control.MousePosition 屬性

定義

取得滑鼠游標在螢幕座標中的位置。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

Point,包含與螢幕左上角相對的滑鼠游標座標。A Point that contains the coordinates of the mouse cursor relative to the upper-left corner of the screen.

範例

下列程式碼範例會在滑鼠游標停留在樹狀節點上時,將 TreeNode 標籤放入可編輯的狀態。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. 此範例會要求您在 Form上有 TreeViewThis example requires that you have a TreeView on a Form. 樹狀檢視在其 Nodes 集合中也應該至少有一個 TreeNodeThe 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 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

備註

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.

適用於

另請參閱