共用方式為


Control.MousePosition 屬性

定義

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

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

屬性值

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

範例

以下程式碼範例將 TreeNode 標籤置於可編輯狀態,當使用者按下 ALT+E 且滑鼠游標位於樹狀節點上時。 使用者完成標籤編輯後,除非再次按下 ALT+E 鍵組合,否則無法再次編輯標籤。 這個例子要求你有一個 TreeViewForm。 樹狀檢視的收藏中也應該至少包含一個TreeNodeNodes

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 屬性回傳的 a Point 代表該屬性被引用時滑鼠游標的位置。 座標表示螢幕上的位置,而非相對於控制器,無論游標是否位於控制器上方,座標都會回傳。 螢幕左上角的座標是 0,0。

MousePosition 物業與該 Cursor.Position 物業相同。

適用於

另請參閱