2

I'm making an application that needs to programmatically move the mouse pointer. My ide is VC++ 2013. I'm using the winuser.h header with this function:

SetCursorPos(int x, int y);

But this moves the mouse only on the primary screen. For the case that has only one monitor this works fine. But if I'm in presentation mode, with 2 monitors I need to move the mouse to the second monitor.

so, I to need detect if has 2 monitors and if it has, move the mouse to the second monitor.

unobf
  • 7,158
  • 1
  • 23
  • 36
  • 4
    `EnumDisplayMonitors` gives you the list of monitors, and for each a rectangle that the monitor occupies on the virtual desktop. Then just move the mouse to coordinates that fall within the desired monitor's rectangle. – Igor Tandetnik Mar 12 '15 at 15:11
  • 1
    @IgorTandetnik you should expand that into an answer.\ – Mark Ransom Mar 12 '15 at 15:13
  • @IgorTandetnik the function SetCursorPos(int x, int y); can be used for move the mouse in virtual desktop? – Paulo Filipe Dantas Mar 12 '15 at 15:24
  • Yes. Conceptually, there's a large desktop surface, and monitors are merely viewports into this surface. You can move the mouse to any point on this desktop area, and it will be visible on whatever monitor this point happens to fall into. – Igor Tandetnik Mar 12 '15 at 15:28
  • @IgorTandetnik Thanks for help, this solved my problem. I use this question http://stackoverflow.com/questions/26541484/enumdisplaymonitors-callback for get the example of use EnumDisplayMonitors. I have one more request, put that into an answer for i'm mark was solved for another users. – Paulo Filipe Dantas Mar 12 '15 at 19:52

1 Answers1

2

Conceptually, the mouse moves (and windows are positioned) on a virtual desktop - a large desktop surface that spans all monitors. A monitor is a viewport onto this surface - it shows whatever happens to fall into a certain rectangle.

EnumDisplayMonitors gives you the list of all monitors, and for each a rectangle that this monitor reveals on the virtual desktop. To move a mouse cursor to a particular monitor, simply move it to a point that falls within the rectangle corresponding to that monitor.

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85
  • This only half-works for me. See my question https://stackoverflow.com/questions/71578927/setting-cursor-position-half-works-from-one-monitor-to-another . Do you know how to overcome that? – ispiro Mar 23 '22 at 13:28