开发者

WPF: How do I position the mouse cursor in code?

I am implementing the system menu (Restore, Move, Size...) 开发者_运维问答on a borderless window, and I want the mouse cursor to move to the center of the Window when size or move is selected.

Ideally in VB but C# is fine as well.


You can use the SetCursorPos function, something like:

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

'...

dim p as point
p.x = 100
p.y = 200
SetCursorPos p


A few tweaks and it seems 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜