WPF: How to place the mouse cursor in code?

I implement a system menu (Restore, Move, Size ...) in a borderless window, and I want the mouse cursor to move to the center of the window when a size or move is selected. Ideally in VB, but C # is good too.

+3
source share
2 answers

You can use a function SetCursorPos, for example:

Declare Function SetCursorPos& Lib "user32" (ByVal p As Point)

'...

dim p as point
p.x = 100
p.y = 200
SetCursorPos p
+2
source

A few tweaks and seem to work:

Private Declare Function SetCursorPos Lib "user32" (ByVal x As Int32, ByVal Y As Int32) As Int32

...

With Win

  Dim left As Int32 = CInt(.Left + .Width - CURSOR_OFFSET_MEDIUM)
  Dim top As Int32 = CInt(.Top + .Height / 2)

  SetCursorPos(left, top)

End With
+1
source

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


All Articles