I want to change the user's cursor when they hover over a specific ToolStripButton, but not for the other items on the ToolStrip. How do I set the button's cursor?
我想在用户光标悬停在特定的ToolStripButton上时更改用户的光标,而不是用于ToolStrip上的其他项目。如何设置按钮的光标?
4 个解决方案
#1
Because ToolStripItem doesn't inherit from Control, it doesn't have a Cursor property.
因为ToolStripItem不从Control继承,所以它没有Cursor属性。
You could set the form cursor on the MouseEnter event, and restore the form cursor on the MouseLeave event, VB sample follows:
您可以在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
Update
And here is the same answer in C#:
这里是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;
}
#2
Drop down to Win32 and handle WM_SETCURSOR. You can put in your own custom logic to change the cursor based on hit testing for the button. Check this article by Raymond Chen for a better understanding of how the Cursor gets set.
下拉到Win32并处理WM_SETCURSOR。您可以根据按钮的命中测试输入自己的自定义逻辑来更改光标。查看Raymond Chen的这篇文章,以便更好地理解Cursor的设置方式。
#3
You must set the Toolstrip.Cursor property in order to change the cursor. Yes your are right, it will change the mouse cursor for all toolstrip buttons.
您必须设置Toolstrip.Cursor属性才能更改光标。是的,你是对的,它将改变所有工具条按钮的鼠标光标。
In order to get around this, create a OnMouseEnter event for each button on the toolstrip, and then set the cursor for the entire toolstrip to the cursor you want for that particular button.
为了解决这个问题,请为工具条上的每个按钮创建一个OnMouseEnter事件,然后将整个工具条的光标设置为您想要该特定按钮的光标。
#4
This is a best Approach:
这是一种最好的方法:
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
where 'ts' is the toolstrip bar, and tsbtnGuardar is a toolstrip button. It worked great for me
其中'ts'是工具条,tsbtnGuardar是工具条按钮。它对我很有用
#1
Because ToolStripItem doesn't inherit from Control, it doesn't have a Cursor property.
因为ToolStripItem不从Control继承,所以它没有Cursor属性。
You could set the form cursor on the MouseEnter event, and restore the form cursor on the MouseLeave event, VB sample follows:
您可以在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
Update
And here is the same answer in C#:
这里是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;
}
#2
Drop down to Win32 and handle WM_SETCURSOR. You can put in your own custom logic to change the cursor based on hit testing for the button. Check this article by Raymond Chen for a better understanding of how the Cursor gets set.
下拉到Win32并处理WM_SETCURSOR。您可以根据按钮的命中测试输入自己的自定义逻辑来更改光标。查看Raymond Chen的这篇文章,以便更好地理解Cursor的设置方式。
#3
You must set the Toolstrip.Cursor property in order to change the cursor. Yes your are right, it will change the mouse cursor for all toolstrip buttons.
您必须设置Toolstrip.Cursor属性才能更改光标。是的,你是对的,它将改变所有工具条按钮的鼠标光标。
In order to get around this, create a OnMouseEnter event for each button on the toolstrip, and then set the cursor for the entire toolstrip to the cursor you want for that particular button.
为了解决这个问题,请为工具条上的每个按钮创建一个OnMouseEnter事件,然后将整个工具条的光标设置为您想要该特定按钮的光标。
#4
This is a best Approach:
这是一种最好的方法:
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
where 'ts' is the toolstrip bar, and tsbtnGuardar is a toolstrip button. It worked great for me
其中'ts'是工具条,tsbtnGuardar是工具条按钮。它对我很有用