Why don't Cursor.Show () and Cursor.Hide () immediately hide or show the cursor?

I am writing a drag and drop system for the visualizer. When you click and drag, it moves what you see in the window. When the mouse lands on the edge of the panel, I begin to move the cursor so that it never leaves the field. It keeps track of the virtual position the cursor will be in if it is inside the box. This part of the code is working fine.

Anytime there is a MouseMoved event, and the position is inside the field, I do Cursor.Show (). If it's out of the box, I do Cursor.Hide (). When the user allows me to move from the mouse button, I do Cursor.Show ().

There are several issues. When the first Hidden Call occurs, it is not hidden. I have to get the virtual cursor position outside the containing window in order to hide it. When I go back, it becomes invisible, even though Show is called. Finally, when you release the mouse button, the cursor does not appear, even though Show is called.

Instead of asking people to debug my code, I'm just wondering what happens in the event system, which makes Cursor.Hide / Show not work as I expect. I got the impression that if the control is called Hide, the cursor will be hidden at any time when it is inside this control; Similarly, if I call a show from a control.

+5
source share
3 answers

Hans had this in a comment. Since this question has an answer, I feel that it should have an answer.

"It is believed that two shows and one hide do not hide the cursor." - Hans Passant

+9
source

For those who have this problem, try something like this:

    private bool _CursorShown = true;
    public bool CursorShown
    {
        get
        {
            return _CursorShown;
        }
        set
        {
            if (value == _CursorShown)
            {
                return;
            }

            if (value)
            {
                System.Windows.Forms.Cursor.Show();
            }
            else
            {
                System.Windows.Forms.Cursor.Hide();
            }

            _CursorShown = value;
        }
    }

and use it:

CursorShown = false; // Will hide the cursor
CursorShown = false; // Will be ignored
CursorShown = true; // Will show the cursor
CursorShown = true; // Will be ignored
+11
source

Could you simplify this

      //Whenever you want to hide the cursor           
       Cursor.Hide();
       countCursorHide++;

And show cursor

    int countCursorHide = 0;
    private void showCursor()
    {
        for (int i = 0; i < countCursorHide; i++)
        {
            Cursor.Show();
        }
        countCursorHide = 0;
    }
0
source

All Articles