1

Is there a method in PowerShell that would allow hiding the mouse cursor when hovering over a form element (for example, on the button)? Or even hide the cursor on the entire form. What is important, the mouse cursor should not be active at this time. (Explanation. That is, so that when the cursor is hidden, he does not click the button on the form. That is, the complete absence of the cursor on the form.)

Something like this:

$Button.CursorVisible = $False

I found a solution in C# (https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.cursor.hide?view=netframework-4.8code here), but I do not know the analogue in PowerShell. Thank you

1 Answer 1

3

I have no idea what you mean by " the mouse cursor should not be active at this time.", so I'll only focus on hiding and showing the mouse cursor when moved over a certain form control.

You should be able to do that by declaring a MouseEnter and MouseLeave event handler function on the control like

$control.Add_MouseEnter( {
    [System.Windows.Forms.Cursor]::Hide()
})

$control.Add_MouseLeave( {
    [System.Windows.Forms.Cursor]::Show()
})

Edit Thanks to your comment I now understand what you mean by the cursor being active or not.

To prevent the hidden cursor to be able to click on the control of form itself, add a check for this inside the control's Click event handler. Something like this:

$control.Add_Click({ 
    # the [Cursor.Current] property returns null if the mouse cursor is not visible
    # see: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.cursor.current?redirectedfrom=MSDN&view=netframework-4.8#System_Windows_Forms_Cursor_Current
    if (![System.Windows.Forms.Cursor]::Current) { return $false } 
})

Another way of doing this could be to not only Hide or Show the mouse cursor when hovering over the control, but at the same time Enable or Disable the control. This will of course have the side-effect of the control's appearance changing..

$control.Add_MouseEnter( {
    [System.Windows.Forms.Cursor]::Hide()
    $this.Enabled = $false
})

$control.Add_MouseLeave( {
    [System.Windows.Forms.Cursor]::Show()
    $this.Enabled = $true
})
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Yes, this makes the mouse cursor invisible. Thanks. However, although the cursor is not visible, by clicking on the buttons on the form they are pressed. That is, the cursor is active, although it is not visible. It would be great if the cursor would not fully function on the form.
@КириллЗацепин Aha, in that case, inside the control's 'Click' event add a test to see if the cursor is hidden or not. Please see my edited answer.
Teo, does the method exist to make the mouse cursor "inactive" immediately to the entire form, all its controls? After hovering over the forum, the cursor remains visible, but no form controls can be pressed and hover, enter, move events are not triggered anywhere. That is, a complete shutdown of the mouse to all elements. Disabling not form elements (.Enabled = $ False), but disabling the mouse cursor itself?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.