How to change cursor for ToolStripButton?

I want to change the user cursor when it hovers over a specific ToolStripButton, but not at other ToolStrip elements. How to set the button cursor?

+3
source share
3 answers

Because ToolStripItem does not inherit Control, it does not have a Cursor property.

You can set the form cursor to the MouseEnter event and restore the form cursor in the MouseLeave event, the following VB example:

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 #:

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;
}
+7
source

To change the cursor, you must set the Toolstrip.Cursor property. Yes, you are right, this will change the mouse cursor for all buttons on the toolbar.

, OnMouseEnter , , .

+1

Go down to Win32 and process WM_SETCURSOR. You can put your own logic to change the cursor based on testing the strokes for the button. Check out this article by Raymond Chen for a better understanding of how the Cursor is set.

+1
source

Source: https://habr.com/ru/post/1702915/


All Articles