Control.MousePosition 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得滑鼠游標在螢幕座標中的位置。
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,包含與螢幕左上角相對的滑鼠游標座標。
範例
下列程式碼範例會在使用者按下 ALT+E,而滑鼠游標位於樹狀節點上方時,將標籤放入 TreeNode 可編輯的狀態。 使用者完成編輯標籤之後,必須先再次按下 ALT+E 按鍵組合,才能再次編輯標籤。 此範例需要您在 上 Form 擁有 TreeView 。 樹狀檢視在其集合中 Nodes 也應該至少有一個 TreeNode 。
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 表示參考屬性時滑鼠游標位置。 座標會指出畫面上的位置,而不是相對於控制項,而且不論游標是否位於控制項上,都會傳回。 畫面左上角的座標為 0,0。
屬性 MousePosition 與 Cursor.Position 屬性相同。