当用户将鼠标悬停在特定的ToolStripButton上时,我想更改其光标,但不在ToolStrip上的其他项目上更改。如何设置按钮的光标?
最佳答案
因为ToolStripItem不继承自Control,所以它没有Cursor属性。
您可以在MouseEnter事件上设置表单光标,并在MouseLeave事件上恢复表单光标,VB示例如下:
Dim savedCursor As Windows.Forms.Cursor
Private Sub ToolStripButton1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles ToolStripButton1.MouseEnter
If savedCursor Is Nothing Then
savedCursor = Me.Cursor
Me.Cursor = Cursors.UpArrow
End If
End Sub
Private Sub ToolStripButton1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles ToolStripButton1.MouseLeave
Me.Cursor = savedCursor
savedCursor = Nothing
End Sub
更新资料
这是C#中的相同答案:
private Cursor savedCursor;
private void ToolStripButton1_MouseEnter(object sender, EventArgs e) {
if (savedCursor == null) {
savedCursor = this.Cursor;
this.Cursor = Cursors.UpArrow;
}
}
private void ToolStripButton1_MouseLeave(object sender, EventArgs e) {
this.Cursor = savedCursor;
savedCursor = null;
}