我想在用户将鼠标悬停在特定的ToolStripButton上时更改光标,但不适用于ToolStrip上的其他项。 如何设置按钮的光标?
我想在用户将鼠标悬停在特定的ToolStripButton上时更改光标,但不适用于ToolStrip上的其他项。 如何设置按钮的光标?
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;
}
为了改变鼠标光标,您必须设置Toolstrip.Cursor属性。是的,您说得对,它将改变所有toolstrip按钮的鼠标光标。
为了解决这个问题,在toolstrip上为每个按钮创建一个OnMouseEnter事件,然后将整个toolstrip的光标设置为您想要的特定按钮的光标。
这是最佳方法:
Private Sub tsbtnGuardar_MouseEnter(sender As Object, e As EventArgs) Handles tsbtnGuardar.MouseEnter
On Error Resume Next
ts.Cursor = Cursors.Hand
End Sub
Private Sub tsbtnGuardar_MouseLeave(sender As Object, e As EventArgs) Handles tsbtnGuardar.MouseLeave
On Error Resume Next
ts.Cursor = Cursors.Arrow
End Sub
其中'ts'是工具栏,而'tsbtnGuardar'是工具栏按钮。这对我非常有效。