Receive mouse move even cursor is outside control

丶灬走出姿态 提交于 2019-12-24 02:23:02

问题


I did something like scrollbar from Control. Everything is fine, but when cursor leaves control area, OnMouseMove is not received anymore.

When you use standard windows scrollbar, you can use it even if mouse cursor is outside the control's surface.

To avoid this, the only idea I have, is get cursor position from screen, then calculate scrollbar position on screen, and use timer or something to update my Control. But it sounds very hard and ugly.

Any idea how to fix it?

EDIT: I meant Control not UserControl.

EDIT2: It receives mouse! I had bug in code, I called MouseDown in MouseMove method, but in MouseDown I had X/Y constraint that will force to return if X<0 etc.


回答1:


What you are looking for is called mouse capture and is described here:

http://msdn.microsoft.com/en-us/library/ms171545(v=vs.80).aspx

In short:

  • on mouse down in your control you "capture" the mouse input
  • then as long as no-one else captures the mouse, you will receive all mouse movement events, even those outside of your control
  • you can release capture if you don't require the data anymore, optionally in "OnMouseUp"


来源:https://stackoverflow.com/questions/14191219/receive-mouse-move-even-cursor-is-outside-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!