How to move mouse to second screen monitor? - c++

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.

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.

Related

Qt - how to glue two windows and move them together?

Like the qmmp(Qt) music player ui design, these two or three windows are in fact in the same window, because there is only a dock icon, and these windows can move together and attach to each other.
I read the source code, it seems use QDockWidget. But I really don't know the details how to get it.
When you manually move the secondary window, in this case - the playlist, you check where the manual move ends, and if it is on the edges of the primary window, you glue it by simply binding its position to the position and dimensions of the primary window.
Since the window position and dimensions are properties, they have notification signals, so you can connect those to a function that automatically moves the glued window.
And finally, when you attempt to manually move the secondary window, you un-glue by disconnecting.
You can easily support offset gluing instead of a purely horizontal or vertical one, by calculating and storing the positioning offset and applying it on every primary window move.
If the drop happens within a given threshold of the primary window you can snap to the edge. If you factor in the mouse position relative to the dragged window, you can even snap particular edges together.

How to detect when the mouse is moving outside the window?

I'd like to detect the mouse move event when it's moving outside the window by using MFC. I have used OnNcMouseMove() but it only detects the mouse move event when it's moving to the non-client area, not outside the window. So how can I do to detect while the mouse is moving outside the window?
All I did was set up a 100ms timer that calls GetCursorPos(). You can tweak that timer value to make it higher resolution if needed, but for my specific UI need, 100ms was good enough - it dumped (x,y) coordinates to a status bar.

Mouse click on a background window without moving mouse c++

I need to know if there is a code in c++ that allows to simulate a click, on a background window (if that's impossible, a foreground window will do fine), without moving the mouse.
I also need to click on specific coordinates, and drag an item to some other position (always without moving the mouse).
Example:
my mouse pointer is at (500,700),
but i need to left click at (100,150),
and drag to (700,300).
I need to be able to move my mouse pointer without affecting the program,
and the program must run correctly without moving, or locking, the mouse pointer.
If this action is impossible in c++, a VB code will be apreciated.
From a C++ Windows application, you can call Windows API directly by first finding the window using FindWindow that will give you the window handle you want, then finding the area within this window that you want to click. You can use API like GetWindowRect for this.
Finally, you can send this window, or an area within it, mouse messages using mouse_event function to cause the mouse to move, click, drag and let go at a new location.

Draw moving icon that is top most all the time like mouse cursor and work for full screen apps

I need to draw an icon that moves approximately in sync with mouse cursor and is always on top of all windows.
OS: Windows 7
I have a solutions that work to some extend by drawing my icon in a top most transparent window. There are some major drawbacks in this solution since that top most window interferes with other top most windows and some full screen apps do not work correctly. Examples are start menu and task bar that will overlay my window if I do not regulary set it to be top most. For some full screen applications performance of updating position of window with icon hugely drops and it does not follow mouse smoothly.
There is another method that I came across where an icon is drawn directly to the device context of desktop Draw mouse pointer icon?. This solution has a drawback that there seems to be no good way of how to remove "trail", especially if desktop content changes quickly.
So my question: is there an ultimate solution that does not have the above mentioned problems?
Is it possible to draw above all windows in the "layer" of mouse cursor? Or make a second mouse cursor with my custom icon that I will control (I know that widows can display two independently controlled mouse cursors like CPNMouse)?
Can someone point me to the right direction?
Thanks!
Use a top-most window with transparency set via WS_EX_LAYERED / UpdateLayeredWindow.
If you set the WS_EX_TRANSPARENT style as well then the window won't intercept mouse input.

Getting mouse cursor position and button state on Windows

What is the most appropriate way of getting the mouse cursor position or button state on Windows (Windows 7 and above)? I previously used DirectInput, but I am not longer using it and do not wish to. I saw there is GetCursorPos, however, I do not see anything for getting mouse button states. I have read previously that just reading the window messages (such as WM_LBUTTONUP) was considered "slow" for real time applications, so I do not know of any other option.
If you want to poll/query the current cursor position, you can use GetCursorPos. To see the button states, use GetAsyncKeyState.
If you are implementing a message loop in a window, the notification you will get for a mouse movement is WM_MOUSEMOVE. You will be notified of mouse inputs through the notifications listed here.
WM_LBUTTONUP is as good as any window message, for windowed games is great because it is generated only when the mouse clicks the client area, so you can resize and move the window freely.
As an alternative to direct input, you can use raw inputs which take up some more code to initialize, but it's the best way to go with the mouse movement since WM_INPUT is generated when the physical mouse moves, not the cursor, so you can clip the cursor in the client area without worrying that the user may hit the side of the clip rect and the mouse movement messages won't be generated anymore.
link